2012-02-12 67 views
0

我試圖創建一個下拉菜單的錯覺,但是在移動子菜單項之後,我不能再選擇它們了嗎?Cocos2d在移動它們後不能選擇菜單項

我在這裏的全部代碼:

#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 

一切似乎是工作像加了精靈和移動菜單項,我用一個切換信號天氣派下拉菜單菜單顯示不以顯示

+0

在上面的代碼中,什麼類是「自我」? – YvesLeBorg 2012-02-12 18:26:11

+0

虐待我的整個代碼 – mattblessed 2012-02-12 21:59:49

+0

我認爲self是指'self'所在的類 – mattblessed 2012-02-12 22:02:37

回答

1

CCMenuItem旨在成爲CCMenu(而不是CCScene)的子項,並且本身不提供任何觸摸處理。因此,您需要創建一個更多的CCMenu,您將在其中添加一個或多個CCMenuItem。它是CCMenu對象,它將處理觸摸處理,並根據觸摸發生的菜單項和當前觸摸事件時的菜單項的狀態,酌情調用menuItem。

我也看到你的代碼中的一些內存泄漏的潛力。當你將addChild添加到任何coco對象時,你添加的對象將被保留,因此你不需要保留它(在大多數情況下)。當父級的清理方法被調用時,保留的對象被釋放。由於它們是autorelease'd對象,它們最終將被釋放而不需要進一步關注。如果要保留它們,請確保在dealloc(或清理)方法中釋放這些保留的對象。

相關問題