下载Cocos2d-android的源代码:cocos2d-android-1
git@github.com:ZhouWeikuan/cocos2d.git
将项目导入到eclipse中。运行实例;
1.首先创建一个android项目,然后将lib导入到项目中
2.对MainActivity.java编写
1)首先创建一个view对象
private CCGLSurfaceView view = null;
2)取得 CCDirector 对象
CCDirector director = CCDirector.sharedDirector();
3)设置应用程序的属性
1 //设置当前游戏中所使用的view对象
2 director.attachInView(view);
3 //设置是否显示fps
4 director.setDisplayFPS(true);
5 //设置游戏渲染一帧数据所需要的时间(fps=30)
6 director.setAnimationInterval(1/30.0f);
7
8 //生成一个游戏场景对象
9 CCScene scene = CCScene.node();
10
11 //生成一个布景层对象
12 GameLayer gameLayer = new GameLayer();
13 //将布景层对象添加至游戏场景中
14 scene.addChild(gameLayer);
15
16 //运行游戏场景
17 director.runWithScene(scene);
18
19 //setContentView(R.layout.activity_main);
20 setContentView(view);
完整代码:
![](http://static.oschina.net/uploads/img/201503/22194603_XwMh.gif)
![](http://static.oschina.net/uploads/img/201503/22194603_lkix.gif)
1 import android.os.Bundle;
2 import android.app.Activity;
3 import android.view.Menu;
4
5 import org.cocos2d.layers.CCScene;
6 import org.cocos2d.nodes.CCDirector;
7 import org.cocos2d.opengl.CCGLSurfaceView;
8
9 public class MainActivity extends Activity {
10
11 private CCGLSurfaceView view = null;
12
13 @Override
14 protected void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16
17 view = new CCGLSurfaceView(this);
18
19 //取得 CCDirector 对象
20 CCDirector director = CCDirector.sharedDirector();
21
22 //设置应用程序的属性
23 //设置当前游戏中所使用的view对象
24 director.attachInView(view);
25 //设置是否显示fps
26 director.setDisplayFPS(true);
27 //设置游戏渲染一帧数据所需要的时间(fps=30)
28 director.setAnimationInterval(1/30.0f);
29
30 //生成一个游戏场景对象
31 CCScene scene = CCScene.node();
32
33 //生成一个布景层对象
34 GameLayer gameLayer = new GameLayer();
35 //将布景层对象添加至游戏场景中
36 scene.addChild(gameLayer);
37
38 //运行游戏场景
39 director.runWithScene(scene);
40
41 //setContentView(R.layout.activity_main);
42 setContentView(view);
43 }
44
45 @Override
46 public boolean onCreateOptionsMenu(Menu menu) {
47 // Inflate the menu; this adds items to the action bar if it is present.
48 getMenuInflater().inflate(R.menu.main, menu);
49 return true;
50 }
51
52 }
![](http://static.oschina.net/uploads/img/201503/22194603_XwMh.gif)
![](http://static.oschina.net/uploads/img/201503/22194603_lkix.gif)
1 import org.cocos2d.layers.CCLayer;
2
3
4 public class GameLayer extends CCLayer {
5 public GameLayer(){
6
7 }
8 }
直接运行就行。