如何停止节点而不是让它在 1 秒后再次移动?
How to stop a node and than get it to move again after 1 second?
我有一辆车是 SKShapeNode。它在移动。当我触摸它时,我想停止它1秒,然后再回到运动。
我有这个代码...但它只是停止,a3
永远不会到达,汽车不会再次启动
let a1 = SKAction.speedTo(0.0, duration: 0.0)
let a2 = SKAction.waitForDuration(0.5)
let a3 = SKAction.speedTo(1.0, duration: 0.0)
下面是一个例子,说明如何将节点从A点移动到B点,并在触摸时停止一秒钟。
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
//Create a car
let car = SKSpriteNode(color: UIColor.purpleColor(), size: CGSize(width: 40, height: 40))
car.name = "car"
car.zPosition = 1
//Start - left edge of the screen
car.position = CGPoint(x: CGRectGetMinX(frame), y:CGRectGetMidY(frame))
//End = right edge of the screen
let endPoint = CGPoint(x: CGRectGetMaxX(frame), y:CGRectGetMidY(frame))
let move = SKAction.moveTo(endPoint, duration: 10)
car.runAction(move, withKey: "moving")
addChild(car)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
if let location = touch?.locationInNode(self){
//Get the node
let node = nodeAtPoint(location)
//Check if it's a car
if node.name == "car" {
//See if car is moving
if node.actionForKey("moving") != nil{
//Get the action
let movingAction = node.actionForKey("moving")
//Pause the action (movement)
movingAction?.speed = 0.0
let wait = SKAction.waitForDuration(3)
let block = SKAction.runBlock({
//Unpause the action
movingAction?.speed = 1.0
})
let sequence = SKAction.sequence([wait, block ])
node.runAction(sequence, withKey: "waiting")
}
}
}
}
}
几乎所有的东西都有评论。所以基本上,这里发生的是:
- 节点移动是通过与 "moving" 键关联的动作完成的
- 当用户触摸节点时,"moving"键关联的动作暂停;发生这种情况时,将启动另一个名为 "waiting" 的操作 "in parallel"
- "waiting" 动作等待一秒钟,并取消暂停 "moving" 动作;因此汽车继续运动
目前,当汽车被触摸时,"moving" 动作暂停...所以如果你再次触摸汽车,它会在原处多停留一秒(新的 "waiting" 动作将覆盖之前的 "waiting" 操作)。如果你不想要这种行为,你可以检查汽车是否已经在等待,像这样:
if node.actionForKey("waiting") == nil {/*handle touch*/}
或者您可以通过检查 "moving" 键关联的动作的速度 属性 值来检查汽车是否已停止。
我有一辆车是 SKShapeNode。它在移动。当我触摸它时,我想停止它1秒,然后再回到运动。
我有这个代码...但它只是停止,a3
永远不会到达,汽车不会再次启动
let a1 = SKAction.speedTo(0.0, duration: 0.0)
let a2 = SKAction.waitForDuration(0.5)
let a3 = SKAction.speedTo(1.0, duration: 0.0)
下面是一个例子,说明如何将节点从A点移动到B点,并在触摸时停止一秒钟。
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
//Create a car
let car = SKSpriteNode(color: UIColor.purpleColor(), size: CGSize(width: 40, height: 40))
car.name = "car"
car.zPosition = 1
//Start - left edge of the screen
car.position = CGPoint(x: CGRectGetMinX(frame), y:CGRectGetMidY(frame))
//End = right edge of the screen
let endPoint = CGPoint(x: CGRectGetMaxX(frame), y:CGRectGetMidY(frame))
let move = SKAction.moveTo(endPoint, duration: 10)
car.runAction(move, withKey: "moving")
addChild(car)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
if let location = touch?.locationInNode(self){
//Get the node
let node = nodeAtPoint(location)
//Check if it's a car
if node.name == "car" {
//See if car is moving
if node.actionForKey("moving") != nil{
//Get the action
let movingAction = node.actionForKey("moving")
//Pause the action (movement)
movingAction?.speed = 0.0
let wait = SKAction.waitForDuration(3)
let block = SKAction.runBlock({
//Unpause the action
movingAction?.speed = 1.0
})
let sequence = SKAction.sequence([wait, block ])
node.runAction(sequence, withKey: "waiting")
}
}
}
}
}
几乎所有的东西都有评论。所以基本上,这里发生的是:
- 节点移动是通过与 "moving" 键关联的动作完成的
- 当用户触摸节点时,"moving"键关联的动作暂停;发生这种情况时,将启动另一个名为 "waiting" 的操作 "in parallel"
- "waiting" 动作等待一秒钟,并取消暂停 "moving" 动作;因此汽车继续运动
目前,当汽车被触摸时,"moving" 动作暂停...所以如果你再次触摸汽车,它会在原处多停留一秒(新的 "waiting" 动作将覆盖之前的 "waiting" 操作)。如果你不想要这种行为,你可以检查汽车是否已经在等待,像这样:
if node.actionForKey("waiting") == nil {/*handle touch*/}
或者您可以通过检查 "moving" 键关联的动作的速度 属性 值来检查汽车是否已停止。