gpt4 book ai didi

ios - Swift Sprite 在与 Bullet 接触时消失

转载 作者:行者123 更新时间:2023-11-29 01:03:50 25 4
gpt4 key购买 nike

我有一个太空射击游戏,它会随机生成敌方飞船。一旦玩家达到30级,就会产生一个“Boss” Sprite 。在最初的应用程序中,我将其设置为 Boss 出现的位置,并且仅左右移动,因为我遇到了 Sprite 在与子弹碰撞后消失的问题。

我没有将其设置为如果双方都进行了联系,则会将 Boss 从父级中删除。

我试图通过复制应用程序并删除除玩家和 boss Sprite 之外的所有内容来解决这个问题。

当设置PhysicsWorld.contactDelegate = self时, Sprite 会消失,因此这是一个碰撞问题。

这可能是 xcode 的错误吗?

这不是图像本身,因为我取出了图像,并且仅使用占位符图像(大红色 X)表示未找到该名称的图像。

这是它的样子的 gif bullet hitting boss

屏幕左侧显示的是从下往上发射的子弹从右到左的图像是老板 Sprite

这是我的 gamescene.swift

import SpriteKit
import UIKit

struct PhysicsCatagory {
static let Enemy : UInt32 = 1 //000000000000000000000000000001
static let Bullet : UInt32 = 0b1 //00000000000000000000000000010
static let Player : UInt32 = 4 //00000000000000000000000000100
static let tj: UInt32 = 0b10

}

class GameScene: SKScene, SKPhysicsContactDelegate {

var Highscore = Int()
var Score = Int()
var BossCounter = Int()
var TJBossHIT = Int()

var Player = SKSpriteNode(imageNamed: "player")
var Enemy = SKSpriteNode(imageNamed: "Enemy.png")
var TJ = SKSpriteNode(imageNamed: "tjBoss9.png")
var Fake = SKSpriteNode(imageNamed: "tjBoss.png")
var ScoreLbl = UILabel()

let HealthBarWidth: CGFloat = 100
let HealthBarHeight: CGFloat = 40
var TJHealth = 20

override func didMoveToView(view: SKView) {


Score = 0

physicsWorld.contactDelegate = self
// self.scene?.backgroundColor = UIColor.darkGrayColor()
self.scene?.size = CGSize(width: 640, height: 1136)
self.scene?.zPosition = 0
Player.position = CGPointMake(self.size.width / 2, self.size.height / 5)
Player.physicsBody = SKPhysicsBody(rectangleOfSize: Player.size)
Player.physicsBody?.affectedByGravity = false
Player.physicsBody?.categoryBitMask = PhysicsCatagory.Player
Player.physicsBody?.contactTestBitMask = PhysicsCatagory.Enemy
Player.physicsBody?.dynamic = false
var Timer = NSTimer.scheduledTimerWithTimeInterval(0.3, target: self, selector: Selector("SpawnBullets"), userInfo: nil, repeats: true)






let enemy1SpawnDelay = SKAction.waitForDuration(0.5)



runAction(
SKAction.sequence([
enemy1SpawnDelay,
SKAction.runBlock({self.SpawnTJ()}),
SKAction.waitForDuration(0.4)]) , withKey: "SpawnStop")


}



func didBeginContact(contact: SKPhysicsContact) {


var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB
//TJ Section for Coliision with bullets
if (((firstBody.categoryBitMask == PhysicsCatagory.tj) && (secondBody.categoryBitMask == PhysicsCatagory.Bullet)) ||
((firstBody.categoryBitMask == PhysicsCatagory.Bullet) && (secondBody.categoryBitMask == PhysicsCatagory.tj))){

// CollisionWithTJBoss(firstBody.node as! SKSpriteNode), Bullet: (secondBody.node as! SKSpriteNode)
CollisionWithTJBoss(firstBody.node as! SKSpriteNode, TJ: secondBody.node as! SKSpriteNode)

}
}

func CollisionWithBullet(Enemy: SKSpriteNode, Bullet:SKSpriteNode){
Enemy.removeFromParent()
Bullet.removeFromParent()
Score++


ScoreLbl.text = "\(Score)"
print(Score)
}
func SpawnTJ() {

TJ.physicsBody = SKPhysicsBody(rectangleOfSize: TJ.size)
TJ.physicsBody?.categoryBitMask = PhysicsCatagory.tj
TJ.physicsBody?.contactTestBitMask = PhysicsCatagory.Bullet


TJ.physicsBody?.affectedByGravity = false
TJ.physicsBody?.dynamic = true
TJ.physicsBody?.allowsRotation = false


TJ.position = CGPoint(x: self.size.width / 2, y: self.size.height + 30)
TJ.zPosition = 0.1

//TJ.zPosition = 0
let moveDown = SKAction.moveToY(self.size.height / 2, duration: 5.0)

let moveRight = SKAction.moveByX(200, y: 0, duration: 2.0)
let moveLeft = SKAction.moveByX(-200, y: 0, duration: 3.0)

let reverseMoveRight = moveRight.reversedAction()
let reverseMoveLeft = moveLeft.reversedAction()

let sequence1 = SKAction.sequence([moveRight, reverseMoveRight, moveLeft, reverseMoveLeft])
// let sequence1 = SKAction.sequence([moveLeft, reverseMoveLeft, moveRight, reverseMoveRight])
// let endlessAction = SKAction.repeatActionForever(sequence)



TJ.runAction(SKAction.sequence([moveDown]))
TJ.runAction(SKAction.repeatActionForever(sequence1), withKey: "TJBossEnd")

self.addChild(TJ)

}

func CollisionWithTJBoss (Bullet: SKSpriteNode, TJ:SKSpriteNode)
{
Bullet.removeFromParent()
TJBossHIT++
NSLog("Hit")
print(TJBossHIT)


}

func SpawnBullets(){

let Bullet = SKSpriteNode(imageNamed: "Bullets")
Bullet.zPosition = -2
Bullet.position = CGPointMake(Player.position.x, Player.position.y)

let action = SKAction.moveToY(self.size.height + 15, duration: 0.8)
let actionDone = SKAction.removeFromParent()
Bullet.runAction(SKAction.sequence([action, actionDone]))

Bullet.physicsBody = SKPhysicsBody(rectangleOfSize: Bullet.size)
Bullet.physicsBody?.categoryBitMask = PhysicsCatagory.Bullet
Bullet.physicsBody?.contactTestBitMask = PhysicsCatagory.Enemy
// Bullet.physicsBody?.contactTestBitMask = PhysicsCatagory.Bullet
Bullet.physicsBody?.contactTestBitMask = PhysicsCatagory.tj

Bullet.physicsBody?.affectedByGravity = false
Bullet.physicsBody?.dynamic = false
self.addChild(Bullet)
}

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

for touch: AnyObject in touches {
let location = touch.locationInNode(self)

Player.position.x = location.x

}

}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

for touch: AnyObject in touches {
let location = touch.locationInNode(self)

Player.position.x = location.x

}

}

override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}

最佳答案

你的子弹和你的敌人有相同的 categoryBitMAsk 值 1:

static let Enemy : UInt32 = 1 //000000000000000000000000000001
static let Bullet : UInt32 = 0b1 //00000000000000000000000000010

所以对 Enemy 的任何测试也将与 Bullet 匹配,反之亦然。

此外,在您的 didBeginContact 中,您通过调用另一个函数来处理碰撞,如下所示:

CollisionWithTJBoss(firstBody.node as! SKSpriteNode, TJ: secondBody.node as! SKSpriteNode)

CollisionWithTJBoss定义为:

func CollisionWithTJBoss (Bullet: SKSpriteNode, TJ:SKSpriteNode)

但是您没有做任何事情来保证 firstBody 是子弹而 secondBody 是 TJ(老板)。因此,当您在 CollisionWithTJBoss 中执行 Bullet.removeFromParent() 时,我认为您认为的 Sprite 是子弹,即 firstBody 实际上是 TJ。

为了调试目的,给你的 Sprite 命名,并在 didBeginContact 中做:

print("First body is \(firstBody.node.name) and second body is \(secondBody.node.name)")

要删除特定的 Sprite ,如果您不这样做,如果它是 firstBody 或 secondBody,请使用以下命令:

        let bulletNode = contact.bodyA.categoryBitMask == PhysicsCatagory.Bullet ? contact.bodyA.node! : contact.bodyB.node!
bulletNode.removeFromParent()

这保证 bulletNode 是项目符号(如果您的 categoryBitMasks 设置正确)。

您可能会发现按照以下方式重构您的 didBeginContact 不会那么困惑:

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

switch contactMask {
// TJ and Bullet have contacted
case PhysicsCatagory.tj | PhysicsCatagory.Bullet :
let bulletNode = contact.bodyA.categoryBitMask == PhysicsCatagory.Bullet ? contact.bodyA.node! : contact.bodyB.node!
bulletNode.removeFromParent()
TJBossHIT -= 1 // ++ is deprecated
NSLog("Boss and Bullet have Hit")
print(TJBossHIT)

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

您可以添加尽可能多的 PhysicsCategory.TJ | PhysicsCategory.Bullet 案例,因为您需要在游戏中采取行动的所有联系人。为每个潜在联系人单独编码,您就不会在 if...then...else 中迷失自己

关于ios - Swift Sprite 在与 Bullet 接触时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36669256/

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