1 6个文件
Classes
AppDelegate.h
AppDelegate.cpp
HelloWorldScene.h
HelloWorldScene.cpp
win32
main.cpp
main.h
2 AppDelegate.h/AppDelegate.cpp
class AppDelegate : private cocos2d::CCApplication
一个不常见的private继承
The reason for implement as private inheritance is
to hide some interface call by CCDirector.
除去构造和析构函数共有3个方法
virtual bool applicationDidFinishLaunching();
virtual void applicationDidEnterBackground();
virtual void applicationWillEnterForeground();
1> applicationDidFinishLaunching();
2> applicationDidEnterBackground()
CCDirector::sharedDirector()->stopAnimation();
当进入后台时调用
3> applicationWillEnterForeground()
CCDirector::sharedDirector()->startAnimation();
当从后台回到前台时调用
3 HelloWorldScene.cpp/HelloWorldScene.h
在Cocos2d中,在层下设置一个创建场景的静态函数是一个常用的技巧。
我们为helloworld层编写了CCLayer的一个子类,在子类中为层添加各种精灵或者逻辑处理代码。
代码分为4个部分:
--1 对父类初始化
相当简单啊,修改这两句就行了。看来引擎果然很方便啊。
Classes
AppDelegate.h
AppDelegate.cpp
HelloWorldScene.h
HelloWorldScene.cpp
win32
main.cpp
main.h
2 AppDelegate.h/AppDelegate.cpp
class AppDelegate : private cocos2d::CCApplication
一个不常见的private继承
The reason for implement as private inheritance is
to hide some interface call by CCDirector.
除去构造和析构函数共有3个方法
virtual bool applicationDidFinishLaunching();
virtual void applicationDidEnterBackground();
virtual void applicationWillEnterForeground();
1> applicationDidFinishLaunching();
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
// 初始化游戏引擎控制器CCDirector,以便启动游戏引擎
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
pDirector->setOpenGLView(pEGLView);
// turn on display FPS
//启用FPS显示
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
//设置绘制间隔
pDirector->setAnimationInterval(1.0 / 60);
// create a scene. it's an autorelease object
//创建helloworld场景
CCScene *pScene = HelloWorld::scene();
// run
pDirector->runWithScene(pScene);
return true;
}
FPS一般60以上流程
2> applicationDidEnterBackground()
CCDirector::sharedDirector()->stopAnimation();
当进入后台时调用
3> applicationWillEnterForeground()
CCDirector::sharedDirector()->startAnimation();
当从后台回到前台时调用
3 HelloWorldScene.cpp/HelloWorldScene.h
class HelloWorld : public cocos2d::CCLayer
{
public:
// Here's a difference. Method 'init' in cocos2d-x returns bool,
//instead of returning 'id' in cocos2d-iphone
virtual bool init();
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
};
1> CCScene* HelloWorld::scene()
在Cocos2d中,在层下设置一个创建场景的静态函数是一个常用的技巧。
我们为helloworld层编写了CCLayer的一个子类,在子类中为层添加各种精灵或者逻辑处理代码。
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
2> bool HelloWorld::init()
代码分为4个部分:
--1 对父类初始化
if ( !CCLayer::init() )
{
return false;
}
--2 创建一个菜单并添加到层
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);
--3 创建helloworld标签并添加到层
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
this->addChild(pLabel, 1);
--4 创建显示"helloworld.png"的精灵并添加到层
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(pSprite, 0);
我们试着换一下图片和文字
相当简单啊,修改这两句就行了。看来引擎果然很方便啊。
CCLabelTTF* pLabel = CCLabelTTF::create("My Radish", "Arial", 24);
CCSprite* pSprite = CCSprite::create("Mytest.png");