0
votes

I have created an SKAction group that alters the SKSpriteNode image (alpha and rotation) whilst playing a sound effect. However, when I runAction: the sound effect is played, but the image remains fixed.

In my subclassed SKSpriteNode (The Sheep) I have the following code that adds another SKSpriteNode (The Food) as a child:

-(void)setupEatingAction
{
    _grassEating = [SKSpriteNode spriteNodeWithImageNamed:@"SheepFood"];
    _grassEating.size = self.size;
    _grassEating.anchorPoint = CGPointMake(0.5, 0.5);
    _grassEating.alpha = 0.5;
    _grassEating.zPosition = -1;

    SKAction *fadeIn = [SKAction fadeInWithDuration:0.01];
    SKAction *soundFX = [SKAction playSoundFileNamed:@"Munching2s.mp3" waitForCompletion:NO];
    SKAction *rotate = [SKAction sequence:@[[SKAction rotateToAngle:degsToRads(-20.0) duration:kMovementAnimationInterval/4.0],[SKAction rotateToAngle:degsToRads(0.0) duration:kMovementAnimationInterval/4.0]]];
    SKAction *eatAction = [SKAction group:@[fadeIn,soundFX,rotate]];

    _grassEating.userData = [NSMutableDictionary dictionaryWithObject:eatAction forKey:KEY_EatAction];

    [self addChild:_grassEating];
}

Then when I want the action group to run, I do the following:

-(void)eat
{
    if(![_grassEating actionForKey:KEY_EatAction])
    {
        [_grassEating runAction:[_grassEating.userData objectForKey:KEY_EatAction] withKey:KEY_EatAction];
    }
}

_grassEating is a property of my parent node:

@property (nonatomic,retain) SKSpriteNode *grassEating;

As I said, I can hear the sound effect playing, but the image isn't changed?!? For the life of me I can't work out why, and I've spent a lot of time trying to work out what is going on... any ideas?

I should also point out (although I don't think is anything to do with it) that this problem appears in both simulator and on hardware - both running iOS7.

I don't know if this is related iOS Spritekit Cannot Run SKAction on a Subclassed SKSpriteNode

But, it's the closest I can find and doesn't appear to be the same problem (on the surface).

1
have you tried doing without saving it to userData ?hamobi
Yes. That was how I did it originally. Then after looking at the thread I've linked, I changed it to the method I have presented. Both methods gave the same result - sounds but no image change.VariableSquid
Have you tried checking whether _grassEating is valid? Try setting it to hidden or remove it from its parent to see if the reference to the sprite is correct.Wraithseeker
_grassEating has an address, and I can "po" it. I check that the userData contains one object, presumably the SKAction i have put in it. Is that what you mean by valid?VariableSquid
Interesting development from @Wraithseekerr suggestion. I have now tried hiding the sprite when it's initial created in setupEatingAction. Then unhide when eat is called. The sprite is nowhere to be seen when eat is called, BUT you can still hear the sound FX...VariableSquid

1 Answers

0
votes

Found the problem. It was a logical issue. The SKSpriteNode was being added and the sound FX started playing, but in the next frame or so the sprite was being removed. As a result, it appeared that the sprite wasn't being displayed, but didn't have time to move into view. The sound FX continued to play, hence the confusion.