开发者

Pass level time to Congratulations scene cocos2d

Just wondered if someone could quickly give me a hand. Im building a simple puzzle slider game, ive added a timer to the game now and all i want to do is pass the time it took to complete the puzzle over to the congrats scene. Could anyone just have a look at my code see where im going wrong please?

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 f开发者_运维问答ontName:@"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

I was kind of hoping that, that would work. But i keep getting this error "error: request for member 'TotalTimeTakenWhenDiedString' in something not a structure or union"

Anyone help at all?

Cheers


Your problem is here:

CCScene *scene = [Congrats scene];
scene.TotalTimeTakenWhenDiedString = TotalTimeString;

The object scene is of type CCScene and you are trying to access TotalTimeTakenWhenDiedString on it. But that is a property of Congrats, not of CCScene.


Here's a suggestion which might work, by getting the Congrats object out from scene. (Caveat: I'm not familiar with cocos2d, and I only looked it up quickly here. This may be a terrible way to be doing it!)

Change here:

// add layer as a child to scene
[scene addChild:layer z:0 tag:1]; // tag it with 1

and here:

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];


... Why dont you just create a singleton class which you can use as a global object? and access/update it anywhere in the game?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜