我不确定我用“模态 sprite-kit 场景”的措辞是否正确,但我想做的是在游戏完成时在场景上出现一个较小尺寸的场景。随附的屏幕截图显示了我的意思:
截图来自 Flappy Bird,当玩家死亡时,游戏结束的小场景几乎像模态效果一样弹出,并显示用户的最终结果。我想知道如何去创建这个。
我尝试在游戏完成时调用它:
[self.player runAction:death completion:^{
[self removeAllActions];
GameOverNode *gameOverNode = [[GameOverNode alloc] initWithScore:self.size];
gameOverNode.gameScene = self;
gameOverNode.position = CGPointMake(self.scene.size.width/2, -150);
[self addChild:gameOverNode];
[gameOverNode runAction:[SKAction moveToY:self.scene.size.height/2 duration:0.6]];
这是 gameOverScene.m 文件中 gameOverNode 的代码:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
self.userInteractionEnabled = YES;
self.zPosition = 20;
SKSpriteNode *bg = [SKSpriteNode spriteNodeWithColor:[UIColor blackColor] size:CGSizeMake(280*DoubleIfIpad, 300*DoubleIfIpad)];
bg.alpha = 0.55;
但是该节点仅显示在屏幕的左下角,而不是像我想要的那样显示在中间。
我做错了什么?我怎样才能让小游戏结束节点弹出并坐在场景中间。
最佳答案
你根本做不到。根据 Apple Docs 和多个 StackOverflow 帖子,您不能同时运行两个 SKScene。要重新创建您想要的效果,您可以创建一个边框和线条颜色与您想要的相似的SKShapeNode
,并在其中放置分数等对象,或者您可以为弹出的屏幕创建一个图像,只需将 SKLabelNode
添加到显示最高分和当前分数的场景中。我附上了一张图片,展示了我如何为我的应用程序 Average Guy 创建最终“ View ”。
我使用这样的 SKShapeNode 制作了弹出屏幕:
SKShapeNode *optionsMenu = [SKShapeNode node];
CGRect tempRect = CGRectMake(SELF_WIDTH/2, SELF_HEIGHT/2, SELF_WIDTH-20, (finalScoreLabel.position.y+(finalScoreLabel.frame.size.height/2)) - (mainMenuButton.position.y-(mainMenuButton.frame.size.height/2)) + 20);
CGPathRef tempPath = CGPathCreateWithRoundedRect(CGRectMake(tempRect.origin.x - tempRect.size.width/2, tempRect.origin.y - tempRect.size.height/2, tempRect.size.width, tempRect.size.height), 5, 5, Nil);
optionsMenu.path = tempPath;
optionsMenu.fillColor = [SKColor grayColor];
optionsMenu.strokeColor = [SKColor blueColor];
optionsMenu.zPosition = 30;
optionsMenu.glowWidth = 2.0f;
CGPathRelease(tempPath);
当然,不要介意繁琐的 CGRect 实现,我想让所有内容都适合所有屏幕尺寸,所以我没有对任何位置进行硬编码。至于添加乐谱和按钮,我是这样做的:
SKLabelNode *restartButton = [SKLabelNode labelNodeWithFontNamed:@"00 Starmap Truetype"];
restartButton.text = @"Play Again";
restartButton.zPosition = 31;
restartButton.fontColor = [SKColor blueColor];
restartButton.fontSize = 30;
restartButton.position = CGPointMake(HALF_WIDTH, HALF_HEIGHT-50);
restartButton.name = @"restart_button";
SKLabelNode *restartButtonShadow = restartButton.copy;
restartButtonShadow.position = CGPointMake(restartButton.position.x+1, restartButton.position.y-1);
restartButtonShadow.color = [SKColor colorWithRed:0 green:0 blue:.5 alpha:.1];
SKLabelNode *mainMenuButton = [SKLabelNode labelNodeWithFontNamed:@"00 Starmap Truetype"];
mainMenuButton.text = @"Main Menu";
mainMenuButton.zPosition = 31;
mainMenuButton.fontColor = [SKColor blueColor];
mainMenuButton.fontSize = 30;
mainMenuButton.position = CGPointMake(HALF_WIDTH, HALF_HEIGHT - 85);
mainMenuButton.name = @"main_menu_button";
关于ios - 如何制作这样的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23448459/