cocos2d_android开发简单游戏

1)游戏图层设计:

public class WellcomeLayer extends CCLayer {
public WellcomeLayer() {
this.setIsTouchEnabled(true);
CCSprite background1 = CCSprite.sprite("background.jpg");
CCSprite background2 = CCSprite.sprite("background.jpg");
background1.setAnchorPoint(0, 0);
background1.setPosition(0, 0);
this.addChild(background1);
background2.setAnchorPoint(0, 0);
background2.setPosition(0, background1.getContentSize().height);
this.addChild(background2);
CCSprite logo = CCSprite.sprite("LOGO.png");
logo.setAnchorPoint(0, 0);
logo.setPosition((CCDirector.sharedDirector().winSize().width - logo.getContentSize().width)
/ 2 , CCDirector.sharedDirector().winSize().height/2);
this.addChild(logo);

}
游戏界面层
public class GameOverLayer extends CCLayer {
public GameOverLayer(int score) {
...

2)滚动背景设计

同一时候在图层放三张图片,垂直排列,而且每帧2个像素点向下移动,当第二张图片到原点时,反复循环

cocos2d_android开发简单游戏

background1 = CCSprite.sprite("background.jpg");
background2 = CCSprite.sprite("background.jpg");
background3 = CCSprite.sprite("background.jpg");
  background3.setAnchorPoint(0, 0);
background2.setAnchorPoint(0, 0);
background1.setAnchorPoint(0, 0); this.addChild(background1);
this.addChild(background2);
this.addChild(background3);
  //初始化三张背景图
  
  //使用cocos2d的定时调度
  this.schedule("backgroundMove", 0.01f);
  
  /**
   * 背景移动
   * @param dalat
   */
  public void backgroundMove(float dalat) {
   background1.setPosition(0, background1.getPosition().y - 2);
   background2.setPosition(0, background1.getPosition().y +
  background1.getContentSize().height);
  background3.setPosition(0, background2.getPosition().y + background2.getContentSize().height);
   if(background2.getPosition().y == 0) {
   background1.setPosition(0, 0);
   }
  }

2)背景音乐:

  context = CCDirector.sharedDirector().getActivity();
  SoundEngine.sharedEngine().preloadEffect(context, R.raw.game_music);
  SoundEngine.sharedEngine().playSound(context, R.raw.game_music, true);

4)精灵设计与产生:

  /**
   * 发射子弹
   * @param dalat
   */
  public void shoot(float dalat) {
   CCSprite bullet = CCSprite.sprite("bullet1.png");
   bullet.setPosition(player.getPosition().x, player.getPosition().y +
  player.getContentSize().height);
   addChild(bullet);
   CCMoveBy move = CCMoveBy.action(5, CGPoint.ccp(0, winSize.getHeight() + 10));
   bullet.runAction(move);
   this.bulletList.add(bullet);
   }
  /**
   * 随机生成敌机
   @param dalat
  */
  public void addEnemy(float dalat) {
   CCSprite enemy = CCSprite.sprite("enemy1.png");
   enemy.setPosition(random.nextInt((int)winSize.getWidth()), winSize.getHeight());
   CGPoint p = CGPoint.ccp(0, 0 - winSize.getHeight() - 20);
   addChild(enemy);
   CCMoveBy move = CCMoveBy.action(20, p);
   enemy.runAction(move);
   this.enemyList.add(enemy);
  }

5)碰撞检測

  /**
   * 推断主机是否与敌机发生碰撞
   * @param dalat
   * @return
   */
   public void boom(float dalat) {
   for(int i = 0; i < enemyList.size(); i++) {
   CCSprite enemy = enemyList.get(i);
   if(CGRect.intersects(player.getBoundingBox(), enemy.getBoundingBox())){
   CCBlink blink = CCBlink.action(1, 3);
   CCHide hide = CCHide.action();
   this.enemyList.remove(i);
   CCSequence seq = CCSequence.actions(blink, hide);
   enemy.runAction(enemyDownSeq);
   player.runAction(seq);
   new Thread(){
   public void run() {
   try {
   Thread.sleep(1000);
   } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   }
   exitGame();
   }
   }.start();
   }
   if(enemy.getPosition().y < 0) {
   this.enemyList.remove(i);
   this.removeChild(enemy, true);
   }
   }
   }
  

界面:

cocos2d_android开发简单游戏

cocos2d_android开发简单游戏cocos2d_android开发简单游戏cocos2d_android开发简单游戏

源代码:https://github.com/wujiagan/plane-war

上一篇:oracle数据库还原以及备份 包括快速备份(并发压缩)


下一篇:stm32按键识别