bool AppDelegate::applicationDidFinishLaunching()
{
// initialize Director
CCDirector* pDirector =CCDirector::sharedDirector(); // 场景管理器
CCEGLView* pEGLView =CCEGLView::sharedOpenGLView(); //创建视口
pDirector->setOpenGLView(pEGLView); //设置OpenGL视口
// Set the design resolution
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's height 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. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 /60); // 设置帧速率 ,最好不要低于30帧
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene(); //调用静态方法创建一个场景,HelloWorld中会负责场景的实现
pDirector->runWithScene(pScene); //导演调用,运行HelloWorld中的场景。
return true;
}
CCScene*HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create(); //创建场景
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create(); //创建图层
// add layer as achild to scene
scene->addChild(layer); //添加图层作为节点
// return the scene
return scene;
}
bool HelloWorld::init()
{
// 1. super initfirst
if (!CCLayer::init() ) //创建图层
{
return false;
}
CCSize visibleSize =CCDirector::sharedDirector()->getVisibleSize(); //获取大小
CCPoint origin =CCDirector::sharedDirector()->getVisibleOrigin(); //获取原点,原点(origin.x , origin.y)在左下角
CCMenuItemImage *pCloseItem =CCMenuItemImage::create( "CloseNormal.png, "CloseSelected.png", this,
menu_selector( HelloWorld::menuCloseCallback ) ); //创建关闭按钮 ,在这里的图片路径如果不是在/Resources 目录下,则图片不能使用。 如果要在Resources目下放置文件夹,则需要在字符串中加入路径名称,如”/raster-32x32/ 32x32.png“
//设置按钮位置,可以看出 Cocos2d-x的坐标原点是左下角
pCloseItem->setPosition(ccp(origin.x+ visibleSize.width - pCloseItem->getContentSize().width/2 ,
origin.y +pCloseItem->getContentSize().height/2));
// visibleSize.width 屏幕的宽度
// pCloseItem->getContentSize().width/2 是图片宽度的1/2
// pCloseItem->getContentSize().height/2 是图片高度的1/2
// pCloseItem->setPosition(ccp(origin.x , origin.y ) ); // pCloseItem的坐标设置是以默认锚点为坐标中心,即锚点在(0,0),此时按钮只能显示1/4
//创建菜单,将关闭按钮加入到菜单项
CCMenu* pMenu = CCMenu::create(pCloseItem,NULL);
pMenu->setPosition(CCPointZero); // 设置菜单的位置
this->addChild(pMenu,1); // 将菜单加入到HelloWorld图层中
//创建 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)); //设置文本(Label)的位置 ,坐标是字符串中心的坐标,并不是最开始的位置
// add the labelas a child to this layer
this->addChild(pLabel,1); // 将文本(Label)加入到HelloWorld图层中
//创建精灵图片
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); // 将图片精灵加入到HelloWorld图层中 (第二个参数是Z轴坐标:0在底层,1在上层 。 Z轴坐标系参考笛卡尔右手坐标系(正方向:x轴向右,y轴向上,z轴向外)
//设置为相同的Z轴坐标时,按加载顺序显示,先加载的先显示,后加载的后显示
return true;
}