第一个例子—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;
}