The SKSpriteNode called runner is moving in the opposite way

58 views Asked by At

I have an SKSpriteNode called runner and whenever I touch the screen to try to drag it from one place to another, runner goes the opposite way? For example, if I try to drag it left, it goes right, if I try to drag it up then it goes down. Adding TouchesEnded did not help. Can anyone look at my code and tell me why this is happening please?

    import UIKit
    import Foundation
    import SpriteKit

    class Maze: SKScene {

        let runner = SKSpriteNode(imageNamed: "Spaceship")

        override func didMoveToView(view: SKView)
        {
            self.createRunner()
        }


        override func touchesBegan(touches: NSSet, withEvent event: UIEvent)
        {
            let touch = touches.anyObject()! as UITouch
            let location = touch.locationInView(self.view)
            runner.position = location
        }

        override func touchesMoved(touches: NSSet, withEvent event: UIEvent)
        {
            let touch = touches.anyObject()! as UITouch
            let location = touch.locationInView(self.view)
            runner.position = location
        }

        func createRunner()
        {
            runner.setScale(0.50)
            runner.position = CGPointMake(0, 0)
            runner.name = "RunnerNode"
            self.addChild(runner)
        }

    }
1

There are 1 answers

0
Dharmesh Kheni On BEST ANSWER

This code is working fine:

    import SpriteKit

    class GameScene: SKScene {

    let runner = SKSpriteNode(imageNamed: "Spaceship")

    override func didMoveToView(view: SKView)
    {
        self.createRunner()
    }


    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        var touch = touches.first as! UITouch
        var touchLocation = touch.locationInNode(self)
        runner.position = touchLocation
    }

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        var touch = touches.first as! UITouch
        var touchLocation = touch.locationInNode(self)
        runner.position = touchLocation
    }

    func createRunner()
    {
        runner.setScale(0.50)
        runner.position = CGPointMake(0, 0)
        runner.name = "RunnerNode"
        self.addChild(runner)
    }
}