2

I get an error that says I improperly converted CCMenuItemImage to "Button"

button.cpp:

#include "button.h"

void Button::selected(){
    CCLOG("SELECTED");
}

void Button::unselected(){
    CCLOG("UNSELECTED");
}

button.h:

#ifndef BUTTON_H
#define BUTTON_H
#include "cocos2d.h"

class Button : public cocos2d::CCMenuItemImage{
public:
    virtual void selected();

    virtual void unselected();
};

#endif

SinglePlayer.ccp:

//I get an error 'invalid conversion from 'cocos2d::CCMenuItemImage*' to 'Button*' '

Button *left1 = Button::create("turncircle.png","turncircle.png", this, menu_selector(SinglePlayer::turning));
David Small
  • 581
  • 1
  • 10
  • 25

1 Answers1

4

When you call Button::create, it's actually referencing the create method in CCMenuItemImage, so you're getting a CCMenuItemImage back (not a Button).

You'll need to make a static create method in your Button class. You can still reference the initializers in your superclass, so it should be fairly straightforward. So in your Button class create method, do something like:

Button* Button::create(const char* normalImg, const char* selectedImg) {
    Button *button = new Button ();
    if (button && button->CCMenuItemImage::initWithNormalImage(normalImg, selectedImg, NULL, NULL, NULL) {
      button->autorelease ();
      return button;
    } else {
      delete button;
      button = NULL;
      return NULL;
    }
}

(Disclaimer: I didn't actually test this, just some quick code here.)

If you use this create method, it will return a Button object, which is exactly what you're looking for.

The initializer you're looking for is:

bool CCMenuItemImage::initWithNormalImage(const char *normalImage, const char *selectedImage, const char *disabledImage, CCObject* target, SEL_MenuHandler selector);

You may want to make your create constructor for your Button class accommodate more values of the CCMenuItemImage initializer. Up to you.

Trace through the CCMenuItem.cpp file; the CCMenuItemImage itself is a subclass of CCMenuItem, so that should give you a fairly strong reference point of how this should all be set up. (Both CCMenuItem and CCMenuItemImage are defined in the CCMenuItem.cpp/.h files.)

Hope this helps!!

  • I apologize for taking so long. I've been busy with my college finals. I also apologize for being such a noob. So I added the create method like you said to my Button.cpp. Now I'm getting an error stating it's an invalid conversion from cocos2d::CCMenuItemImage to Button*. I'm not sure what you wanted me to do with the initializer you posted either. Thanks for helping though! – David Small Dec 14 '12 at 17:38
  • I still don't understand. Any help? – David Small Dec 18 '12 at 18:12