gpt4 book ai didi

ios - Collision Bitmask Hero 穿过一切,甚至是地面

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

我试图避免英雄和硬币之间的碰撞,但不是英雄和草地之间的碰撞。一旦我将碰撞位掩码应用于 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 用于获取两个主体之间接触时的回调。当两个对象共享同一空间时,我们会收到回调。它不处理碰撞。

我认为您需要的是火和硬币的接触检测以及与地面的碰撞检测。因此,只需将 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

关于ios - Collision Bitmask Hero 穿过一切,甚至是地面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28491427/

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