我试图避免Hero和Coins之间的碰撞,但不是Hero和Grass Ground之间的碰撞。一旦我对英雄应用了碰撞位面具,我的英雄就会穿过每件事。我想让它通过硬币而不是通过草地。这是代码,任何想法?
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;
答案 0 :(得分:1)
如果英雄必须只检测地面的碰撞,那么英雄的碰撞位掩码应为
heroSprite.physicsBody?.collisionBitMask = GROUNDCATEGORY
要检测与地面和其他地方的碰撞,您应该使用 OR |
运算符来合并categoryBitMasks
heroSprite.physicsBody?.collisionBitMask = GROUNDCATEGORY | SOMETHINGCATEGORY
contactTestBitMask
用于获取两个机构之间联系的回调。当两个对象共享相同的空间时,我们得到一个回调。它不处理碰撞。
我认为您需要的是用火和硬币接触检测以及与地面碰撞检测。因此,只需将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