Sprite kit 随机化动作
我正试着在屏幕上从左向右移动一个精灵。精灵应从屏幕右侧的随机y位置开始。使用repeatActionForever和带有随机Y数的整数,我希望精灵从不同的Y位置开始重复动作。除了在更新方法中加入随机整数外,还有什么想法可以实现这一点吗?它是否可以通过行动实现 我在精灵上使用此方法:Sprite kit 随机化动作,sprite-kit,skaction,Sprite Kit,Skaction,我正试着在屏幕上从左向右移动一个精灵。精灵应从屏幕右侧的随机y位置开始。使用repeatActionForever和带有随机Y数的整数,我希望精灵从不同的Y位置开始重复动作。除了在更新方法中加入随机整数外,还有什么想法可以实现这一点吗?它是否可以通过行动实现 我在精灵上使用此方法: int randomY = (arc4random()%121; SKAction *pos = [SKAction moveTo:CGPointMake((STAGESIZE.width+(sel
int randomY = (arc4random()%121;
SKAction *pos = [SKAction moveTo:CGPointMake((STAGESIZE.width+(self.size.width/2)),randomY) duration:0];
SKAction *move = [SKAction moveToX:0-self.size.width/2 duration:3];
SKAction *wait = [SKAction waitForDuration:1 withRange:5];
SKAction *sequence = [SKAction sequence:@[pos,move,wait]];
SKAction *repeater = [SKAction repeatActionForever:sequence];
[self runAction:repeater];
一旦定义并运行了标准操作,就无法将其随机化。 但有一种变通方法可以使用[customActionWithDuration]实现预期效果
每次运行动作时,随机性都会随机化,位置也会随之设置。您可以将生成随机数的代码放在块中,这样它就包含在动作中。
SKAction* randomPositionAction = [SKAction customActionWithDuration:0 actionBlock:^(SKNode *node,CGFloat elapsedTime){
int randomY = arc4random_uniform(121);
//Set position instead of running action with duration 0
[node setPosition:CGPointMake((STAGESIZE.width+(self.size.width/2)),randomY)];
}];