[英]Unable to move Sprite
即使在GameScene中的Sprite上設置了類之后,我也無法移動Sprite。 下面是我的GameplayScene類代碼。 我沒有包括Player類,因為代碼確實很基礎。 (只是一個movePlayer bool函數和一個基於x位置移動的if語句)
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 = self.childNode(withName:“ Player”)添加為? 播放功能的玩家didMove in GameplayScene.swift
override func didMove(to view: SKView) {
print("didmove")
center = (self.scene?.size.width)! / (self.scene?.size.height)!
player = self.childNode(withName: "Player") as? Player
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.