Вопрос:

Move SKSpriteNode by button touch

swift xcode sprite-kit mobile-development

186 просмотра

1 ответ

14 Репутация автора

I got SKSpriteNode of hero in my GameScene. I implement two buttons (up and down). I need that SKSpriteNode of my hero is moving up and down by buttons touches. But i only see touchesMoved and touchesEnded methods. I need something about touchesBegan (that feel the button tap while i tap it).

Автор: Игорь Иванов Источник Размещён: 08.11.2017 10:54

Ответы (1)


0 плюса

14147 Репутация автора

Решение

You create some kind of repeating step that starts at touchesBegan and ends at touchesEnded, at touchesCancelled or when the touch is moved off the node by checking 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")
   ...
}
Автор: Knight0fDragon Размещён: 09.11.2017 02:15
Вопросы из категории :
32x32