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

孤街浪徒 提交于 2019-12-08 04:09:22

问题


I want to know if I made a sequence action and while this sequence is running a new action added to the scene , Does the new action stop the sequence ?

If yes , how i can make both of them working if the new action is added in swift ?


回答1:


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.




回答2:


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.




回答3:


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
        }
}


来源:https://stackoverflow.com/questions/41801148/does-adding-new-action-for-the-scene-remove-automatically-the-one-before-in-spri

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