《初学cocos2d-x》- 第一个项目(1)

来源:互联网 发布:安全教育平台软件 编辑:程序博客网 时间:2024/05/18 00:59

最近离职休息找工作,无聊,给自己找点事儿,研究一下cocos2d-x。

首先新建一个项目



next




next  然后保存到一个你希望的位置。


OK项目建完了,开始看一下结构。




Group&Files 


1.Resources


Default.png 加载IOS显示的图片
Icon.png 应用图标
fps_images.png cocos2d显示帧,不润徐删除或者修改
Info.plist 应用程序相关的配置信息


2.main.m


大多数时候不需要对main.m进行修改,main和AppDelegate之间的操作都是由iphone sdk自行处理的。
可以理解成,main函数创建了NSAutoreleasePool,调用UIApplicationMain来启动应用程序,AppDelegate用来实现UIApplicationDelegate协议类。


3.prefix.pch
快速预编工具,永远不会变化或者很少发生变化的头晚间才能被添加到当前缀头文件中。


4.AppDelegate类
AppDelegate类通过特定时间从设备接收消息来追踪应用程序的状态变化。
应用程序收到的第一个消息是applicationDidFinishLaunching方法,cocoa2d的初始化代码都是写到这个方法里面。其中

FPS显示开关
pDirector->setDisplayStats(true);


设置刷新率 1/60 ,ios设备最好的情况是每秒60帧,这个跟游戏逻辑复杂性有直接关系,当然可以设置低一点。
游戏过于复杂FPS值不停波动,这样会影响游戏体验,如果真是这样,那么就可以思考一下代码的质量了
pDirector->setAnimationInterval(1.0 / 60);


当应用程序是不活跃时或来电时,都会调用
applicationDidEnterBackground()方法


当应用程序再次活跃的时候,调用
applicationWillEnterForeground()方法


5.写一个helloWorld

bool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !CCLayer::init() )    {        return false;    }    /////////////////////////////    // 2. add a menu item with "X" image, which is clicked to quit the program    //    you may modify it.    // add a "close" icon to exit the progress. it's an autorelease object    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(                                        "CloseNormal.png",                                        "CloseSelected.png",                                        this,                                        menu_selector(HelloWorld::menuCloseCallback) );    pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );    // create menu, it's an autorelease object    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);    pMenu->setPosition( CCPointZero );    this->addChild(pMenu, 1);    /////////////////////////////    // 3. add your codes below...    // create and initialize a label    CCLabelTTF* pLabel = CCLabelTTF::create("达达's first world!", "Thonburi", 40);    CCLabelTTF* labelm  =   CCLabelTTF::create("qq群:85935444", "Thonburi", 20);    labelm->setColor(ccc3(255,0,255));        labelm->setPosition(ccp(250.0f,120.0f));        this->addChild(labelm);        // ask director the window size    CCSize size = CCDirector::sharedDirector()->getWinSize();    // position the label on the center of the screen    pLabel->setPosition( ccp(size.width / 2, size.height - 60) );    // add the label as a child to this layer    this->addChild(pLabel, 1);    CCSprite* pSprite = CCSprite::create("dada.png");    // position the sprite on the center of the screen    pSprite->setPosition(ccp(100.0f, 100.0f));        // add the sprite as a child to this layer    this->addChild(pSprite, 0);        return true;}



完成!


代码下载:http://download.csdn.net/detail/dadagm/4751176


原创粉丝点击