Crazycriss
Crazycriss

Reputation: 315

Action When Button is Pressed Down Cocos2d 3.0

How would i trigger - (void)onRightButtonClicked:(id)sender when the button is being pressed, not when it has been pressed (let go).

This is the code for the button

rightButton = [CCButton buttonWithTitle:@"" spriteFrame:[CCSpriteFrame frameWithImageNamed:@"right.png"]];
rightButton.position = ccp(self.contentSize.width/3.65, self.contentSize.height/10);
[rightButton setTarget:self selector:@selector(onRightButtonClicked:)];
[self addChild:rightButton];

And then what happens after the button has been pressed.

- (void)onRightButtonClicked:(id)sender
{
CCActionAnimate *animationAction = [CCActionAnimate actionWithAnimation:walkAnim];
CCActionRepeatForever *repeatingAnimation = [CCActionRepeatForever actionWithAction:animationAction];
[dino runAction:repeatingAnimation];
}

Upvotes: 2

Views: 2296

Answers (1)

Tibor Udvari
Tibor Udvari

Reputation: 3012

I'm afraid there is no 1-2 liner for that one, if you check CCButton.m you can see when actions are triggered

- (void) touchUpInside:(UITouch *)touch withEvent:(UIEvent *)event
{
    [super setHitAreaExpansion:_originalHitAreaExpansion];

    if (self.enabled)
    {
        [self triggerAction];
    }

    self.highlighted = NO;
}

Really dirty quickfix

If you want ALL buttons in your game to have this behaviour just move [self triggerAction] to - (void) touchEntered:(UITouch *)touch withEvent:(UIEvent *)eventin CCButton.

Elegant'ish solution

Make a custom subclass of CCButton where you override the touchUpInside and touchEntered methods. But since these are not public you would have to create a CCButton_Protected.h header where you import your normal CCButton header and put the private method signatures that you want to override in there.

Upvotes: 5

Related Questions