好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Phaser实现2048

//score
var scoreSprite = game.add.sprite(10, 10);
var scoreGraphics = game.add.graphics(0, 0);
scoreGraphics.lineStyle(5, 0xA1C5C5);
scoreGraphics.beginFill(0x308C8C);
scoreGraphics.drawRoundedRect(0, 0, 70, 50, 10);
scoreGraphics.endFill();
scoreSprite.addChild(scoreGraphics);
var scoreTitle = game.add.text(0, 5, "SCORE", titleStyle);
scoreTitle.setTextBounds(0, 0, 70, 50);
scoreSprite.addChild(scoreTitle);
this.scoreText = game.add.text(0, 20, this.score, scoreStyle);
this.scoreText.setTextBounds(0, 0, 70, 50);
scoreSprite.addChild(this.scoreText);
// swipe的公共逻辑抽出
this.swipeCommon = function(i, j, arrNode, posJson, condition, nextArrNode, nextPosJson) {
  var that = this;
  var duration = 100;
  // 遇到了可以合并的
  if(!arrNode.newNode && arrNode.value == this.array[i][j].value) {
    arrNode.value = arrNode.value * 2;
    arrNode.newNode = true;
    this.array[i][j].value = 0;
    this.score = this.score + arrNode.value;
    this.scoreText.text = this.score;
    if(this.score > this.best) {
      this.best = this.score;
      this.bestText.text = this.best;
    }
    // 渐渐透明后被kill掉
    var t1 = game.add.tween(arrNode.sprite).to({alpha: 0}, duration, Phaser.Easing.Linear.None, true);
    t1.onComplete.add(function() {
      this.sprite.kill();
      that.placeSquare(this.x, this.y, this.value);
      if(!that.canSwipe) {
        that.canSwipe = true;
        that.generateSquare();
      }
    }, arrNode);
    var t2 = game.add.tween(this.array[i][j].sprite).to({alpha: 0}, duration, Phaser.Easing.Linear.None, true);
    t2.onComplete.add(function() {
      this.kill();
      if(!that.canSwipe) {
        that.canSwipe = true;
        that.generateSquare();
      }
    }, this.array[i][j].sprite);
    game.add.tween(this.array[i][j].sprite).to(posJson, duration, Phaser.Easing.Linear.None, true);
    arrNode.sprite = this.array[i][j].sprite;
    this.array[i][j].sprite = undefined;
  } else if(arrNode.value == 0) {
    arrNode.value = this.array[i][j].value;
    this.array[i][j].value = 0;
    var t = game.add.tween(this.array[i][j].sprite).to(posJson, duration, Phaser.Easing.Linear.None, true);
    t.onComplete.add(function() {
      if(!that.canSwipe) {
        that.canSwipe = true;
        that.generateSquare();
      }
    });
    arrNode.sprite = this.array[i][j].sprite;
    this.array[i][j].sprite = undefined;
  } else if(condition) {
    nextArrNode.value = this.array[i][j].value;
    this.array[i][j].value = 0;
    var t = game.add.tween(this.array[i][j].sprite).to(nextPosJson, duration, Phaser.Easing.Linear.None, true);
    t.onComplete.add(function() {
      if(!that.canSwipe) {
        that.canSwipe = true;
        that.generateSquare();
      }
    });
    nextArrNode.sprite = this.array[i][j].sprite;
    this.array[i][j].sprite = undefined;
  }
};
// add swipe check
this.swipe = new Swipe(this.game, this.swipeCheck);
// swipe检测
this.swipeCheck = {
  up: this.swipeUp.bind(this),
  down: this.swipeDown.bind(this),
  left: this.swipeLeft.bind(this),
  right: this.swipeRight.bind(this)
};
// 在x,y位置放置一个值为value的方块
this.placeSquare = function(x, y, value) {
  var squareStyle = { font: "bold 20px Arial", fill: "#FFFFFF", boundsAlignH: "center", boundsAlignV: "middle" };
  var square = game.add.sprite();
  square.reset(this.transX(x), this.transY(y));
  var squareBackground = game.add.graphics(-45/2, -45/2);
  squareBackground.beginFill(this.colors[value]);
  squareBackground.drawRoundedRect(0, 0, 45, 45, 5);
  squareBackground.endFill();
  square.addChild(squareBackground);
  var squareText = game.add.text(-45/2, -45/2, value, squareStyle);
  squareText.setTextBounds(0, 0, 45, 45);
  square.addChild(squareText);
  this.array[x][y].value = value;
  this.array[x][y].sprite = square;
  square.anchor.setTo(0.5, 0.5);
  square.scale.setTo(0.0, 0.0);
  var tween = game.add.tween(square.scale).to({x:1.0, y:1.0}, 100, Phaser.Easing.Sinusoidal.InOut, true);
  tween.onComplete.add(function() {
    if(this.checkGameover()) {
      this.gameOver();
    }
  }, this);
};

查看更多关于Phaser实现2048的详细内容...

  阅读:51次