gpt4 book ai didi

ios - 碰撞检测后显示结束屏幕

转载 作者:行者123 更新时间:2023-11-28 21:55:23 25 4
gpt4 key购买 nike

在我的第一个 Sprite (一只松鼠)和我的第二个 Sprite (一个橡子/坚果)发生碰撞后,我试图拥有一个 GameOverScene。现在,当两个 Sprite 碰撞时,松鼠刚从屏幕上掉下来。我想让松鼠从屏幕上掉下来,但要弹出一个结束屏幕。这是我目前所拥有的:

- (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但我的开始或结束屏幕都没有出现(我不确定是什么阻止了它)。

这是我的联系委托(delegate)方法:

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

SKPhysicsBody *firstBody, *secondBody;

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

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

}

谢谢!

最佳答案

-(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]];
}

关于ios - 碰撞检测后显示结束屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26772176/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com