2016-01-28 24 views
2

我有一辆车是SKShapeNode。它正在移动。当我触摸它时,我想停止它1秒钟,然后回到运动。如何停止节点并让它在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) 

回答

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

所有事情都有很多评论。所以基本上,这里发生了什么是:

  • 节点移动通过与“移动”相关的动作完成的关键
  • 当用户触摸节点,通过“移动”键暂停相关的动作;当发生这种情况时,称为“等待”的另一动作“并行”开始
  • “等待”动作等待一秒,并取消暂停“动作”动作;因此汽车继续运动

当前,当汽车被触摸时,“移动”动作暂停...因此,如果再次触摸汽车,它会保持附加秒位置(新的“等待”动作将覆盖先前的“等待”动作)。如果你不希望这种行为,你可以检查汽车是否已经在等待,就像这样:

if node.actionForKey("waiting") == nil {/*handle touch*/} 

或者你可以检查是否有车通过检查相关的动作的速度属性的值停止“移动”键。

相关问题