gpt4 book ai didi

ios - Sprite-Kit 类别位掩码对于 Physics body 是错误的

转载 作者:可可西里 更新时间:2023-11-01 01:41:03 24 4
gpt4 key购买 nike

这真让我恼火,我有一个播放器和一个场景,还有碰撞检测。当我进入 didBeginContact 时,if 条件返回 false。

我停下来调试,secondbody(在这种情况下应该是场景)有一个随机数的 categoryBitMask,不是我从中分配的那个PhysicsCategory 结构,所以它不会进入 if 条件。是因为场景是 edgeLoopF​​romRect 吗?

代码如下:

struct PhysicsCategory {
static let None : UInt32 = 0
static let Player : UInt32 = 0b1
static let Scene : UInt32 = 0b10
static let All : UInt32 = UInt32.max
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var canJump = true
var touchStart = CGPoint()
var touchEnd = CGPoint()
var squaresArray = NSMutableArray()
var player = SKSpriteNode(imageNamed: "craft.png")
override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
physicsBody?.categoryBitMask = PhysicsCategory.Scene
physicsBody?.contactTestBitMask = PhysicsCategory.Player
physicsWorld.gravity = CGVectorMake(0, -2)
physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
physicsBody?.usesPreciseCollisionDetection = true
setUpPlayer()
}
func setUpPlayer() {
player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size)
player.physicsBody?.categoryBitMask = PhysicsCategory.Player
player.physicsBody?.contactTestBitMask = PhysicsCategory.All
player.physicsBody?.mass = 1
player.physicsBody?.restitution = 0
player.xScale = 0.5
player.yScale = 0.5
player.position = CGPoint(x: 600, y: 700)
addChild(player)
}

func didBeginContact(contact: SKPhysicsContact) {
var firstBody: SKPhysicsBody?
var secondBody: SKPhysicsBody?
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
if (firstBody!.node != nil && secondBody!.node != nil) {
println("not nil")
if ((firstBody!.categoryBitMask == PhysicsCategory.Player) &&
(secondBody!.categoryBitMask == PhysicsCategory.Scene)) {
println("collision")
canJump = true
}
}
}

最佳答案

我可以等一下吗...

WTTTTFFFFFFF

现在我明白了,问题是我在设置 categoryBitMask 之后声明了 physicsBody。所以切换这个:

override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
physicsBody?.categoryBitMask = PhysicsCategory.Scene
physicsBody?.contactTestBitMask = PhysicsCategory.Player
physicsWorld.gravity = CGVectorMake(0, -2)
physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)//this line moves up
physicsBody?.usesPreciseCollisionDetection = true
setUpPlayer()
}

为此:

override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)//to here
physicsBody?.categoryBitMask = PhysicsCategory.Scene
physicsBody?.contactTestBitMask = PhysicsCategory.Player
physicsWorld.gravity = CGVectorMake(0, -2)
physicsBody?.usesPreciseCollisionDetection = true
setUpPlayer()
}

解决了这个问题...

关于ios - Sprite-Kit 类别位掩码对于 Physics body 是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29662386/

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