Sprite Kit,删除Sprite以进行碰撞

use*_*546 2 iphone xcode ios ios7 sprite-kit

我在精灵套件中制作游戏,我对iOS编程很新,我一直在努力获得它,当2个图像碰撞时,一个被删除或变得不可见.我一直非常不成功,并想知道是否有人知道该怎么做?下面是船(始终保留)和要删除的对象之一.

-(void)addShip
{
    //initalizing spaceship node
    ship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
    [ship setScale:0.5];
    ship.zRotation = - M_PI / 2;

    //Adding SpriteKit physicsBody for collision detection
    ship.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:ship.size];
    ship.physicsBody.categoryBitMask = shipCategory;
    ship.physicsBody.dynamic = YES;
    ship.physicsBody.contactTestBitMask = DonutCategory | PizzaCategory | ChocolateCategory | SoftCategory | AppleCategory | GrapeCategory | OrangeCategory | BananaCategory;
    ship.physicsBody.collisionBitMask = 0;
    ship.physicsBody.usesPreciseCollisionDetection = YES;
    ship.name = @"ship";
    ship.position = CGPointMake(260,30);
    actionMoveRight = [SKAction moveByX:-30 y:0 duration:.2];
    actionMoveLeft = [SKAction moveByX:30 y:0 duration:.2];

    [self addChild:ship];
}

- (void)shoot1 //donut
{
    // Sprite Kit knows that we are working with images so we don't need to pass the image’s                extension
    Donut = [SKSpriteNode spriteNodeWithImageNamed:@"1"];
    [Donut setScale:0.15];
    // Position the Donut outside the top
    int r = arc4random() % 300;
    Donut.position = CGPointMake(20 + r, self.size.height + Donut.size.height/2);


    Donut.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Donut.size];
    Donut.physicsBody.categoryBitMask = DonutCategory;
    Donut.physicsBody.dynamic = YES;
    Donut.physicsBody.contactTestBitMask = shipCategory;
    Donut.physicsBody.collisionBitMask = 0;
    Donut.physicsBody.usesPreciseCollisionDetection = YES;

    // Add the Dount to the scene
    [self addChild:Donut];

    // Here is the Magic
    // Run a sequence
    [Donut runAction:[SKAction sequence:@[
    // Move the Dount and Specify the animation time
    [SKAction moveByX:0 y:-(self.size.height + Donut.size.height) duration:5],
    // When the Dount is outside the bottom
    // The Dount will disappear
    [SKAction removeFromParent]]]];
}
Run Code Online (Sandbox Code Playgroud)

Tia*_*ida 8

你必须设置物理世界的代表:

self.physicsWorld.contactDelegate = self;
Run Code Online (Sandbox Code Playgroud)

然后,您有一个在两个对象联系时调用的委托:

- (void)didBeginContact:(SKPhysicsContact *)contact {
    SKPhysicsBody *firstBody, *secondBody;

    if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
    {
        firstBody = contact.bodyA;
        secondBody = contact.bodyB;
    }
    else
    { 
        firstBody = contact.bodyB;
        secondBody = contact.bodyA;
    }

    if ((firstBody.categoryBitMask & projectileCategory) != 0 &&
        (secondBody.categoryBitMask & monsterCategory) != 0)
    {
        //remove the donut and the target
        SKSpriteNode *firstNode = (SKSpriteNode *) firstBody.node;
        SKSpriteNode *secondNode = (SKSpriteNode *) secondBody.node;
        [firstNode removeFromParent];
        [secondNode removeFromParent];
    }
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,您可以跳转到本教程中碰撞部分.