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

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

本文关键字:位置 原点      更新时间:2023-09-26

这是移相器示例之一,我试图做的是在图像到达帧末尾时再次加载图像。有人可以解释如何做到这一点吗

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的精灵位置(因为到那时它根本不会移动)。