首页 > 解决方案 > 如何堆叠精灵元素而不通过对象丢弃

问题描述

我正在尝试堆叠一些精灵,假设它们是盒子,但是当我堆叠超过 5 个时,最低的精灵会从支撑物上掉下来,好像这组的重量太重了。

这个想法是根据需要堆叠尽可能多的盒子,但似乎我错过了一些要告诉物理引擎的东西。

您可以在此处查看工作代码:https ://codepen.io/dlaguna/pen/xMyzVW

var config = {
type: Phaser.AUTO,
width: 600,
height: 500,
physics: {
    default: 'arcade',
    arcade: {
      debug: true,
      gravity: { y: 200 }
    }
},
scene: {
    preload: preload,
    create: create
}
};

var game = new Phaser.Game(config);

function preload ()
{
    this.load.image('ground', 'https://4.bp.blogspot.com/-BzpfI1EyL4A/WrA98ayD7ZI/AAAAAAAABFA/Z1gjrdLqtRsG5O2Lp_n2odf0n_G-MVLQgCLcBGAs/s400/cccam%2Bspot.PNG');
    this.load.image('box', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT8P5hVl0_rHaIVjVJcIVXvdsQtKJGOzW92KvuoDvaitnRX2POiWQ');
}

function create ()
{
  this.ground = this.physics.add.sprite(200, 470, 'ground');
  this.ground.setImmovable(true);
  this.ground.body.setAllowGravity(false);

  this.stack = this.physics.add.group();
  this.physics.add.collider( this.stack, this.ground);

  this.elem = this.physics.add.sprite(200, 425, 'box').setScale(2);
  this.elem.setMass(0.2);
  this.physics.add.collider( this.elem, this.stack);
  this.stack.add( this.elem );

  let numElems = 6;
  for (let i = 0; i < numElems; i++) {
    this.elem = this.physics.add.sprite(200, 425 - 50*(i+1), 'box').setScale(2);
    this.elem.setMass(0.2);
    this.physics.add.collider( this.elem, this.stack);
    this.stack.add( this.elem );
  }
}

将numElems变量更改为 5 显示了场景的工作方式。

标签: javascriptphaser-framework

解决方案


您需要自定义分开。 在这里提示。似乎可以找到工作,但不确定这到底是如何工作的。

var config = {
type: Phaser.AUTO,
width: 600,
height: 500,
physics: {
    default: 'arcade',
    arcade: {
      debug: true,
      gravity: { y: 200 }
    }
},
scene: {
    preload: preload,
    create: create
}
};

var game = new Phaser.Game(config);

function preload ()
{
    this.load.image('ground', 'https://4.bp.blogspot.com/-BzpfI1EyL4A/WrA98ayD7ZI/AAAAAAAABFA/Z1gjrdLqtRsG5O2Lp_n2odf0n_G-MVLQgCLcBGAs/s400/cccam%2Bspot.PNG');
    this.load.image('box', 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT8P5hVl0_rHaIVjVJcIVXvdsQtKJGOzW92KvuoDvaitnRX2POiWQ');
}

function create ()
{
  this.ground = this.physics.add.sprite(200, 470, 'ground');
  this.ground.setImmovable(true);
  this.ground.body.setAllowGravity(false);

  this.stack = this.physics.add.group();
  this.physics.add.collider( this.stack, this.ground);

  let numElems = 8;
  for (let i = 0; i < numElems; i++) {
    this.elem = this.physics.add.sprite(200, 425 - 50*(i+1), 'box').setScale(2);
    this.elem.setMass(0.2);
    this.elem.customSeparateY = true;
    this.physics.add.collider(this.stack, this.elem, function (s1, s2) {
          var b1 = s1.body;
          var b2 = s2.body;

          if (b1.y > b2.y) {
              b2.y += (b1.top - b2.bottom);
              b2.stop();
          }
          else {
              b1.y += (b2.top - b1.bottom);
              b1.stop();
          }
      });

    this.stack.add( this.elem );
  }
}
    </script>

</body>
</html>

推荐阅读