按住触摸移动SKSpriteNode

时间:2017-11-08 22:54:56

标签: swift xcode sprite-kit mobile-development

我在GameScene中获得了英雄的SKSpriteNode。我实现了两个按钮(向上和向下)。我需要我的英雄的SKSpriteNode通过按钮触摸上下移动。但我只看到touchesMoved和touchesEnded方法。我需要一些关于touchesBegan的东西(当我点击它时感觉按钮点击。)

1 个答案:

答案 0 :(得分:0)

您创建某种重复步骤,从touchesBegan开始,到touchesEnded结束,touchesCancelled或触摸从节点移开时 选中touchesMoved

override func touchesBegan(...)
{
   ...
   let moveAction = SKAction.repeatForever(SKAction.move(by:CGPoint(x:x,y:y)))
   node.run(moveAction,forKey:"moving")
   ...
}
override func touchesMoved(...)
{
   ...
   //add a check to see if if touch.position is not inside the button
   if (...)
   {
       node.removeAction(withKey:"moving")
   } 
   ...
}
override func touchesEnded(...)
{
   ...
   node.removeAction(withKey:"moving")
   ...
}
override func touchesCancelled(...)
{
   ...
   node.removeAction(withKey:"moving")
   ...
}