学习Cocos2D-x之路(2)--学习第一个例子
来源:互联网 发布:创维25nd9000总线数据 编辑:程序博客网 时间:2024/05/17 02:39
第一个例子—HelloWorld
在vs2010中启动HelloCpp,看到如下窗口
暂时不知道什么原因,图片大小与窗口并不匹配,点击右下角按钮可以退出。在Classes目录下有5个文件,在AppMaCros.h中定义了在不同平台下资源的大小。
类AppDelegate继承了CCApplication,定义了三个方法:
virtualbool applicationDidFinishLaunching();//响应窗口启动完成后的工作
virtualvoid applicationDidEnterBackground();//响应窗口进入后台的工作
virtualvoid applicationWillEnterForeground();//响应窗口恢复的工作。
在这个项目中,只有响应窗口启动后的工作。 下面是其中的代码
bool AppDelegate::applicationDidFinishLaunching() {
// initializedirector
CCDirector* pDirector =CCDirector::sharedDirector(); // 场景管理器
CCEGLView* pEGLView =CCEGLView::sharedOpenGLView(); //创建视口
pDirector->setOpenGLView(pEGLView); //设置OpenGL视口
// Set the designresolution
pEGLView->setDesignResolutionSize(designResolutionSize.width,designResolutionSize.height, kResolutionNoBorder); //设置分辨率大小
CCSize frameSize =pEGLView->getFrameSize();
vector<string> searchPath; //资源路径
//在不同的平台下加载不同的资源
if (frameSize.height> mediumResource.size.height)
{
searchPath.push_back(largeResource.directory);
pDirector->setContentScaleFactor(MIN(largeResource.size.height/designResolutionSize.height,largeResource.size.width/designResolutionSize.width));
}
// if the frame'sheight is larger than the height of small resource size, select mediumresource.
else if (frameSize.height > smallResource.size.height)
{
searchPath.push_back(mediumResource.directory);
pDirector->setContentScaleFactor(MIN(mediumResource.size.height/designResolutionSize.height,mediumResource.size.width/designResolutionSize.width));
}
// if the frame'sheight is smaller than the height of medium resource size, select smallresource.
else
{
searchPath.push_back(smallResource.directory); pDirector->setContentScaleFactor(MIN(smallResource.size.height/designResolutionSize.height,smallResource.size.width/designResolutionSize.width));
}
//设置资源文件路径
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPath);
// turn ondisplay FPS
pDirector->setDisplayStats(true); //开启显示FPS
// set FPS. thedefault value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 /60); 设置帧速率
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();
// run
pDirector->runWithScene(pScene); 运行HelloWorld中的场景。
return true;
}
在HelloWorld类中,定义了一个场景。
CCScene*HelloWorld::scene()
{
// 'scene' is anautorelease object
CCScene *scene = CCScene::create(); //创建场景
// 'layer' is anautorelease object
HelloWorld *layer = HelloWorld::create();//创建图层
// add layer as achild to scene
scene->addChild(layer); //添加图层作为节点
// return thescene
returnscene;
}
bool HelloWorld::init()
{
//////////////////////////////
// 1. super initfirst
if (!CCLayer::init() ) //创建图层
{
return false;
}
CCSize visibleSize =CCDirector::sharedDirector()->getVisibleSize(); //获取大小
CCPoint origin =CCDirector::sharedDirector()->getVisibleOrigin();//获取原点
//创建关闭按钮
CCMenuItemImage *pCloseItem =CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
//设置按钮位置,可以看出 Cocos2d-x的坐标原点是左下角
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);
//创建 HelloWorld 文本及设置位置
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World","Arial",TITLE_FONT_SIZE);
// position thelabel on the center of the screen
pLabel->setPosition(ccp(origin.x +visibleSize.width/2,
origin.y +visibleSize.height - pLabel->getContentSize().height));
// add the labelas a child to this layer
this->addChild(pLabel,1);
//创建精灵
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position thesprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y));
// add the spriteas a child to this layer
this->addChild(pSprite,0);
return true;
}
- 学习Cocos2D-x之路(2)--学习第一个例子
- highcharts学习之第一个例子
- odoo之学习第一个例子test(1)
- 学习hibernate第一个例子
- tensorflow 学习第一个例子
- lucene学习的第一个例子之创建索引
- spring aop学习的第一个例子
- ibatis 学习笔记 包括第一个例子
- hadoop第一个例子wordcount学习
- Drools学习笔记4-第一个例子
- SpringMVC学习历程--SpringMVC第一个例子
- Drools学习笔记4-第一个例子
- hadoop第一个例子wordcount学习
- NGUI研究院之开始学习制作第一个例子(一)
- NGUI研究院之开始学习制作第一个例子(一)
- NGUI研究院之开始学习制作第一个例子(一)
- NGUI研究院之开始学习制作第一个例子(一)
- 学习cocos2d-x 之路 (1)--了解cocos2d-x
- 找不到或无法加载主类
- 【鸟枪当炮 PowerPoint玩刻录】
- POJ 1434 二分
- hdu 1003 Max Sum
- 【WPS快速调整从网上复制的零乱文字】
- 学习Cocos2D-x之路(2)--学习第一个例子
- mfs 分布式文件系统
- SSI开发:spring-pool.xml标准配置模板
- 策略模式
- 个人日记
- 【C# 相关】
- i2c_client,i2c_adapter和I2C-core的简介
- FT5406触摸屏驱动
- 详析mysql隐藏空间