我有 3 个类,即 GameScene、HomeScene 和 RecordsScene,我试图将一个整数值从 GameScene 传递给 RecordScene。我的问题是,一旦游戏完成,它就会回家,然后你必须点击记录才能看到记录。基本上,这三个类的顺序是这样的:
GameScene --> HomeScene --> RecordScene
我一直在尝试做的是将 GameScene 的整数值传递给 HomeScene,然后从 HomeScene 传递给 RecordScene,但是当我到达那里时,整数的值仍然是 0。这是我当前的代码:
//GameScene
-(void)goHome:(ccTime)dt
{
HomeScene *home = [HomeScene node];
//myInt is an integer I declared on the HomeScene class, the value changes depending on the stage
home.layer.myInt = 1;
[[CCDirector sharedDirector]replaceScene:home];}
//HomeScene
-(void)viewRecord:(id)sender
{
//View record
RecordScene *record = [RecordScene node];
//lastStage is the integer from RecordScene I'm passing the value of myInt into
record.layer.lastStage = myInt;
[[CCDirector sharedDirector]replaceScene:record];}
我正在实现这样的类:
@implementation HomeScene
@synthesize layer;
-(id)init
{
if((self =[super init]))
{
self.layer = [HomeSceneLayer node];
[self addChild:layer];
}
return self;
}
@end
@implementation HomeSceneLayer
@synthesize myInt;
//methods
@end
然后我的标题看起来像这样:
@interface HomeScene : CCLayer
{
int myInt;}
@property(nonatomic)int myInt;
@end
@interface HomeScene : CCScene
{
HomeSceneLayer *layer;
}
@property (nonatomic, retain)HomeSceneLayer *layer;
@end
为什么它会重置回 0 以及如何将整数值从 GameScene 传递到 RecordScene?