0
votes

I'm working on buttons for a shop section in my simple game. When the button is tapped, I want to briefly highlight it whitish for a little while. I am using SKActions to accomplish this. Here are the relevant parts of my code:

@interface TheScene()
{
    ...
    SKAction *visualFeedbackSequence;
    SKSpriteNode *buttonForFeedback;
    CGRect orangeCheckboxRect;

    ...
}
...
@implementation TheScene
...
-(void)initWithSize:(CGSize)size {
    ...
    buttonForFeedback = [SKSpriteNode spriteNodeWithImageNamed:@"button"];
    SKAction *makeItWhite = [SKAction colorizeWithColor:[SKColor whiteColor] colorBlendFactor:0.8 duration:0.85];
    SKAction *andBackAgain = [SKAction colorizeWithColor:[SKColor whiteColor] colorBlendFactor:0.0 duration:0.01];
    visualFeedback = [SKAction sequence:@[makeItWhite, andBackAgain]];
    orangeCheckboxRect = buttonForFeedback.frame;

...
return self;
}
...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
...
if(CGRectContainsPoint(orangeCheckboxRect, location) ) {
        NSLog(@"Running vis feedback on orange check"); //Inserted for debugging
        [orangeCheckbox runAction:visualFeedback];
        if([shopController hasUpgrade:@"theupgrade"]) {
            [shopController setCurrentUpgrade:@"theupgrade"];

        }
...

Note: All the shopController stuff does is return YES or NO if they have the selected upgrade.

The issue I am having is that when I first tap the button that I'm working with here, the effect shows up. But with any subsequent taps, the effect does not show up. The debug NSLog I put in the if statement does execute, signaling that the action should be running directly after it. I'm thinking I need to change the SKActions in use here, but I've already tried many combinations of the relevant actions with no success. Is this a known bug with SKActions or am I just doing something really wrong?

1
You need to do [yourAction copy] if you want to repeatedly use the same action.LearnCocos2D
Where should that be? Right after I run the action or somewhere else?nathreed

1 Answers

1
votes

Since you keep a reference to the feedback action, you need to make a copy every time you run it:

[orangeCheckbox runAction:[visualFeedback copy]];