使用 Objective-C 子类化 Cocos2D MenuItem
我在我的小项目中使用CCMenu
,它有三个按钮。我需要这些按钮在检测到触摸时保持触发,由于这不是正常行为,我决定对 CCMenuItem 进行子类化并重写几个方法。
我希望重写的两个方法是:
-(void) selected
{
// subclass to change the default action
if(isEnabled_) {
[super selected];
[self stopActionByTag:kZoomActionTag];
originalScale_ = self.scale;
CCAction *zoomAction = [CCScaleTo actionWithDuration:0.1f scale:originalScale_ * 1.2f];
zoomAction.tag = kZoomActionTag;
[self runAction:zoomAction];
}
}
-(void) unselected
{
// subclass to change the default action
if(isEnabled_) {
[super unselected];
[self stopActionByTag:kZoomActionTag];
CCAction *zoomAction = [CCScaleTo actionWithDuration:0.1f scale:originalScale_];
zoomAction.tag = kZoomActionTag;
[self runAction:zoomAction];
}
}
因此,在我的子类中,我只是完全复制这些方法,但用新功能替换里面的代码。为了简单起见,我们会说:
-(void) selected
{
//turn a sprite around
mySprite.rotation = 0;
}
-(void) unselected
{
//turn a sprite around
mySprite.rotation = 180;
}
现在,mySprite 将在主体代码的标头中声明,该主体代码将被导入到该子类中。
问题是无法看到 mySprite,它收到 未声明
错误。我应该使用 [super selected]
而不是 mySprite
吗?我已经尝试过这个,我得到完全相同的错误。
谢谢。
I'm using a CCMenu
in my small project, it has three buttons in it. I need these buttons to keep triggering if they detect a touch, and as this isn't normal behaviour I decided to subclass the CCMenuItem
and override a couple of methods.
The two methods I wish to override are:
-(void) selected
{
// subclass to change the default action
if(isEnabled_) {
[super selected];
[self stopActionByTag:kZoomActionTag];
originalScale_ = self.scale;
CCAction *zoomAction = [CCScaleTo actionWithDuration:0.1f scale:originalScale_ * 1.2f];
zoomAction.tag = kZoomActionTag;
[self runAction:zoomAction];
}
}
-(void) unselected
{
// subclass to change the default action
if(isEnabled_) {
[super unselected];
[self stopActionByTag:kZoomActionTag];
CCAction *zoomAction = [CCScaleTo actionWithDuration:0.1f scale:originalScale_];
zoomAction.tag = kZoomActionTag;
[self runAction:zoomAction];
}
}
So in my subclass I am simply duplicating these exactly, but replace the code inside with the new functionality. To keep it simple, we'll say:
-(void) selected
{
//turn a sprite around
mySprite.rotation = 0;
}
-(void) unselected
{
//turn a sprite around
mySprite.rotation = 180;
}
Now, the mySprite would be declared in the header of the main body code, which gets imported into this subclass.
The problem is mySprite cannot be seen, it's getting an undeclared
error. Instead of mySprite
should I be using [super selected]
? I have tried this, I get the exact same error.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,是的,如果您希望它执行菜单项的默认行为,您应该使用 [super selected],而不是仅仅复制超类函数的内容。对于您复制的 CCMenuItemLabel,调用 [super selected] 将允许标签在选择时的视觉效果方面“做它的事情”。这可以让您专注于您想做的事情。
至于为什么看不到你的精灵,以及“未声明”的错误可能是什么,在没有看到代码的情况下很难说。一个问题可能是您说您正在子类化 CCMenuItem,但正在粘贴 CCMenuItemLabel 代码。
您是否尝试过直接在按钮精灵上观看 ccTouchesBegan 和 ccTouchesEnded ?可能比尝试强制 CCMenuItem 做一些它并非真正设计要做的事情更简单...
如果做不到这一点,请研究“虚拟操纵杆”,您应该获得一些很好的示例代码。 Cocos2d 论坛至少有两个关于该主题的主要主题: 主题 1 主题 2
First of all, yes, you should be using [super selected] if you want it to perform the default behavior for the menu item, rather than just copying the contents of the superclass' function. In the case of the CCMenuItemLabel, which is what you copied, calling [super selected] will allow the label to "do its thing" with respect to the visual effects it does when selected. This allows you to concentrate on what YOU want to do.
As to why your sprite cannot be seen, and what the "undeclared" error might be, it's hard to say without seeing the code. One problem might be that you said you are subclassing CCMenuItem, but are pasting in CCMenuItemLabel code.
Have you tried just watching ccTouchesBegan and ccTouchesEnded directly on your button sprite? Might be more straightforward than trying to force CCMenuItem to do something it wasn't really designed to do...
Failing that, look into "virtual joysticks" and you should get some good sample code. The Cocos2d forums have had at least two major threads on the topic: Thread 1 Thread 2