Cocos2d-x《雷电大战》(6) 一枪消灭一个侵略者

前端之家收集整理的这篇文章主要介绍了Cocos2d-x《雷电大战》(6) 一枪消灭一个侵略者前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

现在有子弹了有敌机了 所以事情简单了,两者碰撞,都消失,真正做到一枪消灭一个侵略者

先重看一下完整的场景

var HelloWorldScene = cc.Scene.extend({
  onEnter:function () {

    this._super();
    //两个背景层
    var layer = new BGLayer(res.BACK4_2,res.BACK4_1,-2);
    this.addChild(layer,100)

    var bg2= new  BGLayer(res.BACK3_1,res.BACK3_2,2);
    this.addChild(bg2,200);
    //敌人来了
    var  enemyLayer=new Enemy();
    this.addChild(enemyLayer,250);

    //可以飞了
    var airLayer=new Airplane();
    this.addChild(airLayer,300);
    airLayer.enemyLayer=enemyLayer;

  }
});

2更完整的airplane,其实与前面相比.只是多了一个update,用于碰撞检测
var Airplane = cc.Layer.extend({
    ctor:function (){
      var me=this;
      me._super();
      var size=cc.winSize;

      me.air=new cc.Sprite(res.AIRPLAN);
      me.air.setPosition(size.width/2,size.height/2);
      me.addChild(me.air);

      me.batchNode=new cc.SpriteBatchNode(res.BULLET1);
      me.batchNode.retain();
      me.bullteId=1;
      me.bullteSpeed=500;

      //有3种风格可以选择
      me.bulletStyle=0; //添加子弹风格,默认为0
      var menu=new cc.Menu(new cc.MenuItemFont("子弹风格",function(sender){
        this.bulletStyle+=1;
        if(this.bulletStyle>2){this.bulletStyle =0}
      },me));
      menu.setPosition(size.width - 100,15);
      me.addChild(menu)

      cc.eventManager.addListener(cc.EventListener.create({
        event: cc.EventListener.TOUCH_ONE_BY_ONE,swallowTouches: true,onTouchBegan: me.onTouchBegan.bind(me),onTouchMoved: me.onTouchMoved.bind(me),onTouchEnded: me.onTouchEnded.bind(me),}),me);
      me.schedule(me.fire,0.5);
      me.scheduleUpdate();
      return true;
    },update:function( dt){
      this.enemyLayer.children.forEach(function(enemy){
        this.children.forEach(function(bullet){
          var enemy=this;
          var eRect=enemy.getBoundingBox();
          var bRect=bullet.getBoundingBox();
          //本层中英雄没有tag,子弹有
          if(bullet.getTag()> 0 &&  cc.rectIntersectsRect(eRect,bRect)){
            bullet.removeFromParent();
            enemy.dropMe();
          }
        }.bind(enemy));
      }.bind(this));
    },onExit:function(){
      me.batchNode.release();
    },fire:function(dt){
      var me=this;
      var point=me.air.getPosition();

      var newbullet=function(px,py,dx){
        var sp=new cc.Sprite(this.batchNode.getTexture());
        sp.setPosition(px,py);
        var bid=this.bullteId++;
        sp.setTag(bid);
        this.addChild(sp,-1);
        var flyLen= cc.winSize.height - py;
        var duration = flyLen / this.bullteSpeed;
        var k= (cc.winSize.height-py)*dx
        var action=new cc.Sequence([
          new cc.MoveTo(duration,cc.p(px + k,cc.winSize.height )),new cc.CallFunc(function(bullet,id){
            this.removeChildByTag(id);
          },this,bid)
        ]);
        sp.runAction(action);
    
      }.bind(me);

      var px=point.x;
      var py=point.y + me.air.getContentSize().height + 20;
      switch(me.bulletStyle){
        case 1:
          newbullet(px-10,0);
          newbullet(px+10,0);
          break;
        case 2:
          newbullet(px-10,-0.2);
          newbullet(px,0.2);
        default:
          newbullet(px,0);
      }
    },onTouchBegan:function(touch,e){
      var me=this;
      var size=me.air.getContentSize();
      var rect = cc.rect(0,size.width,size.height);

      var point = me.air.convertToNodeSpace(touch.getLocation());
      if(cc.rectContainsPoint(rect,point)){
        me.started=true;
        return true;
      }
      return false;
    },onTouchMoved:function(touch,e){
      var me=this;
      var size=me.air.getContentSize();
      var point = me.convertToNodeSpace(touch.getLocation());
      if(!!me.started){
        var x=point.x;
        var y=point.y;
        var between=function(v,max,min){
          if(v<=min){return min;}
          else if(v >= max){return max;}
          else{return v;}
        };

        me.air.x=between(point.x,cc.winSize.width - size.width /2,size.width / 2);
        me.air.y=between(point.y,cc.winSize.height - size.height / 2,size.height / 2);
      }
    },onTouchEnded:function(t,e){
      this.started=false;
    }
});
原文链接:https://www.f2er.com/cocos2dx/338528.html

猜你在找的Cocos2d-x相关文章