无法通过“ physics.add.existing”方法添加游戏对象

时间:2019-07-13 17:03:42

标签: phaser-framework

我无法在游戏中添加现有对象

function create() {
  const scene: Phaser.Scene = this
//  scene.physics.add.image(400, 100, 'ball') THIS WORKS => BALL APEARS IN GAME

  const ball = new Phaser.GameObjects.Image(scene, 400, 100, 'ball')
  scene.physics.add.existing(ball) // nothing displays
}

我错过了什么?

2 个答案:

答案 0 :(得分:0)

var config = {
    width: 800,
    height: 600,
    type: Phaser.AUTO,
    loader: {
      baseURL: 'https://raw.githubusercontent.com/nazimboudeffa/assets/master/',
      crossOrigin: 'anonymous'
    },
    parent: 'phaser-example',
    physics: {
      default: 'arcade'
    },
    scene: {
      preload: preload,
      create: create
    }
};

var game = new Phaser.Game(config);

function preload()
{
  this.load.image('dude', 'sprites/phaser-dude.png')
}

function create ()
{
  var dude = new Phaser.GameObjects.Sprite(this, 100, 100, 'dude')
  this.add.existing(dude)
}
<script src="//cdn.jsdelivr.net/npm/phaser@3.17.0/dist/phaser.min.js"></script>

答案 1 :(得分:0)

我遇到了同样的问题,但是后来发现我必须将其添加到场景和物理管理器中,例如:

const ball = new Phaser.Physics.Arcade.Sprite(scene, 400, 100, 'ball');
this.add.existing(ball);
this.physics.add.existing(ball);