Как складывать спрайт-элементы, не удаляя их через объект - PullRequest
0 голосов
/ 15 февраля 2019

Я пытаюсь сложить несколько спрайтов, скажем, что они являются коробками, но когда я укладываю больше 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 );
  }
}

Изменение переменной numElems5 показывает, как может работать сцена.

Есть идеи?

1 Ответ

0 голосов
/ 24 февраля 2019

Вам нужно на заказ отдельно. подсказка здесь .Кажется, это работает, найти, но не уверен, как именно это работает.

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>
...