gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 17:25:36 24 4
gpt4 key购买 nike

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

这里是.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__

下面是 .cpp 文件中 getMySpriteByName()getBrickByName 的定义:

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

最佳答案

你错过了*(指针)

.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;
}

关于c++ - 如何用C++在cocos2dx中定义非静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45582666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com