gpt4 book ai didi

ios - 无法移动 Sprite

转载 作者:行者123 更新时间:2023-11-30 13:27:52 26 4
gpt4 key购买 nike

即使在 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") 作为?玩家在 GameplayScene.swift 中执行 didMove

override func didMove(to view: SKView) {
print("didmove")

center = (self.scene?.size.width)! / (self.scene?.size.height)!
player = self.childNode(withName: "Player") as? Player
}

关于ios - 无法移动 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36928440/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com