【问题标题】:Cocos2d CCMenuItem animation upon selection选择时的 Cocos2d CCMenuItem 动画
【发布时间】:2012-05-07 01:07:02
【问题描述】:

我有一个CCMenu 和 5 个CCMenuItems。当用户触摸菜单项时,我希望菜单项向右移动 10 个像素,以与其他菜单项区分开来。我尝试将每个菜单项设为全局变量,这样我就可以说:if (item.isSelected) { [item runAction:blah]; } 但这并没有做任何事情。到目前为止,这是我的代码:

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 函数中?感谢您的帮助

【问题讨论】:

  • 我也有这个,我正在使用一个代码,你可以试试这个代码,我相信它对你有用。

标签: iphone ios animation cocos2d-iphone ccmenuitem


【解决方案1】:

如果您想更改 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 类中的代码。

【讨论】:

    【解决方案2】:

    检查以下两行代码:

        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 使其看起来像在右侧或您想要的任何位置偏移 10 像素。
    • cocos2d 是开源的,您可以通过多种方式完成您的任务。检查CCMenu.h 类。你可以 根据您的要求修改课程。
    • 例如,您可以在CCMenu.h 类中更改以下代码片段。

          #pragma mark Menu - Touches
          #ifdef __IPHONE_OS_VERSION_MAX_ALLOWED
      

    如有任何疑问,请告诉我。 问候, 尼尔。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多