Ios 如果精灵套件中有两个精灵发生碰撞,如何运行操作?

Ios 如果精灵套件中有两个精灵发生碰撞,如何运行操作?,ios,sprite-kit,Ios,Sprite Kit,我想让两个精灵在精灵套件中互相碰撞时消失。苹果文档中的例子对我帮助不大,如果你能解释一下,那就好了。:) 好的,那么你需要做几件事 我假设你有两个精灵 SKSpriteNode *sprite1; SKSpriteNode *sprite2; sprite1.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10]; sprite2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:C

我想让两个精灵在精灵套件中互相碰撞时消失。苹果文档中的例子对我帮助不大,如果你能解释一下,那就好了。:)

好的,那么你需要做几件事

我假设你有两个精灵

SKSpriteNode *sprite1;
SKSpriteNode *sprite2;
sprite1.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
sprite2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(10, 10)];
// this is a couple of examples, you'll have to set yours up to match the sprite size, shapes
它们目前正在您的场景周围飞行,可能会也可能不会相互接触

因此,您需要设置一切以启用命中测试和回调

首先,将物理实体添加到精灵中

SKSpriteNode *sprite1;
SKSpriteNode *sprite2;
sprite1.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
sprite2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(10, 10)];
// this is a couple of examples, you'll have to set yours up to match the sprite size, shapes
接下来,您需要向精灵添加类别。这给了物理引擎一些关于它是什么类型的物体的信息

sprite1.physicsBody.categoryBitMask = 1 << 0;
sprite2.physicsBody.categoryBitMask = 1 << 1;
// again this is an example. You probably want to put these in an NS_OPTIONS typedef.
然后创建方法

- (void)didBeginContact:(SKPhysicsContact *)contact
{
    // these two physics bodies have come into contact!

    SKSpriteNode *firstContactNode = contact.bodyA.node;
    SKSpriteNode *secondContactNode = contact.bodyB.node;

    // now remove them...

    [firstContactNode removeFromParent];
    [secondContactNode removeFromParent];
}

谢谢,真的很有帮助!不用担心,乐意帮忙:)
- (void)didBeginContact:(SKPhysicsContact *)contact
{
    // these two physics bodies have come into contact!

    SKSpriteNode *firstContactNode = contact.bodyA.node;
    SKSpriteNode *secondContactNode = contact.bodyB.node;

    // now remove them...

    [firstContactNode removeFromParent];
    [secondContactNode removeFromParent];
}