gpt4 book ai didi

swift - SKPhysicsBody 碰撞不起作用

转载 作者:搜寻专家 更新时间:2023-10-30 22:11:04 26 4
gpt4 key购买 nike

下面是我的“Floor.swift”类,下面基本上是一堆墙。我有来自屏幕顶部的对象,一旦 Floor 和 SKSpriteNodes 发生碰撞,我希望 SKSpriteNode 被移除。下面是我的 Floor 类。

import Foundation
import SpriteKit

class Floor: SKNode {
override init() {
super.init()

let leftWall = SKSpriteNode(color: UIColor.clear, size: CGSize(width: 5, height: 50))
leftWall.position = CGPoint(x: 0, y: 50)
leftWall.physicsBody = SKPhysicsBody(rectangleOf: leftWall.size)
leftWall.physicsBody!.isDynamic = false
self.addChild(leftWall)

let rightWall = SKSpriteNode(color: UIColor.clear, size: CGSize(width: 5, height: 50))
rightWall.position = CGPoint(x: 375, y: 50)
rightWall.physicsBody = SKPhysicsBody(rectangleOf: rightWall.size)
rightWall.physicsBody!.isDynamic = false
self.addChild(rightWall)

let bottomWall = SKSpriteNode(color: UIColor.clear, size: CGSize(width: 500, height: 10))
bottomWall.position = CGPoint(x: 150, y: -5)
bottomWall.physicsBody = SKPhysicsBody(rectangleOf: bottomWall.size)
bottomWall.physicsBody!.isDynamic = false
self.addChild(bottomWall)

self.physicsBody = SKPhysicsBody(bodies: [leftWall.physicsBody!, rightWall.physicsBody!, bottomWall.physicsBody!])

self.physicsBody?.categoryBitMask = floorCategory
self.physicsBody?.contactTestBitMask = nailDropCategory | pointCategory | lifeCategory
self.physicsBody?.collisionBitMask = balloonCategory
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemted")
}
}

我在“func didBegin(_ contact: SKPhysicsContact)”下的 GameScene 类中写道:

func didBegin(_ contact: SKPhysicsContact) {        
if (contact.bodyA.categoryBitMask == nailDropCategory | pointCategory | lifeCategory) && (contact.bodyB.categoryBitMask == floorCategory) {
// contact.bodyB.node!.removeFromParent()
print("COLLISION")
}
}

如您所见,在我的 Floor 类中,我将对象设置为“self.physicsBody = SKPhysicsBody(bodies:)”以及我所有的 SKSpriteNode。但出于某种原因,我没有得到任何检测。我让我的“Floor”类成为我的 objectCategory、pointCategory、lifeCategory 的每个类上的“contactTestBitMask”。这个问题我已经坚持了一分钟了,有什么想法吗?

更新:

所以我现在正在使用下面的这段代码,它可以正常工作!唯一的问题是,默认情况下也删除了我的“balloonCategory”,我不希望它这样做。我怎样才能让它只忽略那个类别,同时充当气球的墙?

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

switch contactMask {
case balloonCategory | nailDropCategory:
print("nailDrop and balloon have contacted.")
contact.bodyB.node!.removeFromParent()
self.run(SKAction.playSoundFileNamed("lostlifesound.mp3", waitForCompletion:false))
lifeLost -= 1
case balloonCategory | pointCategory:
print("point and balloon have contacted.")
contact.bodyB.node!.removeFromParent()
totalPoints += 2
self.run(SKAction.playSoundFileNamed("pointsound.mp3", waitForCompletion:false))
case balloonCategory | lifeCategory:
print("life and balloon have contacted.")
contact.bodyB.node!.removeFromParent()
lifeLost += 1
self.run(SKAction.playSoundFileNamed("pointsound.mp3", waitForCompletion:false))
default:
contact.bodyB.node!.removeFromParent()
print("Removed \(String(describing: contact.bodyB.node!.name))")
}
}

最佳答案

听起来您会受益于为所有对象类型分配名称,如下所示:

class Balloon: SKSpriteNode {
init() {
super.init(texture: nil, color: .blue, size: CGSize(width: 50, height: 50))
self.name = "balloon"
}
}

现在,当您在开关中进行检查时,您可以在决定删除对象之前验证对象的身份。在下面的 switch 语句中查看我对 default case 所做的更改。这样,如果节点是气球,则不会被删除。物理碰撞将继续起作用。

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

switch contactMask {
case balloonCategory | nailDropCategory:
print("nailDrop and balloon have contacted.")
contact.bodyB.node!.removeFromParent()
self.run(SKAction.playSoundFileNamed("lostlifesound.mp3", waitForCompletion:false))
lifeLost -= 1
case balloonCategory | pointCategory:
print("point and balloon have contacted.")
contact.bodyB.node!.removeFromParent()
totalPoints += 2
self.run(SKAction.playSoundFileNamed("pointsound.mp3", waitForCompletion:false))
case balloonCategory | lifeCategory:
print("life and balloon have contacted.")
contact.bodyB.node!.removeFromParent()
lifeLost += 1
self.run(SKAction.playSoundFileNamed("pointsound.mp3", waitForCompletion:false))
default:
if contact.bodyB.node!.name == "balloon" {
print("Balloon collided but we will not remove it.")
return
} else {
contact.bodyB.node!.removeFromParent()
print("Removed \(String(describing: contact.bodyB.node!.name))")
}
}

关于swift - SKPhysicsBody 碰撞不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47984735/

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