Phaser矩形strage行为

时间:2017-02-18 18:58:26

标签: javascript game-engine phaser-framework

我已创建codepen以显示此错误。

更新功能:

  update: function(){
  this.timeBar.width = this.timeBar.width - 1;

设置为仅更改宽度。但是,在Chrome上查看它似乎也改变了位置。这是一个错误,还是只是在编写此代码时我没有考虑到的东西?

1 个答案:

答案 0 :(得分:0)

使用Graphics可能有点复杂,但我找到了一个使用BitmapData的例子:

  create: function(){
    var bmd = this.game.add.bitmapData(300, 40);
        bmd.ctx.beginPath();
        bmd.ctx.rect(0, 0, 300, 80);
        bmd.ctx.fillStyle = '#00685e';
        bmd.ctx.fill();

    var bglife = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, bmd);
    bglife.anchor.set(0.5);

    bmd = this.game.add.bitmapData(280, 30);
    bmd.ctx.beginPath();
        bmd.ctx.rect(0, 0, 300, 80);
        bmd.ctx.fillStyle = '#FFFFFF';
        bmd.ctx.fill();

    this.widthLife = new Phaser.Rectangle(0, 0, bmd.width, bmd.height);
    this.totalLife = bmd.width;

    this.life = this.game.add.sprite(this.game.world.centerX - bglife.width/2 + 10, this.game.world.centerY, bmd);
    this.life.anchor.y = 0.5;
    this.life.cropEnabled = true;
    this.life.crop(this.widthLife);

    this.scheduler = this.game.time.events.loop(1500, this.cropLife, this);
  },

  cropLife: function(){
    if(this.widthLife.width <= 0){
      this.widthLife.width = this.totalLife;
      this.scheduler.stop();
    }
    else{
      this.game.add.tween(this.widthLife).to( { width: (this.widthLife.width - (this.totalLife / 10)) }, 200, Phaser.Easing.Linear.None, true);
    }
  },

  update: function(){
    this.life.updateCrop();
  }

<强> You can see the discussion in detail here