gpt4 book ai didi

swift - 两个 SpriteNode 之间的接触问题

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

我的 func 联系人出现问题,因为第二个联系人不起作用(与 Bonus 和 Vaisseau 联系),但另外两个联系人不起作用。

这是我的代码:

功能:

func didBeginContact(contact: SKPhysicsContact) {

let PremierBody : SKPhysicsBody = contact.bodyA
let SecondBody : SKPhysicsBody = contact.bodyB

if ((PremierBody.categoryBitMask == PhysicsCategories.Meteorites) && (SecondBody.categoryBitMask == PhysicsCategories.Meteorites)) {
contactEntreMeteorites(PremierBody.node as! SKSpriteNode, Meteorites2: SecondBody.node as! SKSpriteNode)
}
else if ((PremierBody.categoryBitMask == PhysicsCategories.Bonus) && (SecondBody.categoryBitMask == PhysicsCategories.Vaisseau) ||
(PremierBody.categoryBitMask == PhysicsCategories.Vaisseau) && (SecondBody.categoryBitMask == PhysicsCategories.Bonus)){
gameOver(PremierBody.node as! SKSpriteNode, Vaisseau: SecondBody.node as! SKSpriteNode)
print("CONTACT")
}
else if ((PremierBody.categoryBitMask == PhysicsCategories.Meteorites) && (SecondBody.categoryBitMask == PhysicsCategories.Vaisseau) ||
(PremierBody.categoryBitMask == PhysicsCategories.Vaisseau) && (SecondBody.categoryBitMask == PhysicsCategories.Meteorites)){
gameOver(PremierBody.node as! SKSpriteNode, Vaisseau: SecondBody.node as! SKSpriteNode)
print("Couco")
}
}

这是我的物理类别:

struct PhysicsCategories {
static let Meteorites : UInt32 = 1
static let Bonus : UInt32 = 2
static let Vaisseau : UInt32 = 5
}

这在我的 func DidMoveToView 中:

Vaisseau = SKSpriteNode(texture: Vaisseau1)
Vaisseau.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)
Vaisseau.physicsBody = SKPhysicsBody(rectangleOfSize: Vaisseau.size)
Vaisseau.physicsBody?.affectedByGravity = false
Vaisseau.physicsBody?.categoryBitMask = PhysicsCategories.Vaisseau
Vaisseau.physicsBody?.contactTestBitMask = PhysicsCategories.Bonus
Vaisseau.physicsBody?.contactTestBitMask = PhysicsCategories.Meteorites
Vaisseau.physicsBody?.dynamic = false
Vaisseau.setScale(0.08)
self.addChild(Vaisseau)

我在此函数中创建了我的 SpriteNode 奖励:

func ApparitionBonus() {

let BonusSprite = SKSpriteNode(imageNamed: "Bonus.png")
var BonusApparitionX = UInt32(self.frame.size.width)
var BonusApparitionY = UInt32(self.frame.size.height)

BonusApparitionX = arc4random() % BonusApparitionX
BonusApparitionY = arc4random() % BonusApparitionY

BonusSprite.position = CGPointMake(CGFloat(BonusApparitionX),CGFloat(BonusApparitionY))
BonusSprite.setScale(0.8)
BonusSprite.physicsBody = SKPhysicsBody(circleOfRadius: 20)
BonusSprite.physicsBody?.affectedByGravity = false
BonusSprite.physicsBody?.categoryBitMask = PhysicsCategories.Bonus
BonusSprite.physicsBody?.contactTestBitMask = PhysicsCategories.Vaisseau
BonusSprite.physicsBody?.dynamic = false
self.addChild(BonusSprite)

let RotationBonus = SKAction.rotateByAngle(CGFloat(M_PI), duration: 3)
let wait = SKAction.waitForDuration(3)
let actionFini = SKAction.removeFromParent()
BonusSprite.runAction(SKAction.sequence([RotationBonus, wait, actionFini]))
BonusSprite.runAction(SKAction.repeatActionForever(RotationBonus))
}

最佳答案

在您的didMoveToView中,当您调用时:

Vaisseau.physicsBody?.contactTestBitMask = PhysicsCategories.Bonus
Vaisseau.physicsBody?.contactTestBitMask = PhysicsCategories.Meteorites

您将 contactTestBitMask 设置为一个类别,然后再设置为另一个类别。您需要将它们按位或在一起。像这样的东西:

Vaisseau.physicsBody?.contactTestBitMask = PhysicsCategories.Bonus | PhysicsCategories.Meteorites

这将组合两个位掩码,使其能够联系BonusMeteorites

如果您不希望 Sprite 发生碰撞,请将 collisionBitMask 设置为 0x0(或者您想要表示 0 的任何方式):

Vaisseau.physicsBody?.collisionBitMask = 0x0

关于swift - 两个 SpriteNode 之间的接触问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33422280/

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