如何使语句仅适用于 Sprite Kit 中的选定级别
How to make statement work only for the selected level in Sprite Kit
所以我有这段代码
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()
这是我的 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 接触地面时,我失去了条件,我收到错误消息。
这是它如何不起作用的图片
https://i.stack.imgur.com/0JrMJ.png
这是一个错误。
https://i.stack.imgur.com/8VGRd.png
你能告诉我哪里错了吗?
非常感谢
您可以使用嵌套的 if-else 语句首先检查它是否是特殊级别,然后根据结果决定使用哪个版本。它看起来像这样:
var useSpecialCollision: bool // Change this to true/false based on when the level changes
if useSpecialCollision == true {
if collision == PhysicsCategory.Cat | PhysicsCategory.Bed| PhysicsCategory.Bottle {
print("SUCCESS")
win()
if currentLevel < 10 {
currentLevel += 1
}
}
} else {
if collision == PhysicsCategory.Cat | PhysicsCategory.Bed {
print("SUCCESS")
win()
if currentLevel < 10 {
currentLevel += 1
}
}
}
这会将您的 if statements/code 放入另一个 if 语句以确定要 运行 的代码。不是很复杂...也不是最简洁的...
可能没有特殊变量并且更简洁:
var desiredCollision = PhysicsCategory.Cat | PhysicsCategory.Bed
if currentLevel == 7 { // or whatever level you want
desiredCollision |= PhysicsCategory.Bottle
}
if collision = desiredCollision {
// ...
所以我有这段代码
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()
这是我的 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 接触地面时,我失去了条件,我收到错误消息。
这是它如何不起作用的图片 https://i.stack.imgur.com/0JrMJ.png
这是一个错误。
https://i.stack.imgur.com/8VGRd.png
你能告诉我哪里错了吗?
非常感谢
您可以使用嵌套的 if-else 语句首先检查它是否是特殊级别,然后根据结果决定使用哪个版本。它看起来像这样:
var useSpecialCollision: bool // Change this to true/false based on when the level changes
if useSpecialCollision == true {
if collision == PhysicsCategory.Cat | PhysicsCategory.Bed| PhysicsCategory.Bottle {
print("SUCCESS")
win()
if currentLevel < 10 {
currentLevel += 1
}
}
} else {
if collision == PhysicsCategory.Cat | PhysicsCategory.Bed {
print("SUCCESS")
win()
if currentLevel < 10 {
currentLevel += 1
}
}
}
这会将您的 if statements/code 放入另一个 if 语句以确定要 运行 的代码。不是很复杂...也不是最简洁的...
可能没有特殊变量并且更简洁:
var desiredCollision = PhysicsCategory.Cat | PhysicsCategory.Bed
if currentLevel == 7 { // or whatever level you want
desiredCollision |= PhysicsCategory.Bottle
}
if collision = desiredCollision {
// ...