0

我有 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?

4

1 回答 1

1

@synthesize myInt;创建一个 iVar _myInt

RecordScene *record = [RecordScene node];
//lastStage is the integer from RecordScene I'm passing the value of myInt into
record.layer.lastStage = myInt;

您正在设置record.layer.lastStage为 myInt(HomeScene 的 iVar)。尝试record.layer.lastStage = self.layer.myInt;

来自 Apple 的编码指南https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CodingGuidelines/Articles/NamingIvarsAndTypes.html

确保实例变量的名称简洁地描述了存储的属性。通常,您不应直接访问实例变量,而应使用访问器方法(您确实在 init 和 dealloc 方法中直接访问实例变量)。为了帮助表明这一点,请在实例变量名称前加上下划线 (_),例如:

Xcode 现在(我不知道从什么时候开始)也会自动合成属性,将 iVar 命名为@property [...] foo _foo.

于 2013-02-06T05:01:10.200 回答