我已经下载了[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);
}
我也不知道怎么回事,
场景对象只需要在源文件中创建就好了,不需要再头文件中添加场景变量,之前没有在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 *)”已有主体
这个是语法问题,和游戏引擎没关系,和逻辑也没关系。不熟悉基础永远学的磕磕碰碰。
这帖子竟然还活跃着。。。。个人建议楼主还是别学2dx了,反正目前看来你这投入也不大,干脆从creator入手更简单,等api熟悉了有兴趣再回头反推2dx的写法,因为你这就算把2dx学好了也没啥意义,首先,2dx官方已经不维护了,随着手机系统更新换代,越往后魔改成本越大,有魔改的技术实力还会继续做2dx?其次,2dx的地位很尴尬,大火的时候是qp,现在gj下场大佬垄断,2dx的qp基本只能小打小闹找个地方小公司混吃等死。为今后打算,想要做原生不如转u3d(个人觉得原生上以前2dx还是能打的,ccc真的不是很如意),想继续cocos不如转creator做h5,原生也能做,不过就是累点拉胯点,找个工作混口饭还是不难的
那这里应如何修改?
换一个函数名称,就可以了。