如何通过按下按钮调用第二类的方法?

时间:2013-12-05 10:15:02

标签: c++ objective-c cocos2d-iphone cocos2d-x

我无法调用另一个类的类方法:rewrite the code cocos2d (objective-c) on cocos2d-x (c++)

错误:

c:\program files\microsoft visual studio 11.0\vc\include\xrefwrap(273): error C2562: std::_Pmd_caller<_Ret,_Arg0>::_Call_pmd: функция типа "void", возвращающая значение
1>          with
1>          [
1>              _Ret=void,
1>              _Arg0=cocos2d::Object
1>          ]
1>          c:\program files\microsoft visual studio 11.0\vc\include\xrefwrap(271): см. объявление "std::_Pmd_caller<_Ret,_Arg0>::_Call_pmd"
1>          with
1>          [
1>              _Ret=void,
1>              _Arg0=cocos2d::Object
1>          ]

没有成为完全的kidat.Tam 20多个相同的行

HudLayer.cpp

CCMenuItem *button = CCMenuItemImage::create("1.png","1.png",menu_selector(HelloWorld::createSprite));

HelloWorld.cpp

void HelloWorld::createSprite(CCObject *sender)
{

    _sprite->initWithFile("bird.png");
    _sprite->setPosition(ccp(x,y));

}

2 个答案:

答案 0 :(得分:2)

你应该这样做..

  CCMenuItem *button = CCMenuItemImage::create("1.png","1.png",menu_selector(HudLayer::createSpriteMethod));

void HudLayer::createSpriteMethod(CCObject *sender)
   {
    _helloWorld//assuming you have instance of HelloWorld
    _helloWorld->createSprite(sender);
  }

对于 HelloWorld :: createSprite .....它应该是静态方法。没有什么好处来自它。

答案 1 :(得分:2)

在IronMan.h中你必须声明

static void playClicked();

在Avenger.cpp中你可以使用这个方法

 CCSize *screenSize   = CCDirector::sharedDirector()->getVisibleSize();
CCPoint pointOrigin  = CCDirector::sharedDirector()->getVisibleOrigin();
float x = screenSize.width;
float y = screenSize.height;*

CCWidgetLayout * widgetLayout = CCWidgetLayout :: create();

的addChild(widgetLayout);

CCButton* playButton = CCButton::create("btn1.png", "btn2.png", "btn3.png");
playButton->setPosition(x/1.2f,y/1.5f);
playButton->setClickSelector(this,click_selector(IronMan::playClicked));
widgetLayout->addChild(playButton,1);