问题
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';
}
回答1:
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
回答2:
-(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];
}
回答3:
I've implemented a Singleton object which returns all SKActions I need. This approach has also the advantage to reuse the SKActions
来源:https://stackoverflow.com/questions/23576798/how-to-run-or-execute-an-skaction-from-outside-of-the-object