一、前言:
Cocos2d-x是目前非常流行的開源移動2D游戲框架。本文HelloWorld示例程序中使用的Cocos2d-x版本是2.0,主要實現一個簡單的入門程序。
二、HelloWorld程序:
HelloWorld程序是很多編程語言的入門程序,對于程序員來說非常重要。
打開本文項目后可以看到AppDelegate.h/.cpp和HelloWorldScene.h/.cpp四個文件,比一般初學編程看到的HelloWorld要稍顯復雜。
具體代碼如下:
#include "AppDelegate.h" #include "HelloWorldScene.h" USING_NS_CC; AppDelegate::AppDelegate() { } AppDelegate::~AppDelegate() { } bool AppDelegate::applicationDidFinishLaunching() { // 初始化CCDirector對象 CCDirector* pDirector = CCDirector::sharedDirector(); // 初始化CCEGLView對象,CCEGLView是顯示窗口,負責窗口級別的功能管理和實現,包括坐標和縮放管理、畫圖工具、按鍵事件 CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); // 將pEGLView傳遞給pDirector pDirector->setOpenGLView(pEGLView); // 打開狀態顯示,包括FPS等 pDirector->setDisplayStats(true); // 設置FPS,每秒刷新多少幀畫面,默認是1秒60幀,幀數越高畫面越流暢,但也越耗電 pDirector->setAnimationInterval(1.0 / 60); // 創建一個HelloWorld場景,能夠自動釋放 CCScene *pScene = HelloWorld::scene(); // 運行HelloWorld場景 pDirector->runWithScene(pScene); return true; } // 來電或者應用進入手機后臺將調用此方法 void AppDelegate::applicationDidEnterBackground() { // 停止所有動畫 CCDirector::sharedDirector()->stopAnimation(); // 如果使用了SimpleAudioEngine(控制背景音樂等),在此處調用暫停 // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic(); } // 當應用從后臺恢復至前臺將調用此方法 void AppDelegate::applicationWillEnterForeground() { // 恢復所有動畫 CCDirector::sharedDirector()->startAnimation(); // 在此處調用SimpleAudioEngine的恢復 // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); }
AppDelegate對Cocos2d-x引擎進行了初始化,并對進行一些全局性的設置。
但是在這個里面并沒有看到具體的界面實現,因為界面實現都在HelloWorldScene中。
#include "HelloWorldScene.h" USING_NS_CC; CCScene* HelloWorld::scene() { // 創建一個Scene CCScene *scene = CCScene::create(); // 創建一個HelloWorld的圖層(HelloWorld繼承自CCLayer) HelloWorld *layer = HelloWorld::create(); // 將創建的HelloWorld圖層添加至之前創建的場景中 scene->addChild(layer); // 返回創建的場景 return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ///////////////////////////// // 1. 調用父類的初始化,如果初始化失敗,則不會繼續往下執行 if ( !CCLayer::init() ) { // 返回false表示初始化失敗 return false; } //獲取可顯示區域大小 CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); //獲取可顯示區域坐標起點 CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); ///////////////////////////// // 2. 添加一個可點擊的菜單按鈕,點擊后關閉程序 // 創建一個圖片菜單選項 CCMenuItemImage *pCloseItem = CCMenuItemImage::create(// 調用創建方法 "CloseNormal.png",// 設置未點擊時菜單圖片 "CloseSelected.png",// 設置點擊時候菜單圖片 this,// ?這個參數是什么 menu_selector(HelloWorld::menuCloseCallback));// 設置菜單點擊時間的回調監聽 // 設置菜單的位置坐標,pCloseItem->getContentSize()用來獲取菜單選項大小 pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); // 創建菜單(菜單選項需要添加到菜單里才能使用),create函數中可以添加多個菜單選項,以NULL結束添加 CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); // 設置菜單的坐標(CCPointZero是坐標(0,0)) pMenu->setPosition(CCPointZero); // 將菜單添加至HelloWorld圖層中,1是菜單在HelloWorld圖層中Z軸位置,數值越大,顯示的層級越高,不易被遮擋 this->addChild(pMenu, 1); ///////////////////////////// // 3. 添加文字控件和背景圖片 // 創建一個文件控件,create函數中參數分別是“控件需要顯示的文字”,“控件文字字體”,“控件文字字號” CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); // 設置文件控件位置(此公式計算的位置為屏幕中央) pLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y + visibleSize.height - pLabel->getContentSize().height)); // 將文字控件添加至HelloWorld圖層中 this->addChild(pLabel, 1); // 創建一個精靈(后續將介紹精靈的具體用處,這里精靈是背景圖片的載體) CCSprite* pSprite = CCSprite::create("HelloWorld.png"); // 設置背景圖片位置(此公式計算的位置為屏幕中央) pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // 將背景圖片添加至HelloWorld圖層中,并設置Z軸為0,置于菜單和文字之下 this->addChild(pSprite, 0); // 返回true表示初始化成功 return true; } // 關閉按鈕的回調函數,pSender傳遞的是調用了該函數的對象 void HelloWorld::menuCloseCallback(CCObject* pSender) { // 宏定義,判斷是否是WinRT或者WP8設備 #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) // 彈出對話框,提示文字信息 CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); #else // 調用CCDirector的end()函數,結束游戲 CCDirector::sharedDirector()->end(); // 宏定義,判斷是否是IOS設備 #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) // 直接調用exit(0)結束游戲 exit(0); #endif #endif }
HelloWorldScene文件是整個HelloWorld工程的核心,從代碼中我們不難發現,在Cocos2d-x的坐標系計算中,默認將屏幕左下角設置為坐標原點,向上方和右方遞增Y軸X軸。而設置控件位置的時候,是以控件的中心為錨點,當然,錨點是可以通過代碼改變的,這里我們需要調用setAnchorPoint()函數。
希望本文所述實例對于大家學習Cocos2d-x能起到一定的幫助作用。
|
新聞熱點
疑難解答
圖片精選