码迷,mamicode.com
首页 > 移动开发 > 详细

cocos2d_android开发简单游戏

时间:2014-06-25 00:40:48      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:android   style   class   blog   code   java   

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个像素点向下移动,当第二张图片到原点时,反复循环

bubuko.com,布布扣

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);
  			}
  		}
  	}
  

界面:



bubuko.com,布布扣

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣


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

cocos2d_android开发简单游戏,布布扣,bubuko.com

cocos2d_android开发简单游戏

标签:android   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/mfrbuaa/p/3806163.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!