是否可以在 spriteKit 中停用物理体中的碰撞? [英] Is it possible to deactivate collisions in physics bodies in spriteKit?
问题描述
我正在寻找在我的 iO 版 spriteKit 游戏中与我的英雄一起收集物品的最佳方法,在尝试了几种方法之后,我的结论是最好的方法是拥有一个带有可以检测到碰撞但不会与我的英雄发生碰撞的实体.有可能做到吗?停用物理实体的碰撞而不停用其检测碰撞的能力?我知道听起来有点矛盾......因为,另一种方法是只创建一个没有物理体的 SKSpriteNode,那么就不会有碰撞,但检测"碰撞的方法将是手工制作的,而且更难,因为我需要在我的英雄中设置一个坐标系检测,当他处于那些特定坐标(在项目上)时,我会让项目消失.知道如何更轻松地执行这两种方法中的任何一种吗?
I'm looking at doing the best way to collect items with my hero in my spriteKit game for iOs, and after to try a few ways to do it, my conclusion is the best way would be to have an item with a physic body which can detect collisions but don't collide with my hero. Is it possible to do it? to deactivate collisions of a physic body without deactivating its capabilities to detect collisions?? Sounds a bit contradictory I know... Because, the other way would be to create only a SKSpriteNode without physic body, then there wouldn't being collisions, but the way to "detect" collisions would be hand made and much more harder, because i would need to set a coordinate system detection in my hero, that when he will be in those specifics coordinates (over the item) then i'll make the item disappears. Any idea of how to do any of the two ways easier?
推荐答案
查看 collisionBitMask, categoryBitMask 和 contactTestBitMask 在 SKPhysicsBody
班级.
Check out collisionBitMask, categoryBitMask, and contactTestBitMask in the SKPhysicsBody
class.
本质上,具有相同 collisionBitMask
值的物理体将相互传递".
Essentially, physics bodies with the same collisionBitMask
value will "pass-through" each other.
- 更正:如果类别和碰撞位匹配,它们就会相互作用.如果它们不匹配,这两者将不交互.如果碰撞位和类别位都是 零,那么该项目当然会与任何东西都没有交互.
然后,您设置 categoryBitMask
和 contactTestBitMask
值以在接触时创建 SKPhysicsContact
对象.最后,你的 Class 应该采用 SKPhysicsContactDelegate
协议.使用 - didBeginContact:
方法检测和处理 SKPhysicsContact
对象.
Then, you set the categoryBitMask
and contactTestBitMask
values to create an SKPhysicsContact
Object on contact. Finally, your Class should adopt the SKPhysicsContactDelegate
protocol. Use the - didBeginContact:
method to detect and handle the SKPhysicsContact
object.
static const uint8_t heroCategory = 1;
static const uint8_t foodCategory = 2;
--
food.physicsBody.categoryBitMask = foodCategory;
food.physicsBody.contactTestBitMask = heroCategory;
food.physicsBody.collisionBitMask = 0;
--
hero.physicsBody.categoryBitMask = heroCategory;
hero.physicsBody.contactTestBitMask = foodCategory;
hero.physicsBody.collisionBitMask = 0;
--
-(void)didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody = contact.bodyA;
SKPhysicsBody *secondBody = contact.bodyB;
}
这篇关于是否可以在 spriteKit 中停用物理体中的碰撞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!