是否可以使用 Phaser 在父 Sprite 后面显示子 Sprite ?我想通过渲染一个填充的绿色圆圈并将其粘贴在父 Sprite 后面,在我的 Sprite 周围绘制一个环。我知道我可以为此使用一个组,但我希望父 Sprite 受到物理影响,并且不知道如何将其与组的使用相协调。
这是我的问题的jsfiddle:http://jsfiddle.net/omm0vvfv/
<script src='https://rawgit.com/photonstorm/phaser/master/build/phaser.js'></script>
<script>
var game = this.game = new Phaser.Game(800, 400, Phaser.AUTO, '', {
create: function() {
game.physics.startSystem(Phaser.Physics.ARCADE);
var main_sprite = game.add.sprite(200, 200, 'foo-sprite')
main_sprite.anchor.setTo(.5, .5)
game.physics.arcade.enable(main_sprite)
main_sprite.body.gravity.y = 30
var bmd = this.game.make.bitmapData(main_sprite.width * 2, main_sprite.width * 2)
bmd.circle(bmd.width * .5, bmd.width * .5, main_sprite.width, '#0000ff')
var ring_sprite = this.game.make.sprite(0, 0, bmd)
ring_sprite.anchor.setTo(.5, .5)
// I can get the ring behind the sprite like this, but it doesn't stick.
var group = game.add.group()
group.add(ring_sprite)
group.add(main_sprite)
ring_sprite.x = main_sprite.x
ring_sprite.y = main_sprite.y
// If I do this it sticks, but I can't get it behind the sprite.
// main_sprite.addChild(ring_sprite)
}
})
</script>
编辑:我意识到我可以在更新函数中将环位置设置为主 Sprite 的位置:http://jsfiddle.net/jqto6z72/
但是我还有另一个问题。正如您所看到的,环似乎有点落后于 Sprite 。因此它的渲染略高于应有的高度。我认为这是因为在我设置环的位置后物理会更新。有没有办法在物理更新之后、渲染之前设置环的位置?
最佳答案
如果您需要将手动对象同步到基于物理的对象,那么我建议您在状态的 preRender
方法中执行此操作。这会在状态更新完成处理之后、渲染之前立即调用。
关于phaser-framework - 使用 Phaser 将子 Sprite 显示在父 Sprite 后面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27913568/