iphone - Cocos2d CCMenuItem 选择时的动画

标签 iphone ios animation cocos2d-iphone ccmenuitem

我有一个包含 5 个 CCMenuItemCCMenu。当用户触摸菜单项时,我希望菜单项向右移动 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 函数中?感谢您的帮助

最佳答案

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

关于iphone - Cocos2d CCMenuItem 选择时的动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10475623/

相关文章:

iphone - NSDate - iPhone 上的 GMT

ios - 有什么方法可以检查 UITests 中的平滑滚动

javascript - 强制使用自定义/货币模式的 iOS 数字键盘

android - 动画 ListView 布局时遇到问题

javascript - 悬停时滚动链接文本 - 无尽的选取框效果

javascript - Three.js:重新连接骨骼后如何重新绑定(bind)/重新连接网格骨架?

ios - 为 UIView 提供数据

ios - 将 SSLSetEnabledCiphers 与 AFNetworking 结合使用以禁用弱密码

iphone - iOS RestKit + 核心数据。获取缓慢且核心数据错误

ios - 我已经在我的服务器上上传了 apple-app-site-association,用于后端服务