1、在主活动中新建导演、场景、精灵等图层对象。
public class MainActivity extends AppCompatActivity { private CCDirector director; private CCScene scene; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 不显示标题栏 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 view = new CCGLSurfaceView(this); setContentView(view); // 获取导演对象 director = CCDirector.sharedDirector(); // 设置游戏引擎画面的输出目标View director.attachInView(view); // 设置游戏是否显示FPS值 director.setDisplayFPS(true); director.setScreenSize(400,800); // 设置游戏的刷新率 FPS = frame per second director.setAnimationInterval(1/60f); // 生成场景对象 scene = CCScene.node(); // 生成图层对象 PlaneLayer layer = new PlaneLayer(); // 将图层添加至场景当中 scene.addChild(layer); // 通知导演,运行场景 director.runWithScene(scene); } @Override protected void onResume() { super.onResume(); director.resume(); SoundEngine.sharedEngine().resumeSound(); //音乐继续 Toast.makeText(getApplicationContext(),"游戏开始",Toast.LENGTH_SHORT).show(); } @Override protected void onPause() { super.onPause(); director.pause(); SoundEngine.sharedEngine().pauseSound();// 暂停游戏的背景音乐 } @Override protected void onDestroy() { super.onDestroy(); director.end(); SoundEngine.sharedEngine().stopSound(); //结束声音 Toast.makeText(getApplicationContext(),"游戏结束",Toast.LENGTH_LONG).show(); } }
2、然后新建一个新的精灵图层
// 生成图层对象 PlaneLayer layer = new PlaneLayer();
/** *新建的战斗图层 */ public class PlaneLayer extends CCLayer { private CCDirector director; //添加导演 private CGSize winSize; private CCSprite sprite; //初始化英雄飞机 private boolean flag = false; //初始值flag为false private boolean firesFlag = false; //子弹发射标志 private final int KTagSprite = 1; static final int manSpeed = 300;//运行的速度 private CCSprite[] fires; //子弹 private CCSprite[] enemays; //敌机 private CCSprite[] bombsmile; //笑脸子弹 private CCSprite[] xin; //心形代表命数 private int numberOfFire; //初始化子弹的数量 private int numberOfEnemy; //初始化敌机的数量 private int numberOfbombsmile; //初始化笑脸子弹 private int numberOfxin; //初始化心 private CGPoint offset; //飞机移动的偏移量 private CGPoint point; //初始化默认点 private CGRect rect; //初始飞机所在的矩形区域 private CCSprite spritedan; //添加帧动画子弹 private CCSprite back1; //添加背景 private boolean flagsprite; private CCSprite pause11; private CCSprite continue11; public CGSize ScreenwinSize = CCDirector.sharedDirector().winSize(); //获取屏幕大小 private int pauseTag = 1; //给暂停按钮设置标记
。。。。。。。
详细连接:https://github.com/SmallSeagull/PlansGames
新手上路,见谅!