Search code examples
objective-cios7sprite-kitskaction

How to run or execute an SKAction from outside of the object?


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';


}

Solution

  • 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