如何在到达帧末尾时将其位置重置回原点? javascript

How to reset its position back to the origin when it reach the end of frame? javascript

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create});

function preload() {

  //  You can fill the preloader with as many assets as your game requires

  //  Here we are loading an image. The first parameter is the unique
  //  string by which we'll identify the image later in our code.

  //  The second parameter is the URL of the image (relative)
}

function create() {

  //  This creates a simple sprite that is using our loaded image and
  //  displays it on-screen
  //  and assign it to a variable

  var image = game.add.sprite(0, 0, 'einstein');
  game.physics.enable(image, Phaser.Physics.ARCADE);
  image.body.velocity.x=150;

  //my attempt below
  if(image.body.coordinate.x > 1){
    var image = game.add.sprite(0, 0, 'einstein');
  }
}

我目前正在尝试将图像精灵重置为坐标 0,0,当它完全离开屏幕时,所以基本上一旦它离开 canvas 它就会重置回来并一直在几个小时尝试了很多事情任何提示将不胜感激。因为我一直在尝试主动获取坐标,以便在第一个图像消失时知道何时加载图像。

谢谢

我认为 create() 方法只有在您创建它时才会被调用。这样 if 语句只被调用一次。您需要多次检查(例如每一帧)。

Phase 似乎有一个 class 可以检查您是否越界。 (onOutOfBounds) 尝试:

  function create() {

  //  This creates a simple sprite that is using our loaded image and
  //  displays it on-screen
  //  and assign it to a variable

  var image = game.add.sprite(0, 0, 'einstein');
  game.physics.enable(image, Phaser.Physics.ARCADE);
  image.body.velocity.x=150;

  image.checkWorldBounds= true;
  image.events.onOutOfBounds.add(resetimage, this);

  }
  function resetimage(image) {

    image.reset(image.x, image.y);
  }

编辑:前面的示例不适用于精灵。使用 onOutOfBounds 代替。

编辑:link 一个工作示例:Phaser out of bounds