我有一个名为 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/