ios - 在场景之间传递整数,iphone 游戏 sprite-kit

标签 ios iphone cocoa-touch sprite-kit scene

我有一个用户在游戏中选择关卡的场景,然后我想切换到用户选择关卡的游戏场景。我想创建具有正确级别的游戏场景,但为此我需要将一个整数(级别编号)传递给游戏场景,以便我知道要加载哪个级别。

我的问题是,我该如何做到这一点?我查看了节点的 userdata 属性,但无法弄清楚如何使用它来完成我想要的。任何帮助将不胜感激。

最佳答案

当您在关卡选择场景中调用 [self.view presentScene:scene transition:transition] 时,首先使用关卡编号设置新场景。所以:

//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

您将为计划支持的每个级别执行此操作

关于ios - 在场景之间传递整数,iphone 游戏 sprite-kit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21817840/

相关文章:

ios - 从 iPhone 上的 Exchange IMAP 邮箱中删除邮件

ruby-on-rails - 如何在客户端(浏览器、iPhone、Android)之间共享代码

iphone - 让对象在从 super View 中删除后发送自己的委托(delegate)消息

ios - secondsFromGMT 在设备和模拟器上返回不同的值

iphone - 从后台恢复应用程序会重新启动整个应用程序吗?

ios - 如何使用 RxSwift 在 1 行中显示 3 列 Collection View

iphone - IOS UItableview reloadRowsAtIndexPaths 刷新问题

iphone - 多操作系统与SIngle OS电话和服务器开发

ios - 滚动时 UItableView 弃用单元格

iphone - iPhone 中的 NSMutableDictionary