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

荒凉一梦 提交于 2019-12-24 08:58:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!