本节简单对新生成的hellowrold 项目相关代码进行简单分析,具体以代码注释的方式展示给大家。代码相对简单些,在此不作过多赘述,直接上码:
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow){ // UNREFERENCED_PARAMETER 避免编译器关于未引用参数的警告 UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); //新建一个代理实体 AppDelegate app; CCEGLView* eglView = CCEGLView::sharedOpenGLView(); // 初始化win32下游戏窗体大小及标题等信息 eglView->setViewName(UTEXT("这是一个测试啦")); eglView->setFrameSize(900, 600); return CCApplication::sharedApplication()->run();}
bool AppDelegate::applicationDidFinishLaunching() { //加载配置文件信息,用于防止中文乱码及国际化 CCConfiguration::sharedConfiguration()->loadConfigFile("config/strings.plist"); // 初始化导演雷 CCDirector* pDirector = CCDirector::sharedDirector(); CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); pDirector->setOpenGLView(pEGLView); // 设置是否限制帧频 pDirector->setDisplayStats(true); // 设置刷新频率,默认为 1.0 / 60 pDirector->setAnimationInterval(1.0 / 60); // 创建启动场景 CCScene *pScene = StartScene::scene(); // run pDirector->runWithScene(pScene); return true;}
CCScene* StartScene::scene(){ // 创建一个场景 CCScene *scene = CCScene::create(); StartScene *layer = StartScene::create(); // 设置子节点,以后你会碰到很多addChild scene->addChild(layer); // return the scene return scene;}bool StartScene::init(){ // 初始化layer 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(StartScene::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 - 20, origin.y + visibleSize.height - pCloseItem->getContentSize().height/2 - 20)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); // 创建一个文本标签 CCConfiguration *conf = CCConfiguration::sharedConfiguration(); const char *helloStr = conf->getCString("hello", "unknown"); CCLabelTTF* pLabel = CCLabelTTF::create(helloStr, "Arial", 24); pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); this->addChild(pLabel, 1); // 生成背景图片,此处使用sprite CCSprite* pSprite = CCSprite::create("HelloWorld.png"); pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); this->addChild(pSprite, 0); // this->setRotation(90.0F); return true;}// 点击退出按钮回调函数void StartScene::menuCloseCallback(CCObject* pSender){ CCDirector::sharedDirector()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0);#endif}