如何在Coco2d-x中指定CREATE_FUNC参数

How to specify argument in CREATE_FUNC in Coco2d-x

本文关键字:CREATE FUNC 参数 Coco2d-x      更新时间:2023-10-16

我的MessageBoxes类在构造函数中传递了一个参数,我希望知道我如何在CREATE_FUNC()中指定参数,在此上下文中是std::string _content ?

我得到的错误是"构造函数不能被声明为'static'"

这是MessageBoxes.h: 的代码
class MessageBoxes : public cocos2d::Node
{
    private:
        Sprite* _sprite;
        bool _speaking;
        float _speakingTime;
        std::string _content;
    public:
        CREATE_FUNC(MessageBoxes(std::string content)); 
    protected:
        virtual bool init(std::string _content);
        void setSprite();
        void setContent();
};

CREATE_FUNC是在CCPlatformMacros.h中定义的预定义宏

#define CREATE_FUNC(__TYPE__) 
static __TYPE__* create() 
{ 
    __TYPE__ *pRet = new(std::nothrow) __TYPE__(); 
    if (pRet && pRet->init()) 
    { 
        pRet->autorelease(); 
        return pRet; 
    } 
    else 
    { 
        delete pRet; 
        pRet = NULL; 
        return NULL; 
    } 
}

代码

CREATE_FUNC(对话框内容(std:: string));

实际上是

new(std::nothrow) MessageBoxes(std::string content)();

在c++中有编译错误。

但是你可以自己编写类似于CREATE_FUNC的创建函数,比如

static MessageBoxes* create(std::string content) {
    MessageBoxes* ret = new(std::nothrow) MessageBoxes();
    if(ret && ret->init(content)) { //<----Or anything you wanna init with
        ret->autorelease();
        return ret;
    } else {
        delete ret;
        ret = nullptr;
        return nullptr;
    }
}