依据上一篇文章。创建好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引擎会把图像绘制在该View private CCGLSurfaceView view; @Override protected 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); } @Override public 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() { } }
时间: 2024-10-13 09:54:37