gpt4 book ai didi

ios - 当我按下运行程序时,我的游戏会加载但无法启动

转载 作者:可可西里 更新时间:2023-11-01 02:16:58 24 4
gpt4 key购买 nike

我正在学习本教程:https://www.raywenderlich.com/118225/introduction-sprite-kit-scene-editor

当我在 Xcode 中按下运行程序时,模拟器加载游戏并弹出显示游戏的第一帧,但它被卡住了。当我点击时,玩家 Sprite 应该移动到我点击的地方并且 AI Sprite 应该试图捕获玩家,但没有任何反应。点击不起作用,我试过让它静置一会儿,看看它是否没有完成加载或其他什么,但也没有用。

到目前为止,该程序的所有代码都在这里:

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

let playerSpeed: CGFloat = 150.0
let zombieSpeed: CGFloat = 75.0

var goal: SKSpriteNode?
var player: SKSpriteNode?
var zombies: [SKSpriteNode] = []

var lastTouch: CGPoint? = nil

override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
updateCamera()
}

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

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

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

private func handleTouches(touches: Set<UITouch>) {
for touch in touches {
let touchLocation = touch.locationInNode(self)
lastTouch = touchLocation
}
}

override func didSimulatePhysics() {
if let _ = player {
updatePlayer()
updateZombies()
}
}

private func shouldMove(currentPosition currentPosition: CGPoint, touchPosition: CGPoint) -> Bool {
return abs(currentPosition.x - touchPosition.x) > player!.frame.width / 2 ||
abs(currentPosition.y - touchPosition.y) > player!.frame.height/2
}

func updatePlayer() {
if let touch = lastTouch {
let currentPosition = player!.position
if shouldMove(currentPosition: currentPosition, touchPosition: touch) {

let angle = atan2(currentPosition.y - touch.y, currentPosition.x - touch.x) + CGFloat(M_PI)
let rotateAction = SKAction.rotateToAngle(angle + CGFloat(M_PI*0.5), duration: 0)

player!.runAction(rotateAction)

let velocotyX = playerSpeed * cos(angle)
let velocityY = playerSpeed * sin(angle)

let newVelocity = CGVector(dx: velocotyX, dy: velocityY)
player!.physicsBody!.velocity = newVelocity;
updateCamera()
} else {
player!.physicsBody!.resting = true
}
}
}

func updateCamera() {
if let camera = camera {
camera.position = CGPoint(x: player!.position.x, y: player!.position.y)
}
}

func updateZombies() {
let targetPosition = player!.position

for zombie in zombies {
let currentPosition = zombie.position

let angle = atan2(currentPosition.y - targetPosition.y, currentPosition.x - targetPosition.x) + CGFloat(M_PI)
let rotateAction = SKAction.rotateToAngle(angle + CGFloat(M_PI*0.5), duration: 0.0)
zombie.runAction(rotateAction)

let velocotyX = zombieSpeed * cos(angle)
let velocityY = zombieSpeed * sin(angle)

let newVelocity = CGVector(dx: velocotyX, dy: velocityY)
zombie.physicsBody!.velocity = newVelocity;
}
}

func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody
var secondBody: SKPhysicsBody

if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}

if firstBody.categoryBitMask == player?.physicsBody?.categoryBitMask && secondBody.categoryBitMask == zombies[0].physicsBody?.categoryBitMask {
gameOver(false)
} else if firstBody.categoryBitMask == player?.physicsBody?.categoryBitMask && secondBody.categoryBitMask == goal?.physicsBody?.categoryBitMask {
gameOver(true)
}

player = self.childNodeWithName("player") as? SKSpriteNode

for child in self.children {
if child.name == "zombie" {
if let child = child as? SKSpriteNode {
zombies.append(child)
}
}
}
goal = self.childNodeWithName("goal") as? SKSpriteNode
}

private func gameOver(didWin: Bool) {
print("- - - Game Ended - - -")
let menuScene = MenuScene(size: self.size)
menuScene.soundToPlay = didWin ? "fear_win.mp3" : "fear_lose.mp3"
let transition = SKTransition.flipVerticalWithDuration(1.0)
menuScene.scaleMode = SKSceneScaleMode.AspectFill
self.scene!.view?.presentScene(menuScene, transition: transition)
}
}

程序中完成的其余事情,例如添加 Sprite 等,都是在 GameScene.sks 中完成的,因此没有相关代码。

最佳答案

您的设置代码位置错误。将此从 didBeginContact 移至 didMoveToView:

player = self.childNodeWithName("player") as? SKSpriteNode
for child in self.children {
if child.name == "zombie" {
if let child = child as? SKSpriteNode {
zombies.append(child)
}
}
}
goal = self.childNodeWithName("goal") as? SKSpriteNode

至少这是我在将您的代码与教程中的示例项目进行比较时看到的差异。

关于ios - 当我按下运行程序时,我的游戏会加载但无法启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37027272/

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