名为runner的SKSpriteNode以相反的方式移动

时间:2015-06-10 04:47:04

标签: ios swift skspritenode

我有一个名为runner的SKSpriteNode,每当我触摸屏幕试图将它从一个地方拖到另一个地方时,跑步者会采取相反的方式吗?例如,如果我尝试将其向左拖动,则向右移动,如果我尝试将其向上拖动则向下拖动。添加TouchesEnded没有帮助。任何人都可以查看我的代码并告诉我为什么会这样吗?

    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 个答案:

答案 0 :(得分:0)

此代码工作正常:

    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)
    }
}