Javascript 当它到达帧的末尾时,如何将其位置重置回原点?
这是一个移相器示例,我试图做的是在图像到达帧的末尾时再次加载图像。有人能解释一下怎么做吗Javascript 当它到达帧的末尾时,如何将其位置重置回原点?,javascript,jquery,html,phaser-framework,Javascript,Jquery,Html,Phaser Framework,这是一个移相器示例,我试图做的是在图像到达帧的末尾时再次加载图像。有人能解释一下怎么做吗 var game = new Phaser.Game(1500, 200, Phaser.AUTO, 'phaser-example', { preload: preload, create: create }); function preload() { // You can fill the preloader with as many assets as your game requir
var game = new Phaser.Game(1500, 200, Phaser.AUTO, '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)
game.load.image('Car', 'car.jpg');
}
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, 'Car');
game.physics.enable(image, Phaser.Physics.ARCADE);
image.body.velocity.x=750;
if (image>game.width)
{
game.load.image('Car', 'car.jpg');
var image = game.add.sprite(0, 0, 'Car');
}
}
我假设上面的代码只是伪代码,因为有很多小错误。但这种方法存在许多问题 如果您事先知道新图像需要什么,请预先加载它,然后使用loadTexture应用它:
function preload() {
game.load.image('Car', 'car.jpg');
game.load.image('Pumpkin', 'pumpkin.png');
}
...
function update() {
if (car.x > game.width) {
car.loadTexture('Pumpkin');
}
}
还有一些其他需要注意的事项:
1) 仅在尚未设置纹理的情况下更改纹理(在上面的示例中,它将反复运行loadTexture,除非重置car.x坐标)
2) 如果无法预先加载图像,则可以在播放期间加载。有关详细信息,请参阅“加载程序事件”示例代码
3) 您需要在update
中检查精灵的位置,而不是create
(因为它根本不会移动到该点)