Cocos2d移动后无法选择菜单项

时间:2012-02-12 16:32:23

标签: iphone xcode4 menu cocos2d-iphone

我试图创建一个下拉菜单的幻觉,但在我移动子菜单项后,我不能再选择它们了吗?

这里是我的整个代码:

#import "HelloWorldLayer.h"


CCMenuItem *playDown;
CCMenuItem *playUp;
CCMenuItemToggle *play;
CCMenuItem *help;
CCMenuItem *options;
int down;

// HelloWorld implementation
@implementation HelloWorldLayer

+(id) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
-(id) init {
    // always call "super" init
    // Apple recommends to re-assign "self" with the "super" return value
    if( (self=[super init])) 
    {
        self.isTouchEnabled = TRUE;

        [CCMenuItemFont setFontSize:70];
        playDown = [CCMenuItemFont itemFromString:@"Play" target:self selector:@selector(playDown:)];
        playUp = [CCMenuItemFont itemFromString:@"Play" target:self selector:@selector(playUp:)];
        play = [CCMenuItemToggle itemWithTarget:self selector:@selector(playDown:) items:playDown,playUp, nil];

        help = [[CCMenuItemFont itemFromString:@"Help" target:self selector:@selector(help:)] retain]; 
        help.position = ccp(512,350);

        CCMenu *menu = [CCMenu menuWithItems:play,help, nil];
        [self addChild:menu];
        play.position = ccp(0,300);

        down = 0;

        [self schedule:@selector(itemSelected) interval:0.01];

    }
    return self;
}
    -(void) playDown: (id) sender {
        if (down == 0) {
            if ([help parent] != self) {
                help = [[CCMenuItemFont itemFromString:@"Help" target:self selector:@selector(help:)] retain];
                [self addChild:help];
                help.position = ccp(512,350);
                [help runAction:[CCMoveTo actionWithDuration:1 position:ccp(512,500)]];
                NSLog(@"Added Help");
            }
            if ([options parent] != self) {
                options = [[CCMenuItemFont itemFromString:@"Options" target:self selector:@selector(options:)] retain];
                [self addChild:options];
                options.position = ccp(512,650);
                [options runAction:[CCMoveTo actionWithDuration:1 position:ccp(512,600)]];
                NSLog(@"Added Options");
                down = 1;
            }
            return;
        }

        if (down == 1) {
            if ([options parent] == self) {
                [self removeChild:options cleanup:YES];
            }
            if ([help parent] == self) {
                [self removeChild:help cleanup:YES];
                down = 0;
            }
            return;
        }
    }

    -(void) playUp: (id) sender {

    }

    -(void) help: (id) sender {
        NSLog(@"Help Selected");
    }
    -(void) options: (id) sender {
        NSLog(@"Options Seleted");
    }

    -(void) itemSelected {
        if (help.isSelected) {
            [self runAction:[CCCallFunc actionWithTarget:self selector:@selector(help:)]];
        }
        if (options.isSelected) {
            [self runAction:[CCCallFunc actionWithTarget:self selector:@selector(options:)]];
        }
    }

- (void) dealloc
{

    [super dealloc];
}
@end

其他一切似乎都在起作用,就像添加精灵和移动菜单项一样,我使用切换来指示天气发送下拉菜单菜单以显示不显示

1 个答案:

答案 0 :(得分:1)

CCMenuItem意味着是CCMenu(不是CCScene)的子代,并且本身不提供触摸处理。因此,您需要创建一个或多个CCMenu,您将添加一个或多个CCMenuItem。 CCMenu对象将处理触摸处理,并根据触摸发生的menuItem以及触摸事件时menuItem的当前状态,根据需要调用menuItem。

我还在代码中看到了一些内存泄漏漏洞。当addChild添加到任何coco对象时,将保留要添加的对象,因此您无需保留它(在大多数情况下)。调用父级的清理方法时,将释放保留的对象。由于它们是自动释放的对象,因此它们最终将被释放,而无需进一步关注。如果要保留它们,请确保在dealloc(或cleanup)方法中释放这些保留的对象。