这个错误如何修改

这几行是判断,如果有114节点了,就不执行下方代码了,防止重复添加。是有必要的

HelloWorldScene.h头文件中,下面用//标注的地方能否解释下它的含义?
#ifndef HELLOWORLD_SCENE_H
#define HELLOWORLD_SCENE_H

#include “cocos2d.h”

class HelloWorld : public cocos2d::Scene //这行的含义是什么?
{
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);         //这行的含义是什么?为什么要手动实现静态方法?
	
void popScene(float t);

};

#endif // HELLOWORLD_SCENE_H

在HelloWorldScene.cpp文件中,下面代码出现这个错误:
auto closeItem1 = MenuItemImage::create(
“CloseNormal.png”,
“CloseSelected.png”,
CC_CALLBACK_1(Www::menuCloseCallback, this));
错误如下:
error:class类"Www"没有成员"menuCloseCallback"。

还有下面代码出现这个错误:
void Www::menuCloseCallback(Ref* pSender)
{
this->scheduleOnce(CC_SCHEDULE_SELECTOR(Www::popScene), 2);
}
错误如下:
error:class类"Www"没有成员"popScene"。
error:this只能用于非静态成员函数内部。

但是我已经在类"Www"中添加了成员函数menuCloseCallback和成员函数popScene,为什么还有上面这些错误?
声明类"Www"的WwwScene.h文件如下:
#ifndef WWW_SCENE
#define WWW_SCENE

#include “cocos2d.h”

class Www : public cocos2d::Scene
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
void menuCloseCallback(cocos2d::Ref* pSender);
CREATE_FUNC(Www);
void popScene(float t);
};
#endif
已将demo文件上传,麻烦看一下怎么回事。
demo.rar (824.5 KB)

创建一个自动销毁的对象,创建的对象会添加到对象池里面。创建失败返回一个 NULL,这一点和引擎的内存管理有关系。

至于你在 HelloWorld 里面实现 Www 这个是语法错误。你这个属于语法错误,可以看看 C++ Primer中文版 (第5版)

我已经下载了[C++ Primer中文版 (第5版)]。
该版本具体是在哪个章节讲到了以上的问题?

如果你的时间够建议从第一章看起,这个就是第三章的 第Ⅲ部分 类设计者的工具,类设计,建议从基础看完,再去看之前的文档,会好很多,这样你是真正的理解。

能否先说明一下我以上问题的错误之处,以及解决方法。
错误如下:
error:class类"Www"没有成员"menuCloseCallback"。
error:class类"Www"没有成员"popScene"。
error:this只能用于非静态成员函数内部。

另外这本C++ Primer Plus会不会更基础些,其中涉及类的章节有这几章:
第10章 对象和类
第11章 使用类
第12章 类和动态内存分配
第13章 类继承
是不是这几章的内容与我的问题有关?
http://www.j9p.com/down/527472.html

你的头文件没有定义这个 函数 menuCloseCallback。

和上面的一样的问题。

这个是语法规定,this 是一个对象,对象不能访问静态成员,静态成员是所有对象都有的,静态成员只能用类名去访问。

这是你的代码的一部分,还有很多处这种问题。

这一份MyCppGame_2.zip (795.0 KB) 是改好没有编译错误的代码。

c++ primer 比 c++ prime plus 好一点。

你的头文件没有定义这个 函数 menuCloseCallback。

我的WwwScene.h头文件如下:
#ifndef WWW_SCENE
#define WWW_SCENE

#include “cocos2d.h”

class Www : public cocos2d::Scene
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
void menuCloseCallback(cocos2d::Ref* pSender);
CREATE_FUNC(Www);
void popScene(float t);
};
#endif
其中这行void menuCloseCallback(cocos2d::Ref* pSender);不是已经在WwwScene.h头文件中定义了吗?为什么说没有在头文件中定义?

你截图给我看下,这错误就是这么回事,没有在头文件定义。

检查了下之前的WwwScene.h头文件中,确实没有添加
void menuCloseCallback(cocos2d::Ref* pSender);
void popScene(float t);
这两个头文件,现在已经添加了,已不存在上面的问题。

现在有另一个功能不知该如何实现,
在WwwScene.cpp文件中,我想在popScene成员函数里添加WwwScene.cpp文件中所创建的那个Scene场景,
但以下这行:
Director::getInstance()->pushScene(Scene);
显示这个错误:
Error:不允许使用类型名。

红色波浪线处显示错误,代码如下:


已将demo文件上传,demo.rar (824.5 KB)

这个属于基础语法,你都没有定义 Scene 就是用 Scene,肯定报错。
可以把它定义成成员变量。你看看你用的变量是不是都需要定义,这个属于基础语法错误。

不知该怎么定义这个场景成员变量,修改如下,运行后还是出错:

WwwScene.h文件修改如下:
#ifndef WWW_SCENE
#define WWW_SCENE

#include “cocos2d.h”

class Www : public cocos2d::Scene{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(Www);
void menuCloseCallback(cocos2d::Ref* pSender);
void popScene(float t);
Scene* wscene;
};
#endif

WwwScene.cpp文件修改如下:
#include “WwwScene.h”
USING_NS_CC;
Scene* Www::createScene(){
Scene* wscene=Scene::create();
return Www::create();
}
bool Www::init(){
if (!Scene::init()){
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
// add “HelloWorld” splash screen"
auto sprite = Sprite::create(“test.png”);
// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(sprite, 0);

return true;

}
void Www::popScene(float t){
Director::getInstance()->pushScene(wscene);
}

我也不知道怎么回事, :rofl:

场景对象只需要在源文件中创建就好了,不需要再头文件中添加场景变量,之前没有在WwwScene.cpp中创建场景对象。

但还有另一个问题,在HelloWorldScene.cpp文件中无法同时使用两个按钮菜单里的menuCloseCallback回调函数。诺使用两个就会显示重复。
HelloWorldScene.cpp文件的内容如下,该如何修改?
#include “HelloWorldScene.h”
#include “WwwScene.h”

USING_NS_CC;

Scene* HelloWorld::createScene()
{
return HelloWorld::create();
}

// on “init” you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Scene::init() )
{
return false;
}

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
//    you may modify it.

// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
                                       "CloseNormal.png",
                                       "CloseSelected.png",
                                       CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                            origin.y + closeItem->getContentSize().height/2));

// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
///////////////////////////////
 auto closeItem1 = MenuItemImage::create(
                                       "CloseNormal.png",
                                       "CloseSelected.png",
                                       CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

closeItem1->setPosition(Vec2(origin.x + visibleSize.width/2,
                            origin.y + closeItem1->getContentSize().height/2));

// create menu, it's an autorelease object
auto menu1 = Menu::create(closeItem1, NULL);
menu1->setPosition(Vec2::ZERO);
this->addChild(menu1, 1);
// 3. add your codes below...

// add a label shows "Hello World"
// create and initialize a label

auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);

// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,
                        origin.y + visibleSize.height - label->getContentSize().height));

// add the label as a child to this layer
this->addChild(label, 1);

// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");

// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

// add the sprite as a child to this layer
this->addChild(sprite, 0);

return true;

}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
this->scheduleOnce(CC_SCHEDULE_SELECTOR(WwwScene::popScene), 2);
}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
this->scheduleOnce(CC_SCHEDULE_SELECTOR(WwwScene::popScene), 2);
}

你想一想,你的变量可以重复定义吗,在同一个作用域,你定义两个一样的,肯定报错,删除其中一个就可以了。或者你创建两个按钮,分别给按钮添加不同的事件。

创建两个按钮,如果使用同一个回调函数menuCloseCallback应该还是会报错,可以使用不同的回调函数吗?有其它的回调函数吗?

不会报错,使用同一个回调函数。其他的自己写一下就可以了。

在HelloWorldScene.cpp文件中,添加了以下文字按钮和回调函数,但还是报错,
#include “HelloWorldScene.h”
#include “WwwScene.h”

USING_NS_CC;

Scene* HelloWorld::createScene()
{
return HelloWorld::create();
}

// on “init” you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Scene::init() )
{
return false;
}

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
//    you may modify it.

// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
                                       "CloseNormal.png",
                                       "CloseSelected.png",
                                       CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
                            origin.y + closeItem->getContentSize().height/2));

// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
///////////////////////////////
auto pMenu=Menu::create();
pMenu->setPosition(Vec2::ZERO);
this->addChild(pMenu, 1);
auto pMenuItemFront=MenuItemFont::create("click me",
										CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
pMenuItemFront->setPosition(Vec2(origin.x + visibleSize.width/2 ,
                            origin.y + pMenuItemFront->getContentSize().height/2));
pMenuItemFront->setColor(Color3B::BLACK);
pMenu->addChild(pMenuItemFront);
// 3. add your codes below...

// add a label shows "Hello World"
// create and initialize a label

auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);

// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,
                        origin.y + visibleSize.height - label->getContentSize().height));

// add the label as a child to this layer
this->addChild(label, 1);

// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");

// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));

// add the sprite as a child to this layer
this->addChild(sprite, 0);

return true;

}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
Scene* pScene=WwwScene::createScene();
Director::getInstance()->pushScene(pScene);
this->scheduleOnce(CC_SCHEDULE_SELECTOR(WwwScene::popScene), 2);
}

void HelloWorld::menuCloseCallback(Ref* pSender)
{
Scene* pScene1=WwwScene::createScene();
Director::getInstance()->pushScene(pScene1);
this->scheduleOnce(CC_SCHEDULE_SELECTOR(WwwScene::popScene), 2);
}

错误如下:
error C2084: 函数“void HelloWorld::menuCloseCallback(cocos2d::Ref *)”已有主体