Does adding new action for the scene remove automatically the one before in SpriteKit?

本小妞迷上赌 提交于 2019-12-08 13:14:31

The only time a new action will interfere with any running actions is if both actions share the same key. If you do not assign a key, then every time you add an action, it gets added to the action pool and will run concurrently.

If you just give it a try, I'm sure you'll find the answer yourself.

But anyway, I tried it for you:

node.run(SKAction.sequence([SKAction.moveBy(x: 100, y: 0, duration: 3), SKAction.moveBy(x: 0, y: 100, duration: 3)]))
node.run(SKAction.rotate(byAngle: CGFloat(M_PI * 2), duration: 6))

And what I see is that the node both moves and rotates. So each subsequent action you tell a node to run will be run simultenuously.

Another way to run actions at the same time is to use SKAction.group.

I've follow your comment, seems you are in the situation of overlap of the actions.

When you have a node and you want to launch one or more action, especially a sequence of actions where your node are involved in movements, you should be sure that these actions are finished.

To do it, for example to self:

let seq = SKAction.sequence([action1,action2,..])
if self.action(forKey: "moveToRoof") == nil {
   self.run(seq, withKey:"moveToRoof")
}

You can also do:

let group1 = SKAction.group([action1, action2,..])
let group2 = SKAction.group([action1, action2,..])
let addNewNode = SKAction.run{
    self.addChild(node)
}
let seq = SKAction.sequence([action1, group1, action2, addNewNode, group2,..])
if self.action(forKey: "moveToGround") == nil {
       self.run(seq, withKey:"moveToGround")
}

In your case seems you want to add nodes to a node that following the position of his parent..

override func update(_ currentTime: TimeInterval) {
        if let child = myNode1, let parent = child.parent  { // if exist follow the parent position
            child.position = parent.position
        }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!