gpt4 book ai didi

swift - 如何使语句仅适用于 Sprite Kit 中的选定级别

转载 作者:搜寻专家 更新时间:2023-10-31 22:20:06 24 4
gpt4 key购买 nike

所以这里我有这段代码

 if collision == PhysicsCategory.Cat | PhysicsCategory.Bed {
print("SUCCESS")
win()
if currentLevel < 10 {
currentLevel += 1
}

基本上,此声明适用于我拥有的每个级别。我只想在一个级别上这样做

 if collision == PhysicsCategory.Cat | PhysicsCategory.Bed| PhysicsCategory.Bottle {
print("SUCCESS")
win()
if currentLevel < 10 {
currentLevel += 1
}

但是如果我要使用它就意味着在每个关卡中都需要与瓶子碰撞,但是我只想在一个关卡中使用它

我用的是swift

谢谢!

为@Mundi

这是GameScene中的代码

 import SpriteKit
Game scene
protocol EventListenerNode {
func didMoveToScene()
}
protocol InteractiveNode {
func interact()
}

struct PhysicsCategory {
static let None: UInt32 = 0
static let Cat: UInt32 = 0b1 // 1
static let Block: UInt32 = 0b10 // 2
static let Bed: UInt32 = 0b100 // 4
static let Edge: UInt32 = 0b1000 // 8
static let Label: UInt32 = 0b10000 // 16
static let Spring:UInt32 = 0b100000 //32
static let Hook: UInt32 = 0b1000000 //64
static let Bottle: UInt32 = 0b10000000 //64

}

class GameScene: SKScene, SKPhysicsContactDelegate {

var bedNode: BedNode!
var catNode: CatNode!
var lineNode: LineNode!
var bottleNode: BottleNode!
var desiredCollision = PhysicsCategory.Cat | PhysicsCategory.Bed


var playable = true

override func didMove(to view: SKView) {
// Calculate playable margin
let maxAspectRatio: CGFloat = 16.0/9.0
let maxAspectRatioHeight = size.width / maxAspectRatio
let playableMargin: CGFloat = (size.height - maxAspectRatioHeight)/2
let playableRect = CGRect(x: 0, y: playableMargin,
width: size.width, height: size.height-playableMargin*2)
physicsBody = SKPhysicsBody(edgeLoopFrom: playableRect)
physicsWorld.contactDelegate = self
physicsBody!.categoryBitMask = PhysicsCategory.Edge

enumerateChildNodes(withName: "//*", using: { node, _ in
if let eventListenerNode = node as? EventListenerNode {
eventListenerNode.didMoveToScene()


}

})

bedNode = childNode(withName: "bed") as! BedNode
catNode = childNode(withName: "//cat_body") as! CatNode
bottleNode = childNode(withName: "bottle") as! BottleNode




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

if collision == PhysicsCategory.Label | PhysicsCategory.Edge {

let labelNode = contact.bodyA.categoryBitMask == PhysicsCategory.Label ?
contact.bodyA.node :
contact.bodyB.node

if let message = labelNode as? MessageNode {
message.didBounce()
}
}

if !playable {
return
}


if currentLevel == 20 { // or whatever level you want
desiredCollision |= PhysicsCategory.Bottle

}

if collision == desiredCollision {
print ("Win")
win()


} else if collision == PhysicsCategory.Cat | PhysicsCategory.Edge {
print("FAIL")
lose()

这是来 self 的 BottleBode 的代码

导入 SpriteKit

class BottleNode: SKSpriteNode, EventListenerNode {
func didMoveToScene() {
print("bottle added to scene")

let bedBodySize = CGSize(width: 40.0, height: 30.0)
physicsBody = SKPhysicsBody(rectangleOf: bedBodySize)
physicsBody!.isDynamic = true

physicsBody!.categoryBitMask = PhysicsCategory.Bottle
physicsBody!.collisionBitMask = PhysicsCategory.Block | PhysicsCategory.Edge | PhysicsCategory.Spring | PhysicsCategory.Spring
parent!.physicsBody!.contactTestBitMask = PhysicsCategory.Bed | PhysicsCategory.Edge

}
}

我按照你告诉我的做了,但它仍然不起作用,当 CatNode 接触地面时,我失去了条件并且我收到错误。

这是它如何不起作用的图片 /image/0JrMJ.png

这是一个错误。

/image/8VGRd.png

你能告诉我哪里错了吗?

非常感谢

最佳答案

可能没有特殊变量并且更简洁:

var desiredCollision = PhysicsCategory.Cat | PhysicsCategory.Bed
if currentLevel == 7 { // or whatever level you want
desiredCollision |= PhysicsCategory.Bottle
}

if collision = desiredCollision {
// ...

关于swift - 如何使语句仅适用于 Sprite Kit 中的选定级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40794778/

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