gpt4 book ai didi

swift - Easy SpriteKit 接触检测(太空射击游戏)无法正常工作

转载 作者:行者123 更新时间:2023-11-28 06:45:17 25 4
gpt4 key购买 nike

我正在尝试制作一款简单的太空射击游戏。接触应该发生在鱼雷和外星人之间,或者航天飞机和外星人之间。问题是第二次接触(航天飞机与外星人)只发生在第一种接触(鱼雷与外星人)发生之后,而且它们并不总是精确的。这是在类外创建的结构

struct PhysicsCategory {
static let alien : UInt32 = 1
static let torpedo : UInt32 = 2
static let shuttle : UInt32 = 3 }

类车:

shuttle.physicsBody = SKPhysicsBody(rectangleOfSize: shuttle.size)
shuttle.physicsBody?.categoryBitMask = PhysicsCategory.shuttle
shuttle.physicsBody?.contactTestBitMask = PhysicsCategory.alien
shuttle.physicsBody?.dynamic = false
shuttle.physicsBody?.affectedByGravity = false

鱼雷:

torpedo.physicsBody = SKPhysicsBody(rectangleOfSize: torpedo.size)
torpedo.physicsBody?.categoryBitMask = PhysicsCategory.torpedo
torpedo.physicsBody?.contactTestBitMask = PhysicsCategory.alien
torpedo.physicsBody?.affectedByGravity = false
torpedo.physicsBody?.dynamic = false

外星人:

alien.physicsBody = SKPhysicsBody(rectangleOfSize: torpedo.size)
alien.physicsBody?.categoryBitMask = PhysicsCategory.alien
alien.physicsBody?.contactTestBitMask = PhysicsCategory.torpedo
alien.physicsBody?.affectedByGravity = false
alien.physicsBody?.dynamic = true

最后,这是我的联系方式:

    func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB

if ((firstBody.categoryBitMask == PhysicsCategory.alien) && (secondBody.categoryBitMask == PhysicsCategory.torpedo)) ||
((firstBody.categoryBitMask == PhysicsCategory.torpedo) && (secondBody.categoryBitMask == PhysicsCategory.alien)) {
self.contactWithTorpedo(firstBody.node as! SKSpriteNode, torpedo: secondBody.node as! SKSpriteNode)
} else if ((firstBody.categoryBitMask == PhysicsCategory.shuttle) && (secondBody.categoryBitMask == PhysicsCategory.alien)) {
self.contactWithShuttle(firstBody.node as! SKSpriteNode, shuttle: secondBody.node as! SKSpriteNode)
}
}


func contactWithTorpedo (alien: SKSpriteNode, torpedo : SKSpriteNode) {
alien.removeFromParent()
torpedo.removeFromParent()
score++
scoreLabel.text = "score: " + "\(score)"
}

func contactWithShuttle (alien:SKSpriteNode, shuttle:SKSpriteNode) {
alien.removeFromParent()
shuttle.removeFromParent()

self.view?.presentScene(EndScene())

}

我不太确定问题出在哪里,而且我看到一些教程也有同样的问题。顺便说一句,我不知道它是否相关,但这不是 iOS 游戏而是 OSX 游戏。提前致谢!

最佳答案

您可能会发现按如下方式重组您的 didBeginContact 不会那么困惑,因为这避免了 firstBody/secondbody 的东西和复杂的 if...then 条件来查看什么联系过什么:

func didBeginContact(contact: SKPhysicsContact) {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

switch contactMask {
case PhysicsCategory.alien | PhysicsCategory.torpedo:
// alien and torpedo have contacted
contact.bodyA.removeFromParent()
contact.bodyB.removeFromParent()
score += 1
scoreLabel.text = "score: " + "\(score)"

case PhysicsCategory.alien | PhysicsCategory.shuttle:
// alien and shuttle have contacted
contact.bodyA.removeFromParent()
contact.bodyB.removeFromParent()

self.view?.presentScene(EndScene())

default :
//Some other contact has occurred
print("Some other contact")
}
}

你可以添加尽可能多的 PhysicsCategory.enemy | PhysicsCategory.player 案例,因为您需要在游戏中采取行动的所有联系人。为每个潜在联系人单独编码,您不会陷入如果……那么……否则的困境。

如果您只需要引用接触中涉及的节点之一(例如,在敌人击中玩家后移除玩家),您可以这样做:

let playerNode = contact.bodyA.categoryBitMask == PhysicsCategory.player ? contact.bodyA.node! : contact.bodyB.node!
playernode.removefromParent

关于swift - Easy SpriteKit 接触检测(太空射击游戏)无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36656396/

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