简体   繁体   中英

Defining my spaceship movement in spritekit game in Swift

Hi I am new to Xcode and Swift, right now I am trying to design a game that involves a spaceship as player with alien spaceships. I ran into a little problem where I am trying to differentiate the movement of the spaceship from the firing of the spaceship.

Basically I used touchesBegan() function to run the function which my spaceship fires, and touchesMoved() function to move the spaceship's x-position.

These are the code:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    pShoot()
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {

    for touch in touches{

        let location = touch.location(in: self)
        spaceship.run(SKAction.moveTo(x: location.x, duration: 0.5))

    }
}

What I am trying to do is to differentiate the clicking or touching indicator and pressed and move indicator, in other words I dont want the spaceship to fire when I am pressed and move on the screen, and I do not want the spaceship to move when I am clicking constantly but at different position. ( touchesMoved() detects changes in touch positions so if I am clicking at different positions spaceship will move which I dont want)

I would like to know what would be the best way of implementing this, thank you.

您可以尝试检查用户按下的区域,然后根据触摸的原点决定是否移动宇宙飞船。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM