gpt4 book ai didi

swift - SpriteNodes 之间没有碰撞检测

转载 作者:行者123 更新时间:2023-11-28 07:36:06 26 4
gpt4 key购买 nike

我有两个类别

enum ColliderType:UInt32 {
case bulletCategory = 1
case bossCategory = 2
}

我让我的类(class)使用 SKPhysicsContactDelegate 并创建了一个节点

class PlayScene: SKScene, SKPhysicsContactDelegate {

var charBullets :[SKSpriteNode] = [SKSpriteNode]()
var charBulletTexture = SKTexture(imageNamed: "Bullet1.png")
var boss = SKSpriteNode()
var bossTexture = SKTexture(imageNamed: "BossImage.png")

并设置 physicsWorld.contactDelegate 并在 didMove(to view: SKView) 函数中创建一个节点

override func didMove(to view: SKView) { 

physicsWorld.contactDelegate = self

boss.name = "boss"
boss = SKSpriteNode(texture: bossTexture)
boss.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 300, height: 300))
boss.physicsBody?.usesPreciseCollisionDetection = true
boss.physicsBody!.isDynamic = true
boss.physicsBody!.affectedByGravity = false
boss.size = CGSize(width: 300, height: 300)
boss.physicsBody!.categoryBitMask = ColliderType.bossCategory.rawValue
boss.physicsBody!.collisionBitMask = 0
boss.physicsBody!.contactTestBitMask = ColliderType.bulletCategory.rawValue
boss.position = CGPoint(x: 0, y: self.size.height/4)
addChild(boss)

}

我在 touchesBegan 函数中创建了另一个节点

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

charBullet.name = "bullet"
charBullet.physicsBody = SKPhysicsBody(circleOfRadius: charBullet.size.width/64)
charBullet.physicsBody!.isDynamic = true
charBullet.physicsBody!.usesPreciseCollisionDetection = true
charBullet.physicsBody!.affectedByGravity = false
charBullet.physicsBody!.velocity = CGVector.init(dx: 0, dy: 450)
charBullet.physicsBody!.categoryBitMask = ColliderType.bulletCategory.rawValue
charBullet.physicsBody!.collisionBitMask = 0
charBullet.physicsBody!.contactTestBitMask = ColliderType.bossCategory.rawValue
charBullet.position = CGPoint(x: character.position.x, y: character.position.y + 100)
addChild(charBullet)
charBullets.append(charBullet)

}

然后我有一个 didBegin(_ contact: SKPhysicsContact) 但是当子弹和 boss 碰撞时它没有被调用

func didBegin(_ contact: SKPhysicsContact) {

//not printed
print("contact!")

if(charBullets.count > 0) {
var firstBody = SKPhysicsBody()
var secondBody = SKPhysicsBody()

if(contact.bodyA.node?.name == "bullet") {
print("test1")
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
print("test2")
firstBody = contact.bodyB
secondBody = contact.bodyA
print(secondBody)
}

if(firstBody.node?.name == "bullet") {
print("bulletname")
}

if(firstBody.node?.name == "boss") {
print("bossname")
}

if(secondBody.node?.name == "bullet") {
print("bulletname2")
}

if(secondBody.node?.name == "boss") {
print("bossname2")
}

if(firstBody.node?.name == "bullet" && secondBody.node?.name == "boss") {
print("hit the boss!")
charBullets[i].removeFromParent()
bossHealth -= 1
bossHealthLabel.text = "Boss Health \(bossHealth)"
}
}
}
}

最佳答案

更新:我通过更改他们的 physicsBody 来使用图像纹理大小而不是设置值并通过使用在节点创建下设置他们的名称来修复它

boss = SKSpriteNode(texture: bossTexture)
boss.name = "boss"
boss.physicsBody = SKPhysicsBody(texture: bossTexture, size: bossTexture.size())

let charBullet = SKSpriteNode(texture: charBulletTexture)
charBullet.name = "bullet"
charBullet.physicsBody = SKPhysicsBody(texture: charBulletTexture, size: charBulletTexture.size())

关于swift - SpriteNodes 之间没有碰撞检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53147313/

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