0
即使在GameScene中的Sprite上设置了类后,我仍无法移动该Sprite。以下是我的GameplayScene类的代码。我没有包含我的Player类,因为代码非常基本。 (只是用if语句movePlayer布尔函数招式基于X位置)无法移动Sprite
import SpriteKit
class GameplayScene: SKScene {
var player: Player? // created new varible player for the scene, inherited the player class
var canMove = false // created new varible for letting player move; false by default
var moveLeft = false // created new varible to move player left; false by default
var center: CGFloat? // created new varible to set center of screen
override func didMoveToView(view: SKView) {
center = (self.scene?.size.width)!/(self.scene?.size.height)! //calculates center of screen
player.self?.childNodeWithName("Player") as? Player! // adds node "Player" within Player class(Player!)
}
override func update(currentTime: NSTimeInterval) {
managePlayer() //constantly checks if player can/is moving
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self) // gets location inside scene
if location.x > center { // if statement to check if user to taping screen on the right
moveLeft = false //moves player right
} else {
moveLeft = true
}
}
canMove = true
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
canMove = false //says if player is not touching screen, the player will not move
}
func managePlayer() {
if canMove {
player?.movePlayer(moveLeft) //takes the Player varible(which has the class), then grabs the move player func, then checks argument moveLeft
}
}
}
通过调试器运行,它表示player = – Doba