2009-09-08 5 views
0

나는 코코스 2D로 자신의 GUI 구성 요소를 만들려고했습니다. Sprite를 확장 한 클래스를 작성했습니다. 이 클래스는 그래픽과 레이블로 초기화합니다. 클래스의 인스턴스를 만들면 구성 요소를 볼 수 있지만 어떤 함수도 사용할 수 없습니다. setLabel 함수를 작성했지만 레이블이 변경되었습니다. 자신의 구성 요소 ... 기능이 작동하지 않습니다

는이 코드로 인스턴스를 생성 :이 인스턴스 기능에 액세스 할 필요가 있기 때문에이 alloc를 제거 할 수 없습니다

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 

.

그것은 문제처럼 보인다
@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<CocosAnimation>) anim 
{ 
    // lazy alloc 
    if(! animations) 
     [self initAnimationDictionary]; 

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

답변

0

dropDown.minit 기능에 있습니다

다음은 원래적인 Cocos2D Sprite 클래스입니다. addChild: 인수에는 [[self alloc] initActiveState]][[self alloc] initLabel]]을 호출하지만 실제로 수행하는 작업은 전체 개체의 공간을 다시 할당하는 것입니다. 대신 initActiveStateinitLabel 방법으로 할당해야합니다. 해당 번호를 [ self initActiveState ][ self initLabel ] (alloc 전화 번호는 제외)로 변경하고 그 번호가 어디 있는지 확인하십시오.

코드를 Sprite 클래스에 게시하는 것이 유용 할 수 있습니다.이 클래스에서 상속 된 메서드를 사용하고 있기 때문입니다.

관련 문제