Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is my solution, based on CCSprite hope it will be useful for somebody</p> <p>this is protocol for controlled object (like player or something):</p> <pre><code>@class AGSensitiveButton; @protocol AGSensitiveButtonControlledObjectProtocol &lt;NSObject&gt; @required - (void)sensitiveButtonTouchDown:(AGSensitiveButton *)sButton; - (void)sensitiveButtonTouchUp:(AGSensitiveButton *)sButton; @optional - (void)sensitiveTouchButtonKeepPressed:(AGSensitiveButton *)sButton forTime:(ccTime)pressTime; @end </code></pre> <p>.h file:</p> <pre><code>#import "CCSprite.h" #import "cocos2d.h" #import "AGSensitiveButtonControlledObjectProtocol.h" typedef enum { AGSensitiveButtonStateNormal = 0, AGSensitiveButtonStateHighlighted, AGSensitiveButtonStateDisabled } AGSensitiveButtonState; @interface AGSensitiveButton : CCSprite &lt;CCTargetedTouchDelegate&gt; @property (nonatomic, assign, getter = isEnabled) BOOL enabled; @property (nonatomic, assign) ccTime maximumTouchDuration; @property (nonatomic, weak) id &lt;AGSensitiveButtonControlledObjectProtocol&gt; controlledObject; @property (nonatomic, copy) void (^touchDownHandler)(); @property (nonatomic, copy) void (^touchUpHandler)(); + (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture highlightedTexture:(CCTexture2D *)highTexture; + (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture highlightedTexture:(CCTexture2D *)highTexture disabledtexture:(CCTexture2D *)disabledTexture; + (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture highlightedTexture:(CCTexture2D *)highTexture controllerObject:(id &lt;AGSensitiveButtonControlledObjectProtocol&gt;)controlledObject; + (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture highlightedTexture:(CCTexture2D *)highTexture disabledtexture:(CCTexture2D *)disabledTexture controlledObject:(id &lt;AGSensitiveButtonControlledObjectProtocol&gt;)controlledObject; + (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture highlightedTexture:(CCTexture2D *)highTexture disabledtexture:(CCTexture2D *)disabledTexture controlledObject:(id &lt;AGSensitiveButtonControlledObjectProtocol&gt;)controlledObject touchDownHandler:(void(^)(void))touchDownHandler touchUpHandler:(void(^)(void))touchUpHandler; - (void)setTexture:(CCTexture2D *)texture forState:(AGSensitiveButtonState)state; - (BOOL)isHighlighted; @end </code></pre> <p>implementation .m file:</p> <pre><code>#import "AGSensitiveButton.h" @interface AGSensitiveButton () @property (nonatomic, assign) AGSensitiveButtonState state; @property (nonatomic, strong) NSDictionary *stateTextures; @property (nonatomic, assign) ccTime currentTouchTime; @end @implementation AGSensitiveButton + (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture highlightedTexture:(CCTexture2D *)highTexture { return [self buttonWithNormalTexture:normalTexture highlightedTexture:highTexture controllerObject:nil]; } + (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture highlightedTexture:(CCTexture2D *)highTexture disabledtexture:(CCTexture2D *)disabledTexture { return [self buttonWithNormalTexture:normalTexture highlightedTexture:highTexture disabledtexture:disabledTexture controlledObject:nil]; } + (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture highlightedTexture:(CCTexture2D *)highTexture controllerObject:(id &lt;AGSensitiveButtonControlledObjectProtocol&gt;)controlledObject { return [self buttonWithNormalTexture:normalTexture highlightedTexture:highTexture disabledtexture:nil controlledObject:controlledObject]; } + (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture highlightedTexture:(CCTexture2D *)highTexture disabledtexture:(CCTexture2D *)disabledTexture controlledObject:(id &lt;AGSensitiveButtonControlledObjectProtocol&gt;)controlledObject { return [self buttonWithNormalTexture:normalTexture highlightedTexture:highTexture disabledtexture:disabledTexture controlledObject:controlledObject touchDownHandler:NULL touchUpHandler:NULL]; } + (id)buttonWithNormalTexture:(CCTexture2D *)normalTexture highlightedTexture:(CCTexture2D *)highTexture disabledtexture:(CCTexture2D *)disabledTexture controlledObject:(id &lt;AGSensitiveButtonControlledObjectProtocol&gt;)controlledObject touchDownHandler:(void(^)(void))touchDownHandler touchUpHandler:(void(^)(void))touchUpHandler { AGSensitiveButton *button = [[self alloc] initWithTexture:normalTexture rect:CGRectMake(0.0, 0.0, normalTexture.contentSize.width, normalTexture.contentSize.height)]; [button setTexture:normalTexture forState:AGSensitiveButtonStateNormal]; [button setTexture:highTexture forState:AGSensitiveButtonStateHighlighted]; [button setTexture:disabledTexture forState:AGSensitiveButtonStateDisabled]; button.controlledObject = controlledObject; button.touchDownHandler = touchDownHandler; button.touchUpHandler = touchUpHandler; return button; } - (void)setEnabled:(BOOL)enabled { [self setupNewState:enabled ? AGSensitiveButtonStateNormal : AGSensitiveButtonStateDisabled]; } - (BOOL)isEnabled { return (self.state != AGSensitiveButtonStateDisabled); } - (BOOL)isHighlighted { return (self.state == AGSensitiveButtonStateHighlighted); } - (void)toggleTextureForCurrentState { CCTexture2D *textureToSet = [self.stateTextures objectForKey:[NSNumber numberWithInteger:self.state]]; if (textureToSet) { self.texture = textureToSet; self.textureRect = CGRectMake(0.0, 0.0, textureToSet.contentSize.width, textureToSet.contentSize.height); } } - (void)setTexture:(CCTexture2D *)texture forState:(AGSensitiveButtonState)state { NSMutableDictionary *newStates = self.stateTextures.mutableCopy; if (texture) { [newStates setObject:texture forKey:[NSNumber numberWithInteger:state]]; } else { [newStates removeObjectForKey:[NSNumber numberWithInteger:state]]; } self.stateTextures = newStates.copy; } - (NSDictionary *)stateTextures { if (!_stateTextures) { _stateTextures = [[NSDictionary alloc] init]; } return _stateTextures; } - (void)onEnter { [super onEnter]; [self toggleTextureForCurrentState]; [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; [self scheduleUpdate]; } - (void)onExit { [super onExit]; [self unscheduleUpdate]; [[[CCDirector sharedDirector] touchDispatcher] removeDelegate:self]; } - (void)update:(ccTime)dt { if ((self.state == AGSensitiveButtonStateHighlighted) &amp;&amp; (self.maximumTouchDuration)) { self.currentTouchTime+=dt; if (self.currentTouchTime &gt;= self.maximumTouchDuration) { [self ccTouchEnded:nil withEvent:nil]; } else { if ([self.controlledObject respondsToSelector:@selector(sensitiveTouchButtonKeepPressed:forTime:)]) { [self.controlledObject sensitiveTouchButtonKeepPressed:self forTime:self.currentTouchTime]; } } } } - (CGRect)rectForTouches { return CGRectMake(-self.contentSize.width/2, -self.contentSize.height/2, self.contentSize.width, self.contentSize.height); } - (void)forwardTouchDownEventIntoHandlers { if ([self.controlledObject respondsToSelector:@selector(sensitiveButtonTouchDown:)]) { [self.controlledObject sensitiveButtonTouchDown:self]; } if (self.touchDownHandler) { self.touchDownHandler(); } } - (void)forwardTouchUpEventIntoHandlers { if ([self.controlledObject respondsToSelector:@selector(sensitiveButtonTouchUp:)]) { [self.controlledObject sensitiveButtonTouchUp:self]; } if (self.touchUpHandler) { self.touchUpHandler(); } } - (void)setupNewState:(AGSensitiveButtonState)state { if (self.state != state) { switch (state) { case AGSensitiveButtonStateHighlighted: { [self forwardTouchDownEventIntoHandlers]; break; } default: { if (self.state == AGSensitiveButtonStateHighlighted) { [self forwardTouchUpEventIntoHandlers]; } break; } } self.state = state; [self toggleTextureForCurrentState]; } } #pragma mark - CCTargetedTouchDelegate - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { if ((self.state != AGSensitiveButtonStateNormal) || (!CGRectContainsPoint([self rectForTouches], [self convertTouchToNodeSpaceAR:touch]))) { return NO; } self.currentTouchTime = 0.0; [self setupNewState:AGSensitiveButtonStateHighlighted]; return YES; } - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event { if (self.state == AGSensitiveButtonStateHighlighted) { if (!CGRectContainsPoint([self rectForTouches], [self convertTouchToNodeSpaceAR:touch])) { [self ccTouchEnded:touch withEvent:event]; } } } - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { if (self.state == AGSensitiveButtonStateHighlighted) { [self setupNewState:AGSensitiveButtonStateNormal]; } } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload