如何运行或之外的对象执行SKAction?(How to run or execute an SKA

2019-10-20 03:15发布

我有一个像下面的一些代码:

-(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];

}

现在我该怎样让跑出来,从侧面如说ballScaleMove。 在touchBegan处理器内部或在initWithSize ..等?

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

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


}

Answer 1:

您需要定义你的属性或高德作为全球。 在类的@interface定义它们

@interface
@property Class *myProperty;
@end

然后,你可以用self.myProperty或_myProperty访问它们(访问你的财产的隐性伊娃)

我希望它能帮助



Answer 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]; 
}


Answer 3:

我已经实现了一个Singleton对象返回我需要的所有SKActions。 这种方法的优点还重用SKActions



文章来源: How to run or execute an SKAction from outside of the object?