views:

136

answers:

2

hi,

i tryed to create a own GUI component with cocos 2d... i haved written a class (extends Sprite) ... this class initialize with graphics and a lable... if I create an instance of my class .. i can see my component but i can user any functions... i wrote a setLabel function .. but the lable change...

i create a instance with this code:

drop1 = [DropDown init];

//here I call the function but it dont works :-(
[drop1 setCaption:@"Produkt"];

///dropDown.m:

#import "DropDown.h"

@implementation DropDown

@synthesize captionLbl;
+ (id) init
{
if( (self=[super init] ))
{
return [[[self spriteWithFile:@"menue_dropdownbtn.png"] addChild:[[self alloc] initActiveState]] addChild:[[self alloc] initLabel]];
}
}

- (id)initActiveState
{
activated = [Sprite spriteWithFile:@"menue_dropactiveated.png"];
[activated setAnchorPoint:ccp(0,0)];
[activated setPosition:ccp(173,0)];
[activated setVisible:NO];
return activated;
}

- (id)initLabel
{
captionLbl = [Label labelWithString:@"Text" fontName:@"Arial" fontSize:14.0f];
[captionLbl setAnchorPoint:ccp(0,0)];
[captionLbl setPosition:ccp(10,5)];
[captionLbl setRGB:0 :0 :0];
return captionLbl;
}

- (void)setCaption:(NSString*)text
{
[captionLbl setString:text];
NSLog(@"Hallooooo");
}

- (void)activate
{
[activated setVisible:YES];
isActive = YES;
}

- (void)deactivate
{
[activated setVisible:NO];
isActive = NO;
}

@end

// DropDown.h
//

//

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface DropDown : Sprite
{
BOOL isActive;

@private
Sprite* activated;
Label* captionLbl;

}

@property (nonatomic, retain) Label* captionLbl;

+ (id) init;
- (id)initActiveState;
- (id)initLabel;
- (void)setCaption:(NSString*)text;
- (void)activate;
- (void)deactivate;
@end
A: 

It looks like the problem is in your init function in dropDown.m. You call [[self alloc] initActiveState]] and [[self alloc] initLabel]] for the addChild: arguments, but what that's actually doing is reallocating space for the entire object. Instead, you should do the allocation in your initActiveState and initLabel methods. Try changing those to [ self initActiveState ] and [ self initLabel ] (omitting the alloc call) and see where that gets you.

It might help to post the code to your Sprite class, as you're using methods that are inherited from it.

Jeff Kelley
A: 

i cant remove the "alloc" .. because it is needed to access the instance functions.. :-/

Here the original cocos2d Sprite class:

@interface Sprite (Private) // lazy allocation -(void) initAnimationDictionary; @end

@implementation Sprite

pragma mark Sprite - image file

  • (id) spriteWithFile:(NSString*) filename { return [[[self alloc] initWithFile:filename] autorelease]; }

  • (id) initWithFile:(NSString*) filename { self = [super init]; if( self ) { // texture is retained self.texture = [[TextureMgr sharedTextureMgr] addImage: filename];

    // lazy alloc
    animations = nil;
    

    }

    return self; }

pragma mark Sprite - CGImageRef

  • (id) spriteWithCGImage: (CGImageRef) image { return [[[self alloc] initWithCGImage:image] autorelease]; }

  • (id) initWithCGImage: (CGImageRef) image { self = [super init]; if( self ) { // XXX: possible bug. See issue #349. New API should be added NSString *key = [NSString stringWithFormat:@"%08X",(unsigned long)image]; self.texture = [[TextureMgr sharedTextureMgr] addCGImage:image forKey:key];

    // lazy alloc
    animations = nil;
    

    }

    return self; }

pragma mark Sprite - Texture2D

  • (id) spriteWithTexture:(Texture2D*) tex { return [[[self alloc] initWithTexture:tex] autorelease]; }

  • (id) initWithTexture:(Texture2D*) tex { if( (self = [super init]) ) { // texture is retained self.texture = tex;

    // lazy alloc
    animations = nil;
    

    } return self; }

pragma mark Sprite

-(void) dealloc { [animations release]; [super dealloc]; }

-(void) initAnimationDictionary { animations = [[NSMutableDictionary dictionaryWithCapacity:2] retain]; }

// // CocosNodeFrames protocol // -(void) setDisplayFrame:(id)frame { self.texture = frame; }

-(void) setDisplayFrame: (NSString*) animationName index:(int) frameIndex { if( ! animations ) [self initAnimationDictionary];

Animation *a = [animations objectForKey: animationName];
Texture2D *frame = [[a frames] objectAtIndex:frameIndex];
self.texture = frame;

}

-(BOOL) isFrameDisplayed:(id)frame { return texture_ == frame; } -(id) displayFrame { return texture_; } -(void) addAnimation: (id) anim { // lazy alloc if( ! animations ) [self initAnimationDictionary];

[animations setObject:anim forKey:[anim name]];

} -(id)animationByName: (NSString*) animationName { NSAssert( animationName != nil, @"animationName parameter must be non nil"); return [animations objectForKey:animationName]; } @end

MRaguse