是否可以在spriteKit中停用物理实体中的碰撞? [英] Is it possible to deactivate collisions in physics bodies in spriteKit?

查看:217
本文介绍了是否可以在spriteKit中停用物理实体中的碰撞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在我的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 SKPhysicsBody 类中的contactTestBitMaskrel =nofollow noreferrer> contactTestBitMask

Check out collisionBitMask, categoryBitMask, and contactTestBitMask in the SKPhysicsBody class.

基本上,具有相同 collisionBitMask 值的物理主体将相互传递。

Essentially, physics bodies with the same collisionBitMask value will "pass-through" each other.


  • 更正: 如果类别和碰撞位 匹配 ,他们会互动。如果他们 不匹配 ,那么 会互动。如果碰撞位和类别位 ,当然该项 无任何交互

  • Correction: If the category and collision bits match, they will interact. If they do not match, those two will not interact. And if the collision bits, and category bits, are both zero, of course that item will interact with nothing whatsoever.

然后,设置 categoryBitMask contactTestBitMask 在联系人中创建 SKPhysicsContact 对象的值。最后,您的班级应采用 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆