碰撞检测后显示结束屏幕

时间:2014-11-06 05:13:39

标签: ios ios7 sprite-kit collision-detection skphysicsbody

我想在我的第一个精灵,一只松鼠和我的第二个雪碧,一个橡子/坚果之间发生碰撞后,有一个GameOverScene。现在,当两个精灵碰撞时,松鼠就会掉到屏幕上。我希望松鼠从屏幕上掉下来但是弹出一个结束屏幕。以下是我到目前为止的情况:

- (instancetype)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
    self.physicsWorld.contactDelegate = self;
    [self buildBackground];
    [self startScrolling];

    [self initializeStartGameLayer];
    [self initializeGameOverLayer];

    _firstPosition = CGPointMake(self.frame.size.width * 0.817f,     self.frame.size.height * .40f);
    _squirrelSprite = [SKSpriteNode spriteNodeWithImageNamed:@"squirrel"];
    _squirrelSprite.position = _firstPosition;
    _atFirstPosition = YES;

    _squirrelSprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
    _squirrelSprite.physicsBody.categoryBitMask = squirrelHitCategory;
    _squirrelSprite.physicsBody.contactTestBitMask = nutHitCategory;
    _squirrelSprite.physicsBody.collisionBitMask =  nutHitCategory;

    _squirrelSprite.physicsBody.affectedByGravity = NO;

    self.physicsWorld.contactDelegate = self;

    [self addChild:_squirrelSprite];

    SKAction *wait = [SKAction waitForDuration:3.0];

    SKAction *createSpriteBlock = [SKAction runBlock:^{
        SKSpriteNode *lightnut = [SKSpriteNode     spriteNodeWithImageNamed:@"lightnut.png"];
        BOOL heads = arc4random_uniform(100) < 50;
        lightnut.position = (heads)? CGPointMake(257,600) : CGPointMake(50,600);

        lightnut.physicsBody = [SKPhysicsBody     bodyWithRectangleOfSize:CGSizeMake(200,160)];

        lightnut.physicsBody.categoryBitMask = nutHitCategory;
        lightnut.physicsBody.contactTestBitMask = squirrelHitCategory;
        lightnut.physicsBody.collisionBitMask =  squirrelHitCategory;

        lightnut.physicsBody.affectedByGravity = NO;

        self.physicsWorld.contactDelegate = self;

        [self addChild: lightnut];

        SKAction *moveNodeUp = [SKAction moveByX:0.0 y:-700.0 duration:1.3];
        [lightnut runAction: moveNodeUp];
    }];

    SKAction *waitThenRunBlock = [SKAction sequence:@[wait,createSpriteBlock]];

    [self runAction:[SKAction repeatActionForever:waitThenRunBlock]];

}
return self;
}

我尝试使用本教程从此页面开始结束并启动屏幕:http://mytechspace.com/2014/02/tutorial-how-to-develop-flappy-bird-game-in-ios-using-spritekit-part2-of-2.html但是我的开始或结束屏幕都没有出现(我不确定是什么阻止它)。

这是我的联系代表方法:

-(void)didBeginContact:(SKPhysicsContact *)contact
{ 

SKPhysicsBody *firstBody, *secondBody;

firstBody = contact.bodyA;
secondBody = contact.bodyB;

if(firstBody.categoryBitMask == squirrelHitCategory || secondBody.categoryBitMask == nutHitCategory)
{

}

谢谢!

1 个答案:

答案 0 :(得分:2)

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

    firstBody = contact.bodyA;
    secondBody = contact.bodyB;

    if(firstBody.categoryBitMask == squirrelHitCategory || secondBody.categoryBitMask == nutHitCategory)
    {
        [self gameOver];
    }
}

- (void)gameOver
{
    GameOverScene *gameOverScene = [GameOverScene sceneWithSize:self.size];
    [self.view presentScene:gameOverScene transition:[SKTransition pushWithDirection:SKTransitionDirectionLeft duration:0.5]];
}