2016-06-30 65 views
1

我在Swift和SpriteKit中创建了一个Side Scroller类似马里奥的游戏。我目前正在移动播放器,但你必须保持点击,然后移动。我所希望的是,如果你坚持它,它会移动,你不必快速点击屏幕。先谢谢你 !!!!连续触摸/移动Swift和SpriteKit

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 


    for touch: AnyObject in touches { 
     let location = touch.locationInNode(self) 

        if location.y > 400{ 

      move = true 

     }else{ 


      if location.x < CGRectGetMidX(self.frame){ 
       player.physicsBody?.applyImpulse(CGVector(dx: -30, dy: 0)) 

       //tap somewhere above this to make character jump 
       if(location.y > 250) { 
        player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200)) 
       } 

      } else if location.x > CGRectGetMidX(self.frame){ 
       player.physicsBody?.applyImpulse(CGVector(dx: 30, dy: 0)) 

       //tap somewhere above this to make character jump 

      } 
     } 

    } 

    } 



override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 


    move = false 

} 
+0

看起来像设置一次移动,只移动一次你的对象?只要用户没有举起手指,你想不断调用它吗? – Shripada

+0

是的像在JavaScript中有一个名为** mouseIsPressed **的布尔值,这基本上是我需要的。 – ScarletStreak

回答

0

就个人而言,我会创建一个具有更新功能的控制器类,你轮询您的场景更新以确定按钮状态是否为开启或关闭状态(触摸开始使按钮处于关闭状态,触摸结束处于关闭状态,更新轮询状态并根据状态执行操作)但是在你的情况下,为了保持简单而不改变很多代码,我只需要使用SKAction.moveBy

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 


    for touch: AnyObject in touches { 
     let location = touch.locationInNode(self) 

     if location.y > 400{ 

      move = true 

     }else{ 
      //tap somewhere above this to make character jump 
      if(location.y > 250) { 
       player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200)) 
      } 
      else 
      { 
       let direction = (location.x > CGRectGetMidX(self.frame)) ? 30 : -30 
       let move = SKAction.moveBy(x:direction,y:0,duration:0) 
       let rep = SKAction.repeatActionForever(move) 
       player.runAction(rep,forKey:"Moving") 
      } 
     } 
    } 
} 



override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 

    player.removeActionForKey("Moving") 
    move = false 

} 
+0

请注意,您需要处理多个触摸,因此您可能还想要删除touchbegin中的操作 – Knight0fDragon

0

我真的不很了解你怎么想,但我尝试写一些代码:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     for touch: AnyObject in touches { 
      let location = touch.locationInNode(self) 
      if location.y > 400{ 
       move = true 
      }else{ 
       if location.x < CGRectGetMidX(self.frame){ 
        movePlayer(-30,dy:0) 

        //tap somewhere above this to make character jump 
        if(location.y > 250) { 
         player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200)) 
        } 
       } else if location.x > CGRectGetMidX(self.frame){ 
        movePlayer(30,dy:0) 
        //tap somewhere above this to make character jump 
       } 
      } 
     } 
    } 
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     for touch: AnyObject in touches { 
      let location = touch.locationInNode(self) 
      if location.y > 400{ 
       move = false 
      }else { 
       if location.x < CGRectGetMidX(self.frame) || if location.x > CGRectGetMidX(self.frame) { 
        movePlayer(0,dy:0) 
       } 
      } 
     } 
    } 

    func movePlayer(dx:CGFloat,dy:CGFloat) { 
     if (player.physicsBody.resting) { 
      player.physicsBody?.applyImpulse(CGVector(dx, dy: dy)) 
     } 
    }