简体   繁体   English

选择时Cocos2d CCMenuItem动画

[英]Cocos2d CCMenuItem animation upon selection

I have a CCMenu with 5 CCMenuItem s. 我有一个CCMenu 5 CCMenuItem秒。 When the user touches a menu item, I want the menu item to move to the right 10 pixels, to distinguish it from the others. 当用户触摸菜单项时,我希望菜单项移动到右边10个像素,以区别于其他像素。 I tried making each menu item a global variable so I could say: if (item.isSelected) { [item runAction:blah]; } 我尝试将每个菜单项if (item.isSelected) { [item runAction:blah]; }全局变量,因此我可以说: if (item.isSelected) { [item runAction:blah]; } if (item.isSelected) { [item runAction:blah]; } But this didn't do anything. if (item.isSelected) { [item runAction:blah]; }但这并没有做任何事情。 This is my code so far: 到目前为止这是我的代码:

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)]];
}

My question is: how can I achieve the effect I mentioned earlier? 我的问题是:我怎样才能达到我之前提到的效果? I want the selected CCMenuItem to move out to the right 10 pixels when the user touches it but doesn't release, and then go back to its normal position when the touch leaves that menu item. 我希望所选的CCMenuItem在用户触摸但未释放时向右移出10个像素,然后在触摸离开该菜单项时返回其正常位置。 Also, where should I put this animation code? 另外,我应该在哪里放这个动画代码? In my init function? 在我的init函数中? Thanks for the help 谢谢您的帮助

If you want to change the 'out of the box' behaviour of the CCMenuItemLabel object, you will need to sub-class that specific class of cocos2d. 如果要更改CCMenuItemLabel对象的“开箱即用”行为,则需要对该特定类cocos2d进行子类化。 The methods you will need to override are 您需要覆盖的方法是

-(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);
}

The 'selected' method is called when the finger touches the label. 当手指触摸标签时,调用“选定”方法。 The 'unselected' method is called when the finger is lifted or is dragged outside the label. 当手指被抬起或被拖到标签外时,会调用“未选择”方法。 I have just shown you a basic (very) approach to selected/unselected behaviour, experiment with it. 我刚刚向您展示了选择/未选择行为的基本(非常)方法,并对其进行了实验。 There are timing issues involved. 涉及时间问题。 I would avoid the use of animations as a first attempt as this. 我会避免使用动画作为第一次尝试。 Look at the code in CCMenuItemLabel class if you want an example with animation. 如果您想要一个带动画的示例,请查看CCMenuItemLabel类中的代码。

Check the following tow line of code: 检查以下两行代码:

    CCMenuItem *item31 = [CCMenuItemImage itemFromNormalImage:@"btn_on.png" selectedImage:@"btn_on_hover.png"];
    CCMenuItem *item32 = [CCMenuItemImage itemFromNormalImage:@"btn_off.png" selectedImage:@"btn_off_hover.png"];
  • As in the above code you can adjust btn_on_hover.png such a way that it look like has offset of 10px to right side or where ever you want. 与上面的代码一样,您可以调整btn_on_hover.png ,使其看起来像是偏向 10px到右侧或者您想要的位置。
  • You can Achieve your task by many ways as cocos2d is open source. 您可以通过多种方式实现您的任务,因为cocos2d是开源的。 check CCMenu.h class. 检查CCMenu.h类。 you can modify class as per your requirement. 你可以根据你的要求修改课程。
  • for example you can do changes in following code fragment in CCMenu.h class. 例如,您可以在CCMenu.h类中对以下代码片段进行CCMenu.h

      #pragma mark Menu - Touches #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED 

Let me know in case of any queries. 如有任何疑问,请告诉我。 Regards, Neil. 此致,尼尔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM