ios - 在游戏过程中修改 SKActions

标签 ios objective-c sprite-kit

我正在开发一款游戏,其中所有的敌人都是各自类别中的 SKNode。在我的 SKScene 中,我通过分配生物并调用特定的生成方法来生成它们。

然而,当它们被生成时,每个生物都被定义了一组它们在其生命周期内运行的 Action 。一个针对特定生物的例子是:

    SKAction *moveLeft = [SKAction moveToX:0 - (fragment.size.width/2) + (width / 2) duration:1.0];
    SKAction *moveRight = [SKAction moveToX:(fragment.size.width / 2) - (width / 2) duration:1.0];

    SKAction *sequence = [SKAction sequence:@[moveLeft, moveRight]];
    SKAction *bounceOnWalls = [SKAction repeatActionForever:sequence];

    [enemy runAction:bounceOnWalls];

所以,回到我的问题。创建特定的 SKAction 后如何对其进行修改?我想改变所有具有此 SKAction 的敌人的 moveLeft 的速度。

最佳答案

您可以使用键运行操作:

SKAction *moveLeft = [SKAction moveToX:0 - (fragment.size.width/2) + (width / 2) duration:1.0];
    SKAction *moveRight = [SKAction moveToX:(fragment.size.width / 2) - (width / 2) duration:1.0];

    SKAction *sequence = [SKAction sequence:@[moveLeft, moveRight]];
    SKAction *bounceOnWalls = [SKAction repeatActionForever:sequence];

    [enemy runAction:bounceOnWalls withKey:@"moving"]; //Run action with key

当您需要更改运行该操作的所有节点上的速度时,您可以使用 enumerateChildNodesWithName 方法。像这样:

[parentNode enumerateChildNodesWithName:name usingBlock:^(SKNode *node, BOOL *stop){

        if([node actionForKey:@"moving"]){


            SKAction* action = [node actionForKey:@"moving"];

           action.speed = 1.5f;

        }

    }];

您可能可以动态更改 Action 的持续时间并以这种方式影响移动节点的速度,但我认为直接更改 Action 的速度是更好的选择。查看此 example 中的两个答案关于如何动态更改持续时间参数。

关于ios - 在游戏过程中修改 SKActions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31471022/

相关文章:

ios - 如何在使用 Swinject 解析对象时每次返回相同的实例?

ios - Swift AVPlayer 无法播放 MP4 : Play Button With Line Through It

objective-c - 动态更新 UIAlertView 消息和换行符问题

swift - 如何在 Spritekit/Swift 3 中的图像上创建按钮?

swift SpriteKit : "MCTFruit does not have a member named append?

swift - 如何从点击中获取 SKSpriteNode 子类对象

ios - 如何在 iOS 中使用全局搜索编写正则表达式?

当我将 HTML 字符串(大量数据)加载到 WebView 时,iPhone 应用程序崩溃

objective-c - OSX/ cocoa : Listening for system wide mouse drag events

ios - 无法显示我的 iOS VoIP 应用程序的 onComingCall ViewController