2012-05-07 25 views
6

我有 s。當用戶觸摸菜單項時,我希望菜單項移動到右側10個像素,以區別於其他菜單項。我試着讓每個菜單項變成一個全局變量,所以我可以說:if (item.isSelected) { [item runAction:blah]; }但是這沒有做任何事情。這是我的代碼到目前爲止:選擇後的Cocos2d CCMenuItem動畫

CCLabelTTF *sin = [CCLabelTTF labelWithString:@"Single Player" dimensions:labelSize alignment:UITextAlignmentLeft fontName:font fontSize:20]; 
item1 = [CCMenuItemLabel itemWithLabel:sin target:self selector:@selector(goToSinglePlayer:)]; 

CCLabelTTF *spl = [CCLabelTTF labelWithString:@"Splitscreen" dimensions:labelSize alignment:UITextAlignmentLeft fontName:font fontSize:20]; 
item2 = [CCMenuItemLabel itemWithLabel:spl target:self selector:@selector(goToSplitscreen:)]; 

CCLabelTTF *ach = [CCLabelTTF labelWithString:@"Achievements" dimensions:labelSize alignment:UITextAlignmentLeft fontName:font fontSize:20]; 
item3 = [CCMenuItemLabel itemWithLabel:ach target:self selector:@selector(goToAchievements:)]; 

CCLabelTTF *str = [CCLabelTTF labelWithString:@"Store" dimensions:labelSize alignment:UITextAlignmentLeft fontName:font fontSize:20]; 
item4 = [CCMenuItemLabel itemWithLabel:str target:self selector:@selector(goToStore:)]; 

CCLabelTTF *set = [CCLabelTTF labelWithString:@"Settings" dimensions:labelSize alignment:UITextAlignmentLeft fontName:font fontSize:20]; 
item5 = [CCMenuItemLabel itemWithLabel:set target:self selector:@selector(goToSettings:)]; 

CCMenu * mainMenu = [CCMenu menuWithItems:item1, item2, item3, item4, item5, nil]; 

[mainMenu setColor:ccBLACK]; 
[mainMenu alignItemsVerticallyWithPadding:10]; 
mainMenu.position = ccp(90, 90); 

[self addChild:mainMenu]; 

if (item1.isSelected) { 
    [item1 runAction:[CCMoveTo actionWithDuration:1.0f position:ccp(120, 90)]]; 
} 

我的問題是:我怎麼能達到我前面提到的效果?我希望選定的CCMenuItem在用戶觸摸它但未釋放時向右移動10個像素,然後在觸摸離開該菜單項時返回到其正常位置。另外,我應該在哪裏放這個動畫代碼?在我的init功能?感謝您的幫助

+0

我有同樣的這一點,我正在使用一個代碼,你可以嘗試代碼,我相信這將是你的工作。 – vishiphone

+0

我可以看到它嗎? – Seany242

回答

9

如果要更改CCMenuItemLabel對象的'開箱即用'行爲,則需要對該特定類的cocos2d進行子類化。您需要覆蓋的方法有:

-(void) selected{ 
    // coco's default is to scale up by 10% 
    // place your code to displace the label. 
    self.position=ccp(self.position.x-10,self.position.y); 
} 

-(void) unselected{ 
    // coco's default is to bring back scale to originalScale. 
    self.position=ccp(self.position.x+10,self.position.y); 
} 

當手指觸摸標籤時調用'selected'方法。當手指擡起或被拖出標籤時,將調用「未選中」方法。我剛剛向您展示了選擇/未選定行爲的基本(非常)方法,並對其進行了實驗。涉及時間問題。我會避免使用動畫作爲第一次嘗試。如果您需要動畫示例,請查看CCMenuItemLabel類中的代碼。

+0

這會去哪裏?與我上面的代碼在同一個文件中? – Seany242

+0

Nvm,我得到它的工作。謝謝! – Seany242

7

檢查下面的代碼拖車行:

CCMenuItem *item31 = [CCMenuItemImage itemFromNormalImage:@"btn_on.png" selectedImage:@"btn_on_hover.png"]; 
    CCMenuItem *item32 = [CCMenuItemImage itemFromNormalImage:@"btn_off.png" selectedImage:@"btn_off_hover.png"]; 
  • 如上面的代碼,你可以調整btn_on_hover.png這樣一種方式,它看起來像已經抵消了10px的到右側或任何你想要的地方。
  • 由於cocos2d是開源的,您可以通過多種方式實現您的任務。檢查CCMenu.h班。您可以根據您的要求修改課程 。
  • 例如,您可以在CCMenu.h類中的以下代碼片段中進行更改。

    #pragma mark Menu - Touches 
        #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED 
    

讓我知道任何疑問的情況下。 Regards, Neil。