在场景之间传递整数,iphone游戏精灵套件
我有一个场景,用户在游戏中select关卡,然后我想切换到用户select关卡的游戏场景。 我想用正确的级别创build游戏场景,但是要做到这一点,我需要传递一个整数,这是游戏场景的关卡号,所以我知道要装入哪个级别。
我的问题是,我如何做到这一点? 我已经看过节点的userdata属性,但无法弄清楚如何使用它来完成我想要的。 任何帮助将深表谢意。
当您在级别select场景中调用[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
你会为你打算支持的每个级别做这个
// First Scene NSUserDefaults.standardUserDefaults().setInteger(12345, forKey:"SCORE") // Second Scene if let myScore: AnyObject = NSUserDefaults.standardUserDefaults().objectForKey("SCORE") { println(myScore) finalScore = myScore as IntegerLiteralType }