You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Phaser中RotateAroundDistance旋转时碰撞盒不跟随问题求助

解决Phaser中旋转精灵时碰撞盒不跟随的问题

问题出在你使用了staticGroup——静态物理体的位置发生变化后,Phaser不会自动同步碰撞盒的位置,因为静态体的设计初衷是作为固定不动的障碍物。

下面是两种可行的解决方案:

方案1:改用动态组并设置不可移动(推荐)

把静态组替换为动态物理组,同时开启immovable: true配置,这样既可以让碰撞盒跟随精灵同步移动,又能保证它不会被其他物理对象推动:

function preload() 
{
    // 加载火焰图片
    this.load.image('fire', 'assets/fire.png')
}
function create()
{
    // 创建旋转火焰组,使用动态组并配置不可移动、关闭重力
    spinningfire = this.physics.add.group({
        immovable: true,
        allowGravity: false
    });
    spinningfire.create(600, -1600, 'fire')
}
function update()
{
    Phaser.Actions.RotateAroundDistance(spinningfire.getChildren(), { 
        x: 600, y: -1780 
    }, 0.02, 180)
}

方案2:手动同步静态体的碰撞盒(仅当必须用静态组时)

如果坚持使用staticGroup,每次旋转操作后需要手动调用body.updateFromGameObject(),强制让碰撞盒与精灵位置同步:

function update()
{
    Phaser.Actions.RotateAroundDistance(spinningfire.getChildren(), { 
        x: 600, y: -1780 
    }, 0.02, 180)
    
    // 遍历所有精灵,同步碰撞盒位置
    spinningfire.getChildren().forEach(sprite => {
        sprite.body.updateFromGameObject();
    });
}

注意:静态物理体频繁移动会影响物理引擎的性能,因此优先推荐方案1。

内容的提问来源于stack exchange,提问作者Bilal Siddiqui

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 09:00:02