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/