gpt4 book ai didi

xcode - 船舶与硬币相撞无法正常工作

转载 作者:行者123 更新时间:2023-11-30 10:18:13 25 4
gpt4 key购买 nike

我创建了一个游戏,玩家必须收集硬币。现在,由于某种原因,当屏幕上有很多硬币可供抓取时,当船与硬币相撞时,它将直接穿过与其碰撞的当前硬币,并移除添加到屏幕上的最后一个硬币。我怎样才能使发生碰撞的实际硬币从屏幕上移除?

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 & UInt32(shipCategory)) != 0 && (secondBody.categoryBitMask & UInt32(obstacleCategory)) != 0 {
ship.removeFromParent()
let reveal = SKTransition.flipHorizontalWithDuration(0.5)
let scene = GameOverScene(size: self.size)
self.view?.presentScene(scene, transition: reveal)
}

if (firstBody.categoryBitMask & UInt32(shipCategory)) != 0 && (secondBody.categoryBitMask & UInt32(coinCategory)) != 0 {
coin.removeFromParent()
playerScore = playerScore + 1
playerScoreUpdate()
}


override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
if currentTime - self.lastMissileAdded > 1 {
self.lastMissileAdded = currentTime + 1
self.addMissile()
}

// Current time + 6 takes longer to respawn coins
if currentTime - self.lastCoinAdded > 1 {
self.lastCoinAdded = currentTime + 0
self.addCoin()
}

// Current time + 6 takes longer to respawn coins
//if currentTime - self.lastDiamondAdded > 1 {
// self.lastDiamondAdded = currentTime + 1
//self.addDiamond()
//}

self.moveBackground()
self.moveObstacle()
self.moveCoin()
//self.moveDiamond()

}

最佳答案

要移除与 ship 碰撞的 coin,请更改 didBeginContact 函数中第二个条件中的代码。您必须删除属于 secondBody 的节点。在您的代码中,您只需删除一个coin 全局变量。这就是为什么当场景中有其他硬币时它不起作用。

if (firstBody.categoryBitMask & UInt32(shipCategory)) != 0 && (secondBody.categoryBitMask & UInt32(coinCategory)) != 0 {
secondBody.node?.removeFromParent() // Changed line.
playerScore = playerScore + 1
playerScoreUpdate()
}

关于xcode - 船舶与硬币相撞无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28976623/

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