如何在Cocos2d中重置场景?

时间:2014-05-26 16:55:11

标签: ios objective-c cocos2d-iphone

第二次启动时,如何重置场景(重新开始倒计时并将分数设为0)?第二次加载场景时,不会调用initWithParameters和onStart。

Cocos2D游戏:

  -(id) initWithParameters:(NSArray *) parameters
{
    NSLog(@"init TheButton with parameters");

    // Apple recommend assigning self with supers return value
    self = [super init];
    if (!self) return(nil);

   gameParameters = parameters;

    // Enable touch handling on scene node
    [self setIsTouchEnabled:YES];

    // Create a colored background (Dark Grey)
    //CCNodeColor *background = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.6f green:0.6f blue:0.6f alpha:1.0f]];
    //[self addChild:background];

    CCMenuItem *starMenuItem = [CCMenuItemImage
                                itemFromNormalImage:@"ButtonStar.png" selectedImage:@"ButtonStarSel.png"
                                target:self selector:@selector(increasePoints:)];
    CGSize size = [[CCDirector sharedDirector] winSize];
    starMenuItem.anchorPoint = ccp(0.5f,0.5f);
    starMenuItem.position = ccp(size.width/2, size.height/2);
    CCMenu *starMenu = [CCMenu menuWithItems:starMenuItem, nil];
    starMenu.position = CGPointZero;
    [self addChild:starMenu];

    timeLabel = [CCLabelTTF labelWithString:@"0.00" fontName:@"Arial" fontSize:18.0f];
    //timeLabel.positionType = CCPositionTypeNormalized;
    timeLabel.position = ccp(30,size.height-10);
    // Add the time label
    [self addChild:timeLabel];
    NSLog(@"time: %@", [gameParameters objectAtIndex:0]);

    clicksLabel = [CCLabelTTF labelWithString:@"0" fontName:@"Arial" fontSize:18.0f];
    //clicksLabel.positionType = CCPositionTypeNormalized;
    clicksLabel.position = ccp(size.width - 20,size.height-10);
    // Add the time label
    [self addChild:clicksLabel];

    [self startGame];

    return self;
}


-(void) startGame
{
    clicks = 0;
    myTime = [[gameParameters objectAtIndex:0] intValue];

    [self schedule:@selector(update:)];

}

场景(cocos2d游戏)集成在故事板项目中。当它完成(倒计时到达0)时,它会加载一个新的viewcontroller。当用户按下“再次播放”按钮时,我想从这个视图控制器再次加载游戏。发生的事情是场景正在加载(我用unwind segue实现它)但是倒计时和得分都没有重置,我该怎么做?

enter image description here

编辑: 我终于从CocosViewController重新加载并重置场景,但是当我在游戏结束时调用performSegueWithIdentifier时它没有加载viewcontroller。方法viewdidload被调用但它没有显示任何内容。

Cocos2dGame:

-(void) onExit
{
[super onExit];

//Send parameters
self.gameTracking = [[NSMutableArray alloc] initWithObjects:@"THE",@"QUICK", @"BROWN", @"FOX",@"FOO", nil];
NSDictionary * userInfo = [[NSMutableDictionary alloc] initWithObjectsAndKeys: [NSString stringWithFormat:@"%d",clicks], @"score", self.gameTracking, @"gameTracking", nil];

//Send a notification to the CocosViewController when the game is finished to load the new view controller
[[NSNotificationCenter defaultCenter] postNotificationName:@"GameEndedNotification" object:self userInfo:userInfo];

NSLog(@"game exit");

//[[CCDirector sharedDirector] end];

}

Cocos2DViewController

 //Receive notification when the game finished
-(void)receiveNotificationFromGame:(NSNotification *) notification
{
    if ([[notification name] isEqualToString:@"GameEndedNotification"])
    {
        NSLog (@"Successfully received the test notification! %@", [self class]);
        NSLog(@"score: %d", [notification.userInfo[@"score"] intValue]);

    //get variables
    try.score = [notification.userInfo[@"score"] intValue];
    NSLog(@"score: %d", try.score);

    try.gameTracking = notification.userInfo[@"gameTracking"];

    //show the next view
    [self performSegueWithIdentifier:@"sg_showScore" sender:self];
}

}

0 个答案:

没有答案