类定义如下:
class HelloWorld : public cocos2d::Layer
{
private:
Sprite *beijing;
Sprite *cangshu;
Sprite *guazi1;
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
//菜单
void Menutest(Ref *ref);
};
在init中的调用:
bool HelloWorld::init()
{
//前边是初始化的语句,下边是创建精灵的语句 ........
// add "HelloWorld" splash screen"
beijing = Sprite::create("beijing.jpg"); //这里开始提示找不到标识符
cangshu = Sprite::create("cangshu.png");
guazi1 = Sprite::create("guazi1.png");
setTouchEnabled(true);
// position the sprite on the center of the screen
beijing->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
beijing->setScale(2.2);
cangshu->setPosition(Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
guazi1->setPosition(Vec2(visibleSize.width / 2 + origin.x , visibleSize.height / 2 + origin.y ));
// add the sprite as a child to this layer
this->addChild(beijing, 0 , beijing_tag);
this->addChild(cangshu, 1 , cangshu_tag);
this->addChild(guazi1, 2 , guazi1_tag);
因为作业射击游戏刚接触Cocos 2d-x的小白一枚。。。
如图,明明在类中定义了成员变量,但是在init函数里调用的时候却说找不到标识符??为什么呢?之前还尝试过把Sprite定义为全局变量,但是这样编译通过,调试报错,那么到底怎样才能定义一个到处都可以用的Sprite变量呢