javascript - 当到达帧结束时如何将其位置重置回原点? javascript

标签 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');
  }
}

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

谢谢

最佳答案

我认为 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);
  }

编辑:前面的示例不适用于 Sprite 。请改用 onOutOfBounds。

编辑:链接到工作示例:Phaser out of bounds

关于javascript - 当到达帧结束时如何将其位置重置回原点? javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28818684/

相关文章:

java - 如何获得物理世界的精确高度和宽度(以米为单位)?

javascript - 如何实现#!基于链接?

javascript - NOT 在 Node/ Mongoose 中不起作用

css - 如何使用背景图像设置按钮样式

html - CSS Sprite 和图像映射

java - 球围绕 3D 球体运动

java - 在Java中创建和读取仅包含最高分数的文本文件

javascript - knockout.js中如何使用文本绑定(bind)显示选中的值?

javascript - 检索数据服务器端并使用 Next.js 保存在上下文中

css 内联列表需要宽度、高度和垂直对齐,以便在悬停时使用 sprite 进行背景图像交换