如何沿水平线移动 SKSpriteNode
How to move an SKSpriteNode along a horizontal line
我有一个 SKSpriteNode 可以移动到我触摸的任何地方,我希望它移动到我手指所在的位置,只是沿着水平线移动,这样就不能垂直移动。
class GameScene: SKScene {
var ship = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let shipTexture = SKTexture(imageNamed: "Evaders Art/EvadersShipVert2.png")
ship = SKSpriteNode(texture: shipTexture)
ship.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame) - 250)
ship.size = CGSize(width: 90, height: 115)
shipTexture.filteringMode = SKTextureFilteringMode.Nearest
ship.zPosition = 2
self.addChild(ship)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
var nodeTouched = SKNode()
var currentNodeTouched = SKNode()
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
nodeTouched = self.nodeAtPoint(location)
ship.position = location
}
}
这是我的代码,提前致谢。
试试这个:
ship.position.x = location.x
当前,您正在将船的位置设置为水龙头的位置。如果你想要一个一致的 y 位置,只设置 x.
我有一个 SKSpriteNode 可以移动到我触摸的任何地方,我希望它移动到我手指所在的位置,只是沿着水平线移动,这样就不能垂直移动。
class GameScene: SKScene {
var ship = SKSpriteNode()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
let shipTexture = SKTexture(imageNamed: "Evaders Art/EvadersShipVert2.png")
ship = SKSpriteNode(texture: shipTexture)
ship.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame) - 250)
ship.size = CGSize(width: 90, height: 115)
shipTexture.filteringMode = SKTextureFilteringMode.Nearest
ship.zPosition = 2
self.addChild(ship)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
var nodeTouched = SKNode()
var currentNodeTouched = SKNode()
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
nodeTouched = self.nodeAtPoint(location)
ship.position = location
}
}
这是我的代码,提前致谢。
试试这个:
ship.position.x = location.x
当前,您正在将船的位置设置为水龙头的位置。如果你想要一个一致的 y 位置,只设置 x.