如何在到达帧结束时将其位置重置回原点? JavaScript的

时间:2015-03-02 20:14:41

标签: javascript sprite game-physics

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,当它完全离开屏幕时,所以基本上一旦它离开画布它将重置回来并且已经在它上面几个小时试了很多东西任何提示将不胜感激。因为我一直试图主动获取坐标,以便在第一个图像消失时知道何时加载图像。

谢谢

1 个答案:

答案 0 :(得分:1)

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

看起来Phase有一个类来检查你是否超出范围。 (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。

修改:指向工作示例的链接:Phaser out of bounds