Reputation: 721
Once again I need your Help, am thanking you as every time you helped me.
I am developing Game in iPhone using 'COCOS2D' framework and 'OBJECTIVE-C'. In my game there is Ten(10) Levels. The LEVEL Label is created and defined in a class ( i:e HudLayer.h, HudLayer.m ) and I am removing LABEL in another class (i:e GameScene.m ), In the game I am using ccLabelBMFont for creating LEVEL label.
Now the Problem is when I am Removing LEVEL label (i:e Level 1) and placing new LEVEL label (i:e Level 2), the code is not removing "Level 1" and placing "Level 2" on the previous LEVEL label (i:e Level 1 ).
Below is my Code for the above same issue, please tell where I am going Wrong :
**Code of HudLayer.h**
#import "Foundation/Foundation.h"
#import "cocos2d.h"
#import "GameScene.h"
@interface HudLayer : CCLayer {
CCLabelBMFont * level;
}
@property (nonatomic,retain) CCLabelBMFont * level;
@end
Now code for "HudLayer.m" where I am synthethising and using 'level':
#import "HudLayer.h"
#import "GameScene.h"
@implementation HudLayer
@synthesize level;
level = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Level 1",level] fontName:@"Marker Felt" fontSize:12];
[level setAnchorPoint:ccp(1,0.5)];
[level setPosition:ccp(250,470)];
[level setColor:ccORANGE];
[self addChild:level];
}
return self;
}
- (void) dealloc {
[super dealloc];
[lives release];
}
@end
*HERE IS MY CODE FOR "GameScene.h"*
#import "cocos2d.h"
#import "HudLayer.h"
typedef enum {
Level1,
Level2,
Level3,
Level4,
Level5,
Level6,
Level7,
Level8,
Level9,
Level10,
} LevelType;
@interface GameLayer : CCLayer
{
int level;
BOOL ifLevel2Started;
BOOL ifLevel3Started;
BOOL ifLevel4Started;
BOOL ifLevel5Started;
BOOL ifLevel6Started;
BOOL ifLevel7Started;
BOOL ifLevel8Started;
BOOL ifLevel9Started;
BOOL ifLevel10Started;
}
@property (assign,readwrite) int level;
@end
AND FINALLY I WANT TO REMOVE THE LEVEL label 1 From HudLayer Class and palce new Label with Name "Level 2" on the basis of Score points:
#import "GameScene.h"
#import "HudLayer.h"
@implementation GameLayer
@synthesize level;
@synthesize levelType = _levelType;
if (self.levelType == Level1) {
[self LevelFeatures];
ifLevel2Started = FALSE;
}
if (self.levelType == Level2) {
[self LevelFeatures1];
ifLevel3Started = FALSE;
}
if (self.levelType == Level3) {
[self LevelFeatures2];
ifLevel4Started = FALSE;
}
-(void)LevelFeatures
{
HudLayer * hl = (HudLayer *)[self getChildByTag:KHudLayer]; // * HERE I AM DOING MY MAIN STEP, HERE AM CREATING OBJECT OF **"HudLayer" class** *
hl.level = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"Level 2"] fontName:@"Marker Felt" fontSize:12];
//**HERE AM SETTING NEW Label FOR LEVEL.**
}
I AM FINDING PROBLEM ONLY ON THE ABOVE LINE OF CODE FOR WHICH I WROTE SO BIG QUERY, SO THAT YOU CAN UNDERSTAND MY PROBLEM EXACTLY.
WHAT I SHOULD WRITE/CODE OVER HERE SO THAT PREVIOUS LABEL COMPLETELY REMOVE AND NEW ONE PLACE OVER IT. (i:e No Overloading of Label happen).
Upvotes: 1
Views: 286
Reputation: 2201
when you syntesize the label do this:
levelLbl = [CCLabelTTF labelWithString:@"" fontName:@"Marker Felt" fontSize:12];
and when you set the label in LevelFeatures just use "setString:" like so:
[hl.levelLbl setString:[NSString stringWithFormat:@"Level %i",level]];
Upvotes: 1
Reputation: 9079
hmmm , there is a chunk of code missing at the start of your implementation snippet, but I would venture this (with minimal change to your current setup) :
-(void)LevelFeatures {
HudLayer * hl = (HudLayer *)[self getChildByTag:KHudLayer];
[hl removeChild:h1.level cleanup:YES];
hl.level=[CCLabelTTF labelWithString:[NSString stringWithFormat:@"Level 2"]
fontName:@"Marker Felt"
fontSize:12];
[hl addChild:hl.level];
}
I am not certain why you are retaining level in HudLayer, but just adding as a child to any CCNode descendant will retain it for you. Also, with this kind of code i tend to use tags to regain access to children I add to a CCNode descendant.
Upvotes: 1