如何用c ++在cocos2dx中定义非静态方法

时间:2017-08-09 05:58:27

标签: c++ cocos2d-x

我想定义一个这样的自定义非静态方法:

这是.h文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Scene
{

private:
    Director *dir;
    SpriteFrameCache *spriteCache;
    Size visiableSize;
    Vec2 visialbeOrigin;
    cocos2d::Sprite getMySpriteByName(const std::string &name);
    cocos2d::Sprite getBrickByName(const std::string &name);

public:
    static cocos2d::Scene* createScene();

    virtual bool init();

    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

以下是getMySpriteByName()文件中定义getBrickByName.cpp的方式:

cocos2d::Sprite HelloWorld::getMySpriteByName(const std::string &name) {
    return Sprite::createWithSpriteFrameName(name);

}

cocos2d::Sprite HelloWorld::getBrickByName(const std::string &name) {
    auto brickSprite = this->getMySpriteByName(name);
    return brickSprite;
}

但IDE或编译器不允许我这样做

我做错了什么?

构建错误是:

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0415   no suitable constructor exists to convert from "cocos2d::Sprite *" to "cocos2d::Sprite" onestep d:\cocos\onestep\Classes\HelloWorldScene.cpp    14  
Error (active)  E0330   "cocos2d::Sprite::Sprite(const cocos2d::Sprite &)" (declared at line 725 of "d:\cocos\onestep\cocos2d\cocos\2d\CCSprite.h") is inaccessible onestep d:\cocos\onestep\Classes\HelloWorldScene.cpp    19  
Error (active)  E0330   "cocos2d::Sprite::Sprite(const cocos2d::Sprite &)" (declared at line 725 of "d:\cocos\onestep\cocos2d\cocos\2d\CCSprite.h") is inaccessible onestep d:\cocos\onestep\Classes\HelloWorldScene.cpp    20  
Error (active)  E0020   identifier "visibleSize" is undefined   onestep d:\cocos\onestep\Classes\HelloWorldScene.cpp    51  

1 个答案:

答案 0 :(得分:2)

你错过了.h(指针)

cocos2d::Sprite* getMySpriteByName(const std::string &name); cocos2d::Sprite* getBrickByName(const std::string &name); 中的

应为

.cpp
cocos2d::Sprite* HelloWorld::getMySpriteByName(const std::string &name) { return Sprite::createWithSpriteFrameName(name); } cocos2d::Sprite* HelloWorld::getBrickByName(const std::string &name) { auto brickSprite = this->getMySpriteByName(name); return brickSprite; } 中的

应为

<img src="{{url('')}}/img/vcc3logo.png" height="50" width="50" style="float: left">