Learning Cocos2d-x for WP8(2)——深入刨析Hello World

时间:2022-06-01 12:20:49

原文:Learning Cocos2d-x for WP8(2)——深入刨析Hello World

cocos2d-x框架

在兄弟篇Learning Cocos2d-x for XNA1——小窥cocos2d-x框架中已有详细介绍cocos2d-x框架下的基本元素。可自行参考学习,概念的东西基本一样。

HelloWorld

HelloWorld程序虽然简单,但能测试程序是否能正确的运行,同时很能体现一个框架的整体结构。

cocos2d-x中HelloWorld显示主要通过AppDelegate和HelloWorldScene。

在显示HelloWorld程序时,得将图片资源放进Assets文件夹中

Learning Cocos2d-x for WP8(2)——深入刨析Hello World

AppDelegate

C++程序中最主要的是头文件(.h)和源文件(.cpp)。

AppDelegate.h

AppDelegate.h头文件中,定义类(Class)AppDelegate继承CCApplication。在AppDelegate中声明相关方法。

Learning Cocos2d-x for WP8(2)——深入刨析Hello World

AppDelegate.cpp

AppDelegate.cpp源文件中包含AppDelegate.h头文件,在其中实现头文件中声明的方法。

Learning Cocos2d-x for WP8(2)——深入刨析Hello World

其中在方法applicationDidFinishLaunching中,我们找到了熟悉的CCDirector(导演),当中pScene为起始页面。很显然HelloWorld类,需要在AppDelegate.cpp中引用Classes文件夹中的HelloWorldScene.h头文件(fei话,呵呵)。

Learning Cocos2d-x for WP8(2)——深入刨析Hello World

HelloWorldScene

scene()方法

主要负责将Layer层通过addChild到Scene层

Learning Cocos2d-x for WP8(2)——深入刨析Hello World

init()方法

主要将Layer层以下层内容添加到Layer层。

HelloWorld::init()
 bool HelloWorld::init()
{
bool bRet = false; do
{
if ( !CCLayer::init() )
{
break;
}
this->setIsTouchEnabled(true); CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it's an auto release object.
CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
CC_BREAK_IF(! pCloseItem); // Place the menu item bottom-right conner.
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - , )); // Create a menu with the "close" menu item, it's an auto release object.
CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer.
this->addChild(pMenu, ); // 2. Add a label shows "Hello World". // Create a label and initialize with string "Hello World".
CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World", "Arial", );
CC_BREAK_IF(! pLabel); // Get window size and place the label upper.
CCSize size = CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition(ccp(size.width / , size.height - )); // Add the label to HelloWorld layer as a child layer.
this->addChild(pLabel, ); // 3. Add add a splash screen, show the cocos2d splash image.
CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png");
CC_BREAK_IF(! pSprite); // Place the sprite on the center of the screen
pSprite->setPosition(ccp(size.width/, size.height/)); // Add the sprite to HelloWorld layer as a child layer.
this->addChild(pSprite, ); bRet = true;
} while (); return bRet;
}

HelloWorldScene.h完整代码

HelloWorldScene.h
 #ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__ #include "cocos2d.h"
#include "SimpleAudioEngine.h" class HelloWorld : public cocos2d::CCLayer
{
public:
HelloWorld();
~HelloWorld(); // 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 recommand to return the exactly class pointer
static cocos2d::CCScene* scene(); // a selector callback
void menuCloseCallback(CCObject* pSender); // implement the "static node()" method manually
LAYER_NODE_FUNC(HelloWorld);
}; #endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp完整代码

HelloWorldScene.cpp
 #include "pch.h"
#include "HelloWorldScene.h" using namespace cocos2d; HelloWorld::~HelloWorld()
{
// cpp don't need to call super dealloc
// virtual destructor will do this
} HelloWorld::HelloWorld()
{
} CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::node();
CC_BREAK_IF(! scene); // 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::node();
CC_BREAK_IF(! layer); // add layer as a child to scene
scene->addChild(layer);
} while (); // return the scene
return scene;
} // on "init" you need to initialize your instance
bool HelloWorld::init()
{
bool bRet = false; do
{
if ( !CCLayer::init() )
{
break;
}
this->setIsTouchEnabled(true); CCSize screenSize = CCDirector::sharedDirector()->getWinSize(); // 1. Add a menu item with "X" image, which is clicked to quit the program. // Create a "close" menu item with close icon, it's an auto release object.
CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
CC_BREAK_IF(! pCloseItem); // Place the menu item bottom-right conner.
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - , )); // Create a menu with the "close" menu item, it's an auto release object.
CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(! pMenu); // Add the menu to HelloWorld layer as a child layer.
this->addChild(pMenu, ); // 2. Add a label shows "Hello World". // Create a label and initialize with string "Hello World".
CCLabelTTF* pLabel = CCLabelTTF::labelWithString("Hello World", "Arial", );
CC_BREAK_IF(! pLabel); // Get window size and place the label upper.
CCSize size = CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition(ccp(size.width / , size.height - )); // Add the label to HelloWorld layer as a child layer.
this->addChild(pLabel, ); // 3. Add add a splash screen, show the cocos2d splash image.
CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png");
CC_BREAK_IF(! pSprite); // Place the sprite on the center of the screen
pSprite->setPosition(ccp(size.width/, size.height/)); // Add the sprite to HelloWorld layer as a child layer.
this->addChild(pSprite, ); bRet = true;
} while (); return bRet;
} void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}

Learning Cocos2d-x for WP8(2)——深入刨析Hello World

版本cocos2dx-0.13.0-wp8-0.8似乎不怎么给力,Lumia820真机上测试不通过,模拟器没任何问题。不过快有新版本出来了吧,现在凑合学习学习。

著作权声明:本文由http://www.cnblogs.com/suguoqiang 原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!