问题:我的HudLayer中的分数没有得到更新。我在GameLayer中包含了HudLayer并更新了分数但分数没有得到更新..
HudLayer在我运行我的应用程序时显示,但在更新分数时,方法未在HudLayer中调用。
让我告诉你的情况,在GameLayer我有两个项目,一个是Pick the Coint,另一个是Bump to wall,当我点击item1时,我将我的分数增加1,当我点击item2时我是递减1.它只是简单的演示程序。
// GameLayer.m
// MyScene
//
// Created by Vaibhav on 1/21/13.
// Copyright 2013 __MyCompanyName__. All rights reserved.
//
#import "GameLayer.h"
#include "IntroLayer.h"
#include "HelloWorldLayer.h"
#include "HudLayer.h"
@implementation GameLayer
@synthesize hud;
+(CCScene *) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
// 'layer' is an autorelease object.
GameLayer *layer = [GameLayer node];
HudLayer *anotherLayer= [HudLayer node];
layer.hud=anotherLayer;
// add layer as a child to scene
[scene addChild: layer];
[scene addChild:anotherLayer];
// return the scene
return scene;
}
-(void)goToLoadScene
{
[[CCDirector sharedDirector]replaceScene:[ HelloWorldLayer node]];
}
-(void)pick
{
[hud addToScore:2];
}
-(void)wall
{
[hud addToScore:-1];
[hud hello];
}
-(id) init
{
if( (self=[super initWithColor:ccc4(0, 0,255,255)])) {
CCMenuItem *item1=[CCMenuItemFont itemFromString:@"End Game" target:self selector:@selector(goToLoadScene)];
item1.position=ccp(120, -120);
CCMenuItem *item2=[CCMenuItemFont itemFromString:@"Pick the Coin" target:self selector:@selector(pick)];
item2.position=ccp(0, 20);
CCMenuItem *item3=[CCMenuItemFont itemFromString:@"Bump to wall" target:self selector:@selector(wall)];
item3.position=ccp(0, -20);
CCMenu *menu=[CCMenu menuWithItems:item1,item2,item3, nil];
[self addChild:menu];
[self addChild:[HudLayer node]];
}
return self;
}
@end
//
// GameLayer.h
// MyScene
//
// Created by Vaibhav on 1/21/13.
// Copyright 2013 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "HudLayer.h"
@interface GameLayer : CCLayerColor {
HudLayer *hud ;
int score;
}
@property(nonatomic,retain) HudLayer *hud;
+(CCScene *) scene;
@end
//
// HudLayer.m
// MyScene
//
// Created by Vaibhav on 1/23/13.
// Copyright 2013 __MyCompanyName__. All rights reserved.
//
#import "HudLayer.h"
@implementation HudLayer
-(id) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super's" return value
if( (self=[super initWithColor:ccc4(255,255, 0,128)]) ) {
NSLog(@"I am here");
self.position=ccp(0,280);
self.contentSize=CGSizeMake(480, 40);
scoreLabel=[CCLabelTTF labelWithString:@"0" fontName:@"Marker Felt" fontSize:24];
scoreLabel.position=ccp(62, 20);
scoreLabel.anchorPoint=ccp(0, 0.5);
[self addChild:scoreLabel];
CCLabelTTF *score1=[CCLabelTTF labelWithString:@"Score" fontName:@"Marker Felt" fontSize:24];
score1.position=ccp(5, 20);
score1.anchorPoint=ccp(0, 0.5);
[self addChild:score1];
// [self addToScore:2];
}
return self;
}
-(void)hello
{
NSLog(@"hello ");
}
-(void)addToScore:(int)number
{
score=score+number;
[scoreLabel setString:[NSString stringWithFormat:@"%d ",score]];
}
@end
//
// HudLayer.h
// MyScene
//
// Created by Vaibhav on 1/23/13.
// Copyright 2013 __MyCompanyName__. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "cocos2d.h"
@interface HudLayer : CCLayerColor {
CCLabelTTF *scoreLabel;
int score;
}
-(void)addToScore:(int)number;
-(void)hello;
@end