gpt4 book ai didi

ios - Swift SKPhysicsBody - 物理体节点设置为零

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

我正在制作一款《太空入侵者》游戏,但我在碰撞方面遇到了麻烦。在我的游戏中,敌方船只应该在子弹击中时消失并爆炸,但相反,它有时会爆炸,有时什么也不会发生。我研究了代码并尝试进行一些调试,我发现有时,子弹的物理主体为空。我找不到任何其他问题可以回答这个问题。

以下是与碰撞和 SKPhysicsBody 相关的代码的一些部分

let background = SKSpriteNode(imageNamed: "background")
let player = SKSpriteNode(imageNamed: "playerShip")

struct PhysicsCategory {
static let None: UInt32 = 0 // 0
static let Player: UInt32 = 0b1 // 1
static let Bullet: UInt32 = 0b10 // 2
static let Enemy: UInt32 = 0b100 // 4
}

// Did move to view
override func didMove(to view: SKView) {
self.physicsWorld.contactDelegate = self

// Player
player.setScale(1)
player.position = CGPoint(x: self.size.width/2, y: self.size.height/5)
player.zPosition = 2
player.physicsBody = SKPhysicsBody(rectangleOf: player.size)
player.physicsBody!.affectedByGravity = false
player.physicsBody!.categoryBitMask = PhysicsCategory.Player // assigning the physics category
player.physicsBody!.collisionBitMask = PhysicsCategory.None // no collision
player.physicsBody!.contactTestBitMask = PhysicsCategory.Enemy // what the physics body can touch
self.addChild(player)
}

// Contact
func didBegin(_ contact: SKPhysicsContact) {
var body1 = SKPhysicsBody()
var body2 = SKPhysicsBody()

// Organizing physics bodies based on category number
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
body1 = contact.bodyA
body2 = contact.bodyB
} else {
body1 = contact.bodyB
body1 = contact.bodyA
}

if body1.categoryBitMask == PhysicsCategory.Player && body2.categoryBitMask == PhysicsCategory.Enemy {
// Player hits enemy
if body1.node != nil { // If there is a node (avoids crashing)
spawnExplosion(body1.node!.position)
}
if body2.node != nil { // If there is a node (avoids crashing)
spawnExplosion(body2.node!.position)
}
body1.node?.removeFromParent()
body2.node?.removeFromParent()
}
if (body1.categoryBitMask == PhysicsCategory.Bullet && body2.categoryBitMask == PhysicsCategory.Enemy) {
// Bullet hits enemy
updateScore()

if body2.node != nil {
if body2.node!.position.y > self.size.height {
return // will stop function
} else {
spawnExplosion(body2.node!.position)
}
}

body1.node?.removeFromParent()
body2.node?.removeFromParent()
}
}

// Bullet
func fireBullet() {
let bullet = SKSpriteNode(imageNamed: "bullet")
bullet.setScale(1)
bullet.position = player.position
bullet.zPosition = 1
bullet.physicsBody = SKPhysicsBody(rectangleOf: bullet.size)
bullet.physicsBody!.affectedByGravity = false
bullet.physicsBody!.categoryBitMask = PhysicsCategory.Bullet // assigning the physics category
bullet.physicsBody!.collisionBitMask = PhysicsCategory.None // no collisions
bullet.physicsBody!.contactTestBitMask = PhysicsCategory.Enemy // what the physics body can touch
self.addChild(bullet)

let moveBullet = SKAction.moveTo(y: self.size.height + bullet.size.height, duration: 1)
let deleteBullet = SKAction.removeFromParent()
let bulletSequence = SKAction.sequence([bulletSound,moveBullet, deleteBullet])
bullet.run(bulletSequence)
}

// Enemy
func spawnEnemy() {
let randomStartX = random(min: gameArea.minX, max: gameArea.maxX)
let randomEndX = random(min: gameArea.minX, max: gameArea.maxX)
let startPoint = CGPoint(x: randomStartX, y: self.size.height * 1.2)
let endPoint = CGPoint(x: randomEndX, y: -self.size.height * 0.2)

let enemy = SKSpriteNode(imageNamed: "enemyShip")
enemy.setScale(1)
enemy.position = startPoint
enemy.zPosition = 2
enemy.physicsBody = SKPhysicsBody(rectangleOf: enemy.size)
enemy.physicsBody!.affectedByGravity = false
enemy.physicsBody!.categoryBitMask = PhysicsCategory.Enemy // assigning the physics category
enemy.physicsBody!.collisionBitMask = PhysicsCategory.None // no collisions
enemy.physicsBody!.contactTestBitMask = PhysicsCategory.Player | PhysicsCategory.Bullet // what the physics body can touch
self.addChild(enemy)

调试(在didBeginContact中)

print(body1) // DEBUGGING
print(body2) // DEBUGGING
print() // DEBUGGING

控制台

<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> 
name:'(null)' texture:[<SKTexture> 'bullet' (25 x 100)] position:
{795.82611083984375, 1766.0010986328125} scale:{1.00, 1.00} size:{25,
100} anchor:{0.5, 0.5} rotation:0.00]
<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode>
name:'(null)' texture:[<SKTexture> 'enemyShip' (204 x 88)] position:
{808.9945068359375, 1900.0888671875} scale:{1.00, 1.00} size:{204, 88}
anchor:{0.5, 0.5} rotation:-1.39]

<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> .
name:'(null)' texture:[<SKTexture> 'enemyShip' (204 x 88)] position:
{892.953125, 1039.9288330078125} scale:{1.00, 1.00} size:{204, 88}
anchor:{0.5, 0.5} rotation:-1.58]
<SKPhysicsBody> type:<Unknown> representedObject:[(null)]

<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode>
name:'(null)' texture:[<SKTexture> 'enemyShip' (204 x 88)] position:
{891.6375732421875, 896.56890869140625} scale:{1.00, 1.00} size:{204,
88} anchor:{0.5, 0.5} rotation:-1.58]
<SKPhysicsBody> type:<Unknown> representedObject:[(null)]

<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode>
name:'(null)' texture:[<SKTexture> 'enemyShip' (204 x 88)] position:
{889.73736572265625, 689.49334716796875} scale:{1.00, 1.00} size:{204, 88}
.anchor:{0.5, 0.5} rotation:-1.58]
<SKPhysicsBody> type:<Unknown> representedObject:[(null)]

<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> .
name:'(null)' texture:[<SKTexture> 'playerShip' (88 x 204)] position:
{850.55072021484375, 254.14501953125} scale:{1.00, 1.00} size:{88, 204}
anchor:{0.5, 0.5} rotation:0.00]
<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> .
name:'(null)' texture:[<SKTexture> 'enemyShip' (204 x 88)] position:
{886.52154541015625, 339.05770874023438} scale:{1.00, 1.00} size:{204,
88} anchor:{0.5, 0.5} rotation:-1.58]

<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode>
name:'(null)' texture:[<SKTexture> 'bullet' (25 x 100)] position:
{1150.1441650390625, 777} scale:{1.00, 1.00} size:{25, 100} anchor:
{0.5, 0.5} rotation:0.00]
<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> .
name:'(null)' texture:[<SKTexture> 'enemyShip' (204 x 88)] position:
{1205.85693359375, 800.99554443359375} scale:{1.00, 1.00} size:{204, 88} anchor:{0.5, 0.5} rotation:-1.47]

最佳答案

请尝试更改这部分

 body1 = contact.bodyB
body1 = contact.bodyA

至:

 body1 = contact.bodyB
body2 = contact.bodyA

关于ios - Swift SKPhysicsBody - 物理体节点设置为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51954462/

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