S
S
Stanislav Korolevskiy2015-12-20 19:04:04
Swift
Stanislav Korolevskiy, 2015-12-20 19:04:04

How to move an object only when you click on it?

I feel that I am already close to the goal! ) The object reacts only on a click on itself, but also moves only along its border .... How to fix it?

import SpriteKit

class GameScene: SKScene {
    
    //создаем свойство
    var figureUser = SKSpriteNode!()
    
    override func didMoveToView(view: SKView) {
        
        //инициализируем свойство
        figureUser = SKSpriteNode(imageNamed: "circle")
        
        //определяем позицию square на экране
        figureUser.position = CGPoint(x: 25, y: 25)
        
        //даем имя
        figureUser.name = "circle"
        
        figureUser.userInteractionEnabled = false
        
        //добавляем square на экран
        addChild(figureUser)
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        
        let touch = touches.first
        let touchLocation = touch!.locationInNode(self)
        let node = self.nodeAtPoint(touchLocation)
        
        if (node.name == "circle") {
            
            let moveAction = SKAction.moveTo(touchLocation, duration: 0)
            
            figureUser.runAction(moveAction)
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Korolevskiy, 2015-12-21
@korolevsky_s

He asked and answered himself. ) Instead of touchesBegan, you need to use touchesMoved.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question