3

I have an SKSpriteNode that can be moved anywhere I am touching, and I want it to move where my finger is, just along a horizontal line so that is cannot move vertically.

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

    }

}

Here's my code, thanks in advance.

Nate N.
  • 35
  • 2

1 Answers1

1

Try this:

ship.position.x = location.x

Currently, you are setting the ship's location to the location of the tap. If you want a consistent y position, only set the x.

Pranav Wadhwa
  • 7,666
  • 6
  • 39
  • 61
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/13108728) – Johnathon Sullinger Jul 24 '16 at 22:54
  • This does provide an answer. It shows how to solve the OP's problem. – Pranav Wadhwa Jul 24 '16 at 23:22
  • You should explain why this code is considered an answer. That the ships position needs to be updated by assigning it the values from the current touch location etc. Just posting source code without any narrative is frowned on. – Johnathon Sullinger Jul 24 '16 at 23:23
  • 1
    Thanks for the tips. I will make sure to add that on my answers. – Pranav Wadhwa Jul 24 '16 at 23:26