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

标签 ios swift skspritenode

我有一个名为 runner 的 SKSpriteNode,每当我触摸屏幕试图将它从一个地方拖到另一个地方时,runner 会走相反的路吗?例如,如果我试图向左拖动它,它就会向右移动,如果我试图向上拖动它,它就会向下移动。添加 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)
        }

    }

最佳答案

这段代码工作正常:

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

关于ios - 名为 runner 的 SKSpriteNode 以相反的方式移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30747297/

相关文章:

ios - 行数在部分

ios - 以编程方式创建的按钮在 iPhone (Swift 3) 上弹出

ios - 在swift中将容器 View 分成2个 Controller

swift - 在 swift 中子类化子类(skspritenode)

swift - 如何从 sks 文件加载节点,然后应用场景编辑器中定义的 Action

ios - 将 MKMapView 发送回 iOS

ios - 如何将颜色保存到 Realm 并在其他 View Controller 中显示颜色?

swift - 专门的通用协议(protocol)只能作为通用约束

swift - 无法将表达式的类型 '()' 转换为类型 'UINavigationController?'

swift : Transform SKSpriteNode