【问题标题】:Anchored sprite lags when the underlying sprite moves in Phaser当底层精灵在 Phaser 中移动时,锚定精灵滞后
【发布时间】:2017-05-23 09:29:33
【问题描述】:

在此链接上有一个使用 Phaser 开发的游戏示例 http://examples.phaser.io。 你的坦克是一个带有炮塔精灵的精灵:

//  The base of our tank
tank = game.add.sprite(0, 0, 'tank', 'tank1');
tank.anchor.setTo(0.5, 0.5);

...

//  Finally the turret that we place on-top of the tank body
turret = game.add.sprite(0, 0, 'tank', 'turret');
turret.anchor.setTo(0.3, 0.5);
..

还要注意每帧执行的更新函数中的以下内容:

turret.x = tank.x;
turret.y = tank.y;

请注意,当您加速时,炮塔会稍微滞后,并且仅在您达到零速度时才赶上底层坦克精灵。如何解决这个问题?

【问题讨论】:

    标签: phaser-framework


    【解决方案1】:

    有点晚了.. 你应该考虑到你的精灵使用了在不同部分渲染的物理。尝试查看 API 文档 preUpdate、postUpdate、Phaser.World。

    因此,在您的情况下,如果您使用例如 ARCADE Physics,您应该通过属于 arcade.physics 而不是其他的 body 重新加载 x,y 的数据。

    tank = game.add.sprite(0, 0, 'tank', 'tank1');
    tank.anchor.setTo(0.5, 0.5);
    game.physics.enable(tank, Phaser.Physics.ARCADE);
    
    ......
    
    turret.x = tank.body.x;
    turret.y = tank.body.y;
    

    【讨论】: