打地鼠游戏核心算法

1.添加图片背景
  var bg = new cc.Sprite("res/res/bg2.jpg");
  this.addChild(bg);
  bg.setAnchorPoint(0,0);
  bg.setPosition(cc.winSize.width*0.2,0);
2.添加敌方小怪地板
  for(var i = 0; i0.4?that.visible = true:that.visible = false;
       that.type = parseInt(Math.random() * 4);
       that.initWithSpriteFrame(cc.spriteFrameCache.getSpriteFrame(GAME_ENEMY.LGX[that.type] + ".png"));
   },GAME_CONFIG.ENEMY_SPEED);    
   var listener = cc.EventListener.create({
       event:cc.EventListener.TOUCH_ONE_BY_ONE,
       swallowTouches:true,      //吞没事件
       onTouchBegan: function (touch, event) {
            var target = event.getCurrentTarget();             //当前点击的对象
            var pos = touch.getLocation();                     //获取当前全局点击点坐标
            var s = target.getContentSize();                   //获取点击对象的尺寸
            var rect = cc.rect(0, 0, s.width, s.height);       //对象的框型区域
            var localPoint = target.convertToNodeSpace(pos);   //转换为本地坐标
            if (cc.rectContainsPoint(rect, localPoint)) {      //检测碰撞
                  if(that.visible){
                      that.visible = false;
                      target.initWithSpriteFrame(cc.spriteFrameCache.getSpriteFrame(GAME_ENEMY.LGX[that.type] + ".png"));
                      if(that.type == 0){
                          cc.eventManager.dispatchCustomEvent("SCORE",100)
                      }else{
                          cc.eventManager.dispatchCustomEvent("SCORE",-50)
                      }
                  }
                    return true
         } else {
                    return false
                }
        }
    });
        this._listener = cc.eventManager.addListener(listener,this)
5.接受怪被消灭时抛出的时间获得分数
     this._listener = cc.eventManager.addCustomListener("SCORE",this._setScore.bind(this))
    },
    _setScore:function(event){
        var data = event.getUserData();
        if(this._madeScore == 0&&data

你可能感兴趣的:(js)