依据上一篇文章。创建好cocos2d——android的开发环境
先上效果图
实现该效果的代码:
package com.cn.firstgame;import org.cocos2d.layers.CCScene;import org.cocos2d.nodes.CCDirector;import org.cocos2d.opengl.CCGLSurfaceView;import android.app.Activity;import android.os.Bundle;import android.view.Menu;public class MainActivity extends Activity {//CC引擎会把图像绘制在该Viewprivate CCGLSurfaceView view;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);view=new CCGLSurfaceView(this);setContentView(view);//获得导演对象CCDirector director=CCDirector.sharedDirector();/*** 设置游戏属性*/director.attachInView(view);director.setDisplayFPS(true);director.setAnimationInterval(1.0f/30);//生成游戏场景对象CCScene scene=CCScene.node();//生成游戏布景对象GameLayer layer=new GameLayer();//将布景对象增加到场景中scene.addChild(layer);//执行布景director.runWithScene(scene);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
package com.cn.firstgame;import org.cocos2d.layers.CCLayer;public class GameLayer extends CCLayer {public GameLayer() { }}