简体   繁体   中英

Unable to move sprite left or right in SpriteKit

https://github.com/bmh3110/JackTheGiantGame

I'm unable to move my sprite left or right when the screen is touched. I'm just starting this game and I can't even get it to work. I've had this problem for a few days and it's getting really frustrating.

GameplayScene.swift

import SpriteKit

class GameplayScene: SKScene {
    var player : Player?
    var canMove = false
    var moveLeft = false
    var center : CGFloat?

    override func didMoveToView(view: SKView) {
        center = (self.scene?.size.width)! / (self.scene?.size.height)!
        player = self.childNodeWithName("Player") as? Player!
    }

    override func update(currentTime: NSTimeInterval) {
        managePlayer()
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch in touches {
            let location = touch.locationInNode(self)

            if location.x > center {
                moveLeft = false
            } else {
                moveLeft = true
            }
        }

        canMove = true
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
    }

    func managePlayer() {
        if canMove {
            player?.movePlayer(moveLeft)
        }
    }
}

Player.swift

import SpriteKit

class Player: SKSpriteNode {
    func movePlayer(moveLeft: Bool) {
        if moveLeft {
            self.position.x = self.position.x - 7
        } else {
            self.position.x = self.position.x + 7 
        }
    }
}

touchesEnded函数中设置canMove = false

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