cocos2d-android-1 使用方法

來源:互聯網
上載者:User

轉自:http://hi.baidu.com/184367426/blog/item/b1f4c6396c798412baa1678b.html

現在最新的工程已經轉移到 https://github.com/ZhouWeikuan/cocos2d

此篇博文講解最基本cocos2d-android-1 使用方法,主要參考此文

對於cocos2d以及cocos2d-android-1 不瞭解的銅鞋移步這裡
或者這裡

OK, 回到正題,在開發之前需要做如下的準備工作:

  • android開發環境
  • 到這裡下載最新的cocos2d-template.zip 解壓.
  • 在eclipse將剛剛解壓的cocos2d-template項目import進來,把項目名字改成你的遊戲名,如TankWar等. 現在就可以在這個模板之上做之後遊戲開發了.

這個遊戲模板的運行結果如:遊戲的包括左下角的即時幀數顯示,以及頁面當中的touch點的座標顯示,所以如果想要添加其它豐富的內容的話要靠自己動手來DIY了。

觀察這個project,有幾個重要的部分要說明一下,首先這是個android Project,所以做過一些簡單開發的就應該能看出來;  其次,在assets目錄中有一個fps_images.png圖片,左下角的fps所用的字型就是來自於這個檔案,後面還會發現,遊戲中的的素材,一般情況下都是放到assets目錄下的;最後,在libs目錄裡是cocos2d-android.jar,是個cocos2d-android-1引擎,它實現了諸如情境(Scene),圖層(Layer),精靈(Sprite),以及遊戲過程中需要用到的各種運動和效果,它是整個項目裡最重要的部分。

在src檔案夾裡有只有一個GameActivity.java的源檔案,下面說明一下檔案裡重要的部分:

1234567 ...import
android.app.Activity;
import
android.os.Bundle;
import
android.view.MotionEvent;
import
android.view.Window;
import
android.view.WindowManager;
public
class
GameActivity extends
Activity {

GameActivity是一個Android的Activity,包含著一個Activity從create 到destory的所有方法:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 /** Called when the activity is first created. */    @Override    public
void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);         // set the window status, no tile, full screen and don't sleep        //windows 無標題,全屏,不休眠        requestWindowFeature(Window.FEATURE_NO_TITLE);        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                WindowManager.LayoutParams.FLAG_FULLSCREEN);        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,                WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);         CCGLSurfaceView mGLSurfaceView;         mGLSurfaceView =
new CCGLSurfaceView(this);         setContentView(mGLSurfaceView);         // attach the OpenGL view to a window        //串連opengl與當前視窗,這個的原理我也不是很懂...        CCDirector.sharedDirector().attachInView(mGLSurfaceView);         // no effect here because device orientation is controlled by manifest        CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);         // show FPS        // set false to disable FPS display, but don't delete fps_images.png!!        //顯示fps        CCDirector.sharedDirector().setDisplayFPS(true);         // frames per second // 幀數        CCDirector.sharedDirector().setAnimationInterval(1.0f /
60);         //建立情境        CCScene scene = TemplateLayer.scene();         // Make the Scene active 開始運行情境        CCDirector.sharedDirector().runWithScene(scene);    }     @Override    public
void onStart() {        super.onStart();    }     @Override    public
void onPause() {        super.onPause();         CCDirector.sharedDirector().pause();    }     @Override    public
void onResume() {        super.onResume();         CCDirector.sharedDirector().resume();    }     @Override    public
void onDestroy() {        super.onDestroy();         CCDirector.sharedDirector().end();    }

下面是GameActivity類中的的TemplateLayer類,

123456789101112131415161718192021222324252627282930313233343536373839 static
class
TemplateLayer extends
CCLayer {     CCLabel lbl;      public
static CCScene scene() {         CCScene scene = CCScene.node();         CCLayer layer =
new TemplateLayer();          scene.addChild(layer);          return
scene;     }      protected
TemplateLayer() {          this.setIsTouchEnabled(true);          lbl = CCLabel.makeLabel("Hello World!",
"DroidSans",
24);          addChild(lbl,
0);         lbl.setPosition(CGPoint.ccp(160,
240));      }      @Override     public
boolean ccTouchesBegan(MotionEvent event) {         CGPoint convertedLocation = CCDirector.sharedDirector()             .convertToGL(CGPoint.make(event.getX(), event.getY()));          String title = String.format("touch at point(%.2f, %.2f)",                     convertedLocation.x, convertedLocation.y);          if
(lbl != null) {             lbl.setString(title);         }          return
CCTouchDispatcher.kEventHandled;     }  }

注意看scene()方法,事實上CCDirector.sharedDirector()需要一個CCScene 做為開始點,而Scence需要一個CCLayer 做為開始點,TemplateLayer scence()方法返回了一個包含著CCLayer的CCScence給Director,就是這樣子。其它的部分,layer裡放置了一個label用來顯示上次touch事件位置,重載了ccTouchesBegan(touch donw)事件,在這個方法裡做label的更新,就是這樣子。

到此,一個簡單的基於cocos2d-android-1引擎的不怎麼算得上遊戲的遊戲已經跑通了,呼~ 我也休息一下,查一些東西,過些天再整理一下CCSprite相關的東西。
=============================

Ps,使用模板和現成的jar的好處是方便,你花幾鐘就可以搭好一個東西,開始把精力投放在耗時耗力的關卡上了。不過現階段cocos2d-android-1還不是很成熟,有時候會出現一些詭異的事情,這種情況下就無法通過jar包來定位問題到底出在哪裡,所以前段時間我的實驗過程用的都是從這裡checkout出的代碼,這樣一是解決了調試的問題; 再就是還可以更加具體和有針對性對將問題反饋給開發人員
; 而且源碼裡還有一個cocos2d的示範demo,我們可以參考這個demo來開始進行實驗工作 : )

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.