Collision Bitmask Hero 穿过一切,甚至是地面
Collision Bit Mask Hero passes right through EVERY thing, even the Ground
我试图避免英雄和硬币之间的碰撞,但不是英雄和草地之间的碰撞。一旦我将碰撞位掩码应用于 Hero,我的 Hero 就会穿过所有东西。我希望它穿过硬币而不是穿过草地。这是代码,有什么想法吗?
let HEROCATEGORY: UInt32 = 0x1 << 1;
let GROUNDCATEGORY: UInt32 = 0x1 << 2;
let FIRECATEGORY: UInt32 = 0x1 << 3;
let COINCATEGORY: UInt32 = 0x1 << 4;
let NUMBERCATEGORY: UInt32 = 0x1 << 5;
heroSprite.physicsBody!.categoryBitMask = HEROCATEGORY;
heroSprite.physicsBody!.collisionBitMask = 0x10000000;
heroSprite.physicsBody!.contactTestBitMask = GROUNDCATEGORY | FIRECATEGORY | COINCATEGORY | NUMBERCATEGORY;
grassGround.physicsBody!.categoryBitMask = GROUNDCATEGORY;
grassGround.physicsBody!.collisionBitMask = 0x01000000;
coinSprite.physicsBody!.categoryBitMask = COINCATEGORY;
coinSprite.physicsBody!.contactTestBitMask = HEROCATEGORY;
coinSprite.physicsBody!.collisionBitMask = 0x10000000;
如果英雄只需要检测与地面的碰撞,那么英雄的碰撞位掩码应该是
heroSprite.physicsBody?.collisionBitMask = GROUNDCATEGORY
要检测与地面和其他物体的碰撞,您应该使用 OR |
运算符 将两者结合起来 categoryBitMasks
heroSprite.physicsBody?.collisionBitMask = GROUNDCATEGORY | SOMETHINGCATEGORY
contactTestBitMask
用于获取两个物体之间接触时的回调。当两个对象共享相同的 space 时,我们会收到回调。它不处理碰撞。
我认为你需要的是火和硬币的接触检测以及与地面的碰撞检测。因此,只需将 heroSprite
的冲突位掩码设置为 GROUNDCATEGORY
,就像在我的第一个代码片段中一样。
let HEROCATEGORY: UInt32 = 0x1 << 1
let GROUNDCATEGORY: UInt32 = 0x1 << 2
let FIRECATEGORY: UInt32 = 0x1 << 3
let COINCATEGORY: UInt32 = 0x1 << 4
let NUMBERCATEGORY: UInt32 = 0x1 << 5
heroSprite.physicsBody!.categoryBitMask = HEROCATEGORY
heroSprite.physicsBody!.collisionBitMask = GROUNDCATEGORY // changed
heroSprite.physicsBody!.contactTestBitMask = GROUNDCATEGORY | FIRECATEGORY | COINCATEGORY | NUMBERCATEGORY
grassGround.physicsBody!.categoryBitMask = GROUNDCATEGORY
grassGround.physicsBody!.collisionBitMask = HEROCATEGORY
coinSprite.physicsBody!.categoryBitMask = COINCATEGORY
coinSprite.physicsBody!.contactTestBitMask = HEROCATEGORY
我试图避免英雄和硬币之间的碰撞,但不是英雄和草地之间的碰撞。一旦我将碰撞位掩码应用于 Hero,我的 Hero 就会穿过所有东西。我希望它穿过硬币而不是穿过草地。这是代码,有什么想法吗?
let HEROCATEGORY: UInt32 = 0x1 << 1;
let GROUNDCATEGORY: UInt32 = 0x1 << 2;
let FIRECATEGORY: UInt32 = 0x1 << 3;
let COINCATEGORY: UInt32 = 0x1 << 4;
let NUMBERCATEGORY: UInt32 = 0x1 << 5;
heroSprite.physicsBody!.categoryBitMask = HEROCATEGORY;
heroSprite.physicsBody!.collisionBitMask = 0x10000000;
heroSprite.physicsBody!.contactTestBitMask = GROUNDCATEGORY | FIRECATEGORY | COINCATEGORY | NUMBERCATEGORY;
grassGround.physicsBody!.categoryBitMask = GROUNDCATEGORY;
grassGround.physicsBody!.collisionBitMask = 0x01000000;
coinSprite.physicsBody!.categoryBitMask = COINCATEGORY;
coinSprite.physicsBody!.contactTestBitMask = HEROCATEGORY;
coinSprite.physicsBody!.collisionBitMask = 0x10000000;
如果英雄只需要检测与地面的碰撞,那么英雄的碰撞位掩码应该是
heroSprite.physicsBody?.collisionBitMask = GROUNDCATEGORY
要检测与地面和其他物体的碰撞,您应该使用 OR |
运算符 将两者结合起来 categoryBitMasks
heroSprite.physicsBody?.collisionBitMask = GROUNDCATEGORY | SOMETHINGCATEGORY
contactTestBitMask
用于获取两个物体之间接触时的回调。当两个对象共享相同的 space 时,我们会收到回调。它不处理碰撞。
我认为你需要的是火和硬币的接触检测以及与地面的碰撞检测。因此,只需将 heroSprite
的冲突位掩码设置为 GROUNDCATEGORY
,就像在我的第一个代码片段中一样。
let HEROCATEGORY: UInt32 = 0x1 << 1
let GROUNDCATEGORY: UInt32 = 0x1 << 2
let FIRECATEGORY: UInt32 = 0x1 << 3
let COINCATEGORY: UInt32 = 0x1 << 4
let NUMBERCATEGORY: UInt32 = 0x1 << 5
heroSprite.physicsBody!.categoryBitMask = HEROCATEGORY
heroSprite.physicsBody!.collisionBitMask = GROUNDCATEGORY // changed
heroSprite.physicsBody!.contactTestBitMask = GROUNDCATEGORY | FIRECATEGORY | COINCATEGORY | NUMBERCATEGORY
grassGround.physicsBody!.categoryBitMask = GROUNDCATEGORY
grassGround.physicsBody!.collisionBitMask = HEROCATEGORY
coinSprite.physicsBody!.categoryBitMask = COINCATEGORY
coinSprite.physicsBody!.contactTestBitMask = HEROCATEGORY