久仰cocos2dx的大名想要研習一下一直苦於沒有時間,最近不是很忙想起這茬,準備惡補一番,先上網大體查了下資料,發現cocos2dx開發環境的搭建貌似還挺麻煩的(可能與多平臺支持有關),在官網下載了最新的cocos2dx 3.0 rc 版,配置方式參考了這篇文章http://blog.csdn.net/star530/article/details/21483729,貌似cocos2dx遊戲開發都是經過平臺開發在移植的模式,再加上「據稱」eclipse CDT+NDK的android開發模式調試困難+打包加密安全性不太好?(小白請輕拍,後續會本身嘗試一下,沒有實踐就沒有發言權嘛~~),抱着方便學習的心態先搭建了win平臺開發環境----VS2012+python2.7.6(最新官方標配),據說2.x時期還須要安裝cygwin等,想來目前版本算是輕鬆了許多。python
搭建好環境以後,官網推薦的是「HelloWorld」的cocos工程(of course),利用python建立好工程以後用VS打開運行,能夠看到一個簡單的cocos2dx遊戲界面android
咱們來看下源碼HelloWordScene.cpp安全
#include "HelloWorldScene.h" USING_NS_CC; Scene* HelloWorld::createScene() { // 'scene' is an autorelease object auto scene = Scene::create(); // 'layer' is an autorelease object auto layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); ///////////////////////////// // 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 auto closeItem = MenuItemImage::create( "CloseNormal.png", "CloseSelected.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); closeItem->setPosition(Point(origin.x + visibleSize.width - closeItem->getContentSize().width/2 , origin.y + closeItem->getContentSize().height/2)); // create menu, it's an autorelease object auto menu = Menu::create(closeItem, NULL); menu->setPosition(Point::ZERO); this->addChild(menu, 1); ///////////////////////////// // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label auto label = LabelTTF::create("Hello World", "Arial", 24); // position the label on the center of the screen label->setPosition(Point(origin.x + visibleSize.width/2, origin.y + visibleSize.height - label->getContentSize().height)); // add the label as a child to this layer this->addChild(label, 1); //add "HelloWorld" splash screen" auto sprite = Sprite::create("HelloWorld.png"); // position the sprite on the center of the screen sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); // add the sprite as a child to this layer this->addChild(sprite, 0); return true; } void HelloWorld::menuCloseCallback(Ref* pSender) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); return; #endif Director::getInstance()->end(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit(0); #endif }
能夠看到當前場景是由一個sprite對象(cocos圖標),"Hello world"的label,關機按鈕(推出程序按鈕和回調處理函數)組成,注意這句sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));由於sprite對象錨點爲圖像幾何中心點,而座標原點在左上角,所以居中對齊時須要平移屏幕中心點座標的距離,想要拉伸sprite對象到全屏,咱們能夠經過eclipse
Size spriteSzie = sprite->getContentSize(); float scaleX = spriteSzie.width/visibleSize.width; float scaleY = spriteSzie.height/visibleSize.height; //log("scaleX = %f, scaleY = %f",scaleX,scaleY); sprite->setScale(1/scaleX, 1/scaleY);
從新設置sprite對象的長寬比以適配屏幕,若是咱們想要sprite對象擁有必定的效果,以達到相似開機動畫的東東,咱們能夠利用cocos的CCAnimation對象實現動畫效果,如下是一個縮放的效果python2.7
// ccscaleto 做用:建立一個縮放的動做 // 參數1:達到縮放大小所需的時間 // 參數2 :縮放的比例 //CCAnimation* animation = CCAnimation::create(); CCActionInterval* scaleto = CCScaleTo::create(2,2); sprite->runAction(scaleto);
這樣,在啓動helloworld工程時sprite對象會出現一個縮放的動畫,如圖
函數