只是想知道是否有人可以快点帮我一把。我正在构建一个简单的拼图滑块游戏,我现在为游戏添加了一个计时器,我想要做的就是将完成拼图所需的时间传递到恭喜场景。任何人都可以看看我的代码,看看我哪里出错了吗?
play.h
#import "cocos2d.h"
#import "Box.h"
@interface PlayLayer : CCLayer
{
int timeInt;
int secs;
int mins;
CCLabel *timeLabel;
NSString *TotalTimeString;
}
@property (nonatomic, assign) int timeInt;
@property (nonatomic, assign) int secs;
@property (nonatomic, assign) int mins;
@property (nonatomic, retain) NSString *TotalTimeString;
@end
Play.m
#import "PlayLayer.h"
#import "Congrats.h"
@implementation PlayLayer
@synthesize timeInt;
@synthesize secs;
@synthesize mins;
@synthesize TotalTimeString;
-(id) init{
self = [super init];
TotalTimeString = [NSString stringWithFormat:@"Time: %02d:%02d", mins, secs];
timeLabel = [[CCLabel labelWithString:TotalTimeString dimensions: CGSizeMake(130,27) alignment: UITextAlignmentCenter fontName:@"Marker Felt" fontSize:25.0] retain];
timeLabel.position = ccp(155,430);
[self addChild:timeLabel];
[self schedule: @selector(tick2:) interval:1.0];
return self;
}
-(void) tick2: (id) sender{
timeInt++;
secs = timeInt % 60;
mins = timeInt / 60;
[timeLabel setString: TotalTimeString];
}
-(void) check: (id) sender data: (id) data{
int valueToCheck = 0;
bool breakOut = false;
for (int y=0; y < box.size.height && !breakOut; y++) {
for (int x=0; x < box.size.width && !breakOut; x++) {
Tile *tile = [box objectAtX:x Y:y];
if (tile.check != [box hashOfXY:x y:y]) breakOut = true;
valueToCheck++;
}
}
int totalTiles = box.size.width * box.size.height;
if (valueToCheck == totalTiles){
[[CCDirector sharedDirector] replaceScene:[CCFadeTransition transitionWithDuration:1 scene:[Congrats node]]];
CCScene *scene = [Congrats scene];
scene.TotalTimeTakenWhenDiedString = TotalTimeString;
[[CCDirector sharedDirector] replaceScene:scene];
}
}
@end
Congrats.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "PlayLayer.h"
@interface Congrats : CCLayer {
CCLabel *timeLabel;
NSString *TotalTimeTakenWhenDiedString;
}
@property (nonatomic, assign) NSString *TotalTimeTakenWhenDiedString;
+(id) scene;
@end
Congrats.m
#import "Congrats.h"
#import "DifficultyLevel.h"
#import "PlayLayer.h"
#import "PlayLayerMedium.h"
#import "PlayLayerHard.h"
#import "MainMenu.h"
@implementation Congrats
@synthesize TotalTimeTakenWhenDiedString;
+(id) scene {
// ‘scene’ is an autorelease object.
CCScene *scene = [CCScene node];
// ‘layer’ is an autorelease object.
Congrats *layer = [Congrats node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
// on “init” you need to initialize your instance
-(id) init {
// always call “super” init
// Apple recommends to re-assign “self” with the “super” return value
if( (self=[super init] )) {
timeLabel = [[CCLabel labelWithString: TotalTimeTakenWhenDiedString dimensions: CGSizeMake(130,27) alignment: UITextAlignmentCenter fontName:@"Marker Felt" fontSize:25.0] retain];
timeLabel.position = ccp(155,430);
}
return self;
}
-(void) goToGameplay: (id) sender {
[[CCDirector sharedDirector] replaceScene:[CCFadeTransition transitionWithDuration:1 scene:[MainMenu node]]];
}
}
@end
我有点希望,那会有用。但我不断得到这个错误“错误:请求成员'TotalTimeTakenWhenDiedString'在某个结构或联合的东西中”
任何人都有帮助吗?
干杯
答案 0 :(得分:0)
你的问题在这里:
CCScene *scene = [Congrats scene];
scene.TotalTimeTakenWhenDiedString = TotalTimeString;
对象scene
属于CCScene
,您尝试访问TotalTimeTakenWhenDiedString
。但这是Congrats
的属性,而不是CCScene
的属性。
通过从Congrats
获取scene
对象,这是一个可行的建议。 (警告:我不熟悉cocos2d,我只是很快查看here。这可能是一种可怕的方式!)
在此处更改:
// add layer as a child to scene
[scene addChild:layer z:0 tag:1]; // tag it with 1
在这里:
CCScene *scene = [Congrats scene];
Congrats *congrats = (Congrats *)[scene getChildByTag:1]; // get the congrats child object, which we tagged 1
congrats.TotalTimeTakenWhenDiedString = TotalTimeString;
[[CCDirector sharedDirector] replaceScene:scene];
答案 1 :(得分:0)
...为什么不创建一个可以用作全局对象的单例类?并在游戏中的任何地方访问/更新它?