无法在SpriteKit中向左或向右移动精灵

时间:2016-04-29 17:40:28

标签: ios swift sprite-kit

https://github.com/bmh3110/JackTheGiantGame

触摸屏幕时,我无法向左或向右移动精灵。我刚刚开始这个游戏,我甚至无法让它工作。我已经有几天这个问题而且它真的很令人沮丧。

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 
        }
    }
}

1 个答案:

答案 0 :(得分:0)

touchesEnded功能集canMove = false