在场景之间传递整数,iphone游戏sprite-kit [英] Passing integer between scenes, iphone game sprite-kit

查看:155
本文介绍了在场景之间传递整数,iphone游戏sprite-kit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个场景,其中用户在游戏中选择级别,然后我想切换到具有用户选择的级别的游戏场景。我想创建游戏场景与正确的水平,但要做到这一点,我需要传递一个整数,这是水平数字到游戏场景,所以我知道加载哪个级别。

I have a scene where the user chooses level in a game, then i want to switch to a game scene with the level the user chose. I want to create the game scene with the right level but to do this i need to pass an integer which is the level-number to the game scene so i know which level to load.

我的问题是,我该如何做到这一点?我已经看了节点的userdata属性,但无法弄清楚如何使用它来完成我想要的。

My question is, how do i accomplish this? I have looked at the userdata property of nodes but could not figure out how to use it to accomplish what i want. Any help would be deeply appreciated.

推荐答案

当你调用 [self.view presentScene:scene transition:转换] 在级别选择场景中,首先使用级别编号设置新场景。 SO:

When you call [self.view presentScene:scene transition:transition] inside of your level selection scene, first set up your new scene with the level number. SO:

//GameScene.h
//This is the public interface for your game scene
@import SpriteKit;
@interface GameScene : SKScene
@property (nonatomic, assing)NSInteger levelNumber;
@end

然后

//Level Selection Scen
//Select.m
#import "GameScene.h"
... Rest of your code
User selects level and you prepare for transition and call
- (void)transitionToLevel:(NSInteger)level{
    GameScene *destinationScene = [[GameScene alloc] initWithSize:self.size];
    destinationScene.levelNumber = level;
    SKTransition *transition = //Set up transition here
    [self.view presentScene:destinationScene transition:transition];
}    

在您的游戏场景的实现

//GameScene.m
#import "GameScene.h"
@implementation GameScene
-(id)initWithSize:(CGSize){
    if (self.levelNumber == 1){
        [self playLevelOne]; //Where this is where you play your game
    }
    else if (self.levelNumber == 2){
        [self playLevelTwo];
    }
}
- (void)playLevelOne{
    //Level Implementation
}
- (void)playLevelTwo{
    //Level Implementation
}
@end

以支持

这篇关于在场景之间传递整数,iphone游戏sprite-kit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆