空闲的时间学习一下Cocos2d,顺手记录一些逝去的青春。。。。。
代码段如下:主要学习工程的流程以及注释解释。。。
//main.cpp 函数入口#include "main.h"
#include "AppDelegate.h"
#include "CCEGLView.h"
USING_NS_CC;
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// 创建应用程序实例
AppDelegate app;
CCEGLView* eglView = CCEGLView::sharedOpenGLView();
//设置标题栏的名称
eglView->setViewName("mythcpp");
//设置窗口大小
eglView->setFrameSize(480, 320);
return CCApplication::sharedApplication()->run();
}//main.h头文件,这些都有脚本自动生成#ifndef __MAIN_H__
#define __MAIN_H__
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
// Windows Header Files:
#include <windows.h>
#include <tchar.h>
// C RunTime Header Files
#include "CCStdC.h"
#endif // __MAIN_H__
//AppDelegate.cpp 主要是导演类的定义以场景的管理,都在这里处理。#include "AppDelegate.h"
#include "HelloWorldScene.h"
USING_NS_CC;
AppDelegate::AppDelegate()
{
//AppDelegate构造函数
}
AppDelegate::~AppDelegate()
{
//AppDelegate析构函数
}
// 程序启动后调用此方法
bool AppDelegate::applicationDidFinishLaunching() {
// 初始化导演类
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
//设置OpenGL视图
pDirector->setOpenGLView(pEGLView);
// 设置是否打开FPS、0或flase为不打开
pDirector->setDisplayStats(true);
// 设置FPS默认值,为每秒60帧
pDirector->setAnimationInterval(1.0 / 60);
// 创建一个场景,其使用智能指针进行内存管理,引用计数器方式
CCScene *pScene = HelloWorld::scene();
// 运行场景,真正的游戏开始。
pDirector->runWithScene(pScene);
return true;
}
// 当进入后台,运行此方法,
void AppDelegate::applicationDidEnterBackground() {
CCDirector::sharedDirector()->stopAnimation();
// 如果有声音,请进行声音暂停。
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}
// 恢复游戏调用此方法。
void AppDelegate::applicationWillEnterForeground() {
CCDirector::sharedDirector()->startAnimation();
//恢复声音
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
//AppDelegate.h #ifndef _APP_DELEGATE_H_
#define _APP_DELEGATE_H_
#include "cocos2d.h"
/**
@brief The cocos2d Application.
The reason for implement as private inheritance is to hide some interface call by CCDirector.
*/
class AppDelegate : private cocos2d::CCApplication
{
public:
AppDelegate();
virtual ~AppDelegate();
/**
@brief Implement CCDirector and CCScene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*/
virtual bool applicationDidFinishLaunching();
/**
@brief The function be called when the application enter background
@param the pointer of the application
*/
virtual void applicationDidEnterBackground();
/**
@brief The function be called when the application enter foreground
@param the pointer of the application
*/
virtual void applicationWillEnterForeground();
};
#endif // _APP_DELEGATE_H_
//HelloWorldScene.cpp helloWorld场景的实现#include "HelloWorldScene.h"
using namespace boost;
USING_NS_CC;
CCScene* HelloWorld::scene()
{
//‘场景’是一个自动释放的对象
CCScene *scene = CCScene::create();
// 'layer' 是一个自动释放的对象
HelloWorld *layer = HelloWorld::create();
// 添加 图层 到场景
scene->addChild(layer);
// return the scene
//返回场景
return scene;
}
// 在这里进行场景的一系列的初始化
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
/////////////////////////////
// 2. 以“X”的形象,这是点击退出程序添加一个菜单项
//你可以修改它。
// 添加一个退出关闭按钮,它是一个自动释放的对象
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. 添加你的代码....
//添加一个标签以显示hello world
// 创建并初进行始化标签
//CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World!!!", "Arial", 48);
// 在场景中心位置显示标签
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - pLabel->getContentSize().height));
// 添加标签到这一层
this->addChild(pLabel, 1);
// 创建hello world背景图
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// 在场景中心位置显示
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
//添加helloWorld精灵到这一层
this->addChild(pSprite, 0);
return true;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}
//"HelloWorldScene.h"#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "boost_head.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
//初始化声明
virtual bool init();
// 返回实例的指针
static cocos2d::CCScene* scene();
// 一个选择回调函数
void menuCloseCallback(CCObject* pSender);
// 内存回收
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
本文档详细介绍了使用Cocos2d-x游戏引擎搭建基本项目的步骤与代码解析,包括了从项目初始化到运行场景的全过程,并展示了如何添加菜单项及标签等基本UI元素。

750

被折叠的 条评论
为什么被折叠?



