Question

I have some code like following:

-(void) createBall {

    _ballSprite = [SKSpriteNode spriteNodeWithImageNamed:@"ball0001"];
    _ballSprite.position = CGPointMake(firstNode.position.x, firstNode.position.y);
     SKAction *ballScale =[SKAction scaleXTo:-0.5 duration:0];
     SKAction *ballMove = [SKAction moveByX:0 y:-300 duration:0];
     SKAction *ballMoveScale = [SKAction sequence:@[ballScale,ballMove];
     SKAction *ballScaleMove = [SKAction sequence:@[ballMove,ballScale]; //Unused variable
     [_ballSprite runAction:ballMoveScale];
     [self addChild:_ballSprite];

}

Now how do I run let say ballScaleMove from out side eg. inside the touchBegan handler or at initWithSize ..etc.?

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

///here maybe
[_ballSprite runAction:ballScaleMove];  // Error undeclare identifier 'ballScaleMove';


}
Was it helpful?

Solution 2

You need to define your properties or iVars as global. define them in the @interface of your class

@interface
@property Class *myProperty;
@end

Then you can access them with self.myProperty or _myProperty (accessing the implicit iVar for your property)

I hope it helps

OTHER TIPS

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
     SKAction *ballScale =[SKAction scaleXTo:-0.5 duration:0];
     SKAction *ballMove = [SKAction moveByX:0 y:-300 duration:0];
     SKAction *ballMoveScale = [SKAction sequence:@[ballScale,ballMove];
    [_ballSprite runAction:ballScaleMove]; 
}

I've implemented a Singleton object which returns all SKActions I need. This approach has also the advantage to reuse the SKActions

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top