【问题标题】:own component... functions wont work自己的组件...功能不起作用
【发布时间】:2009-09-08 13:53:39
【问题描述】:

我尝试用 cocos 2d 创建一个自己的 GUI 组件。我写了一个类(扩展了 Sprite)。此类使用图形和标签进行初始化。如果我创建我的类的一个实例,我可以看到我的组件,但我不能使用任何功能。我写了一个setLabel 函数但是标签改变了。

我用这段代码创建了一个实例:

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 

我无法删除alloc,因为它是访问实例函数所必需的。

这里是原来的 cocos2d Sprite 类:

@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

【问题讨论】:

    标签: iphone objective-c components cocos2d-iphone


    【解决方案1】:

    看起来问题出在dropDown.m 中的init 函数中。您为 addChild: 参数调用 [[self alloc] initActiveState]][[self alloc] initLabel]],但实际上所做的是为整个对象重新分配空间。相反,您应该在 initActiveStateinitLabel 方法中进行分配。尝试将它们更改为 [ self initActiveState ][ self initLabel ](省略 alloc 调用),看看会怎样。

    将代码发布到您的 Sprite 类可能会有所帮助,因为您正在使用从它继承的方法。

    【讨论】:

      猜你喜欢
      • 2014-11-19
      • 2022-01-10
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 2016-12-30
      • 2014-08-15
      • 1970-01-01
      相关资源
      最近更新 更多