【问题标题】:How to move an SKSpriteNode along a horizontal line如何沿水平线移动 SKSpriteNode
【发布时间】:2016-07-24 21:13:52
【问题描述】:

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

    }

}

这是我的代码,在此先感谢。

【问题讨论】:

    标签: swift skspritenode touchesmoved


    【解决方案1】:

    试试这个:

    ship.position.x = location.x
    

    目前,您正在将船的位置设置为水龙头的位置。如果你想要一个一致的 y 位置,只设置 x。

    【讨论】:

    • 这没有提供问题的答案。要批评或要求作者澄清,请在他们的帖子下方留下评论。 - From Review
    • 这确实提供了答案。它展示了如何解决 OP 的问题。
    • 你应该解释为什么这个代码被认为是一个答案。需要通过为其分配当前触摸位置的值等来更新船舶位置。仅发布没有任何叙述的源代码是不受欢迎的。
    • 感谢您的提示。我会确保将其添加到我的答案中。
    猜你喜欢
    • 2016-06-26
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多