名为 runner 的 SKSpriteNode 以相反的方式移动
The SKSpriteNode called runner is moving in the opposite way
我有一个名为 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)
}
}
我有一个名为 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)
}
}