gpt4 book ai didi

ios - Spritekit 物理实体与错误的实体碰撞

转载 作者:行者123 更新时间:2023-11-28 07:47:24 25 4
gpt4 key购买 nike

我正在学习 Spritekit 并制作 flappy bird 游戏。我正在尝试在管道之间添加一个分数节点,但鸟一直与该节点发生碰撞。我发现这是因为管道,但不知道如何解决这个问题。

这是 pipesHolder 的声明:

var PipesHolder = SKNode()

这是创建管道和分数节点的函数:

func createPipes() {
pipeHolder = SKNode()
pipeHolder.name = "Holder"

// SCORE NODE
let scoreNode = SKSpriteNode()
scoreNode.name = "Score"
scoreNode.color = UIColor.red
scoreNode.size = CGSize(width: 5, height: 300)
scoreNode.position = CGPoint(x: 15, y: 0)
scoreNode.physicsBody = SKPhysicsBody(rectangleOf: scoreNode.size)
scoreNode.physicsBody?.categoryBitMask = ColliderType.Score
scoreNode.physicsBody?.collisionBitMask = 0
scoreNode.physicsBody?.affectedByGravity = false
scoreNode.physicsBody?.isDynamic = false

let pipeUp = SKSpriteNode(imageNamed: "pipeUp")
pipeUp.name = "PipeUp"
pipeUp.yScale = 1.5
pipeUp.position = CGPoint(x: 0, y: self.frame.height / 2.1)
pipeUp.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: pipeUp.size.width, height: pipeUp.size.height - pipeUp.size.height / 12))
pipeUp.physicsBody?.categoryBitMask = ColliderType.Pipes
pipeUp.physicsBody?.affectedByGravity = false
pipeUp.physicsBody?.isDynamic = false

let pipeDown = SKSpriteNode(imageNamed: "pipeDown")
pipeDown.name = "pipeDown"
pipeDown.yScale = 1.5
pipeDown.position = CGPoint(x: 0, y: -(self.frame.height / 2.1))
pipeDown.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: pipeDown.size.width, height: pipeDown.size.height - pipeDown.size.height / 12))
pipeDown.physicsBody?.categoryBitMask = ColliderType.Pipes
pipeDown.physicsBody?.affectedByGravity = false
pipeDown.physicsBody?.isDynamic = false

PipeHolder.zPosition = 2
PipeHolder.position.x = self.frame.width + 20
PipeHolder.position.y = CGFloat.randomBetween(numberOne: -(self.frame.size.height / 5.5), numberTwo: (self.frame.height / 5.5))
PipeHolder.addChild(scoreNode)
PipeHolder.addChild(PipesUp)
PipeHolder.addChild(PipesDown)

下面是 Collider 设置和 bird 的配置:

struct ColliderType {
static let Bird: UInt32 = 1
static let Score: UInt32 = 2
static let Pipes: UInt32 = 3
}

class Bird: SKSpriteNode {
func initialize() {
self.name = "Bird"
self.zPosition = 5
self.setScale(1.5)
self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width / 3)
self.physicsBody?.affectedByGravity = true
self.physicsBody?.allowsRotation = false

self.physicsBody?.categoryBitMask = ColliderType.Bird
self.physicsBody?.collisionBitMask = ColliderType.Pipes
self.physicsBody?.contactTestBitMask = ColliderType.Pipes | ColliderType.Score
}

最佳答案

保持直截了当的一个好方法是像这样定义您的类别:

let oneCategory: UInt32 = 0x1 << 1
let anotherCategory: UInt32 = 0x1 << 2
let thirdCategory: UInt32 = 0x1 << 3
let fourthrCategory: UInt32 = 0x1 << 4
let fifthCategory: UInt32 = 0x1 << 5
let sixthCategory: UInt32 = 0x1 << 6
let nothingCategory: UInt32 = 0

关于ios - Spritekit 物理实体与错误的实体碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50632475/

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