Bevy框架中NodeBundle无法移动的问题求助
问题原因
Bevy的UI实体(比如NodeBundle)的位置不由Transform组件控制——UI系统会根据Style组件的布局规则自动计算并覆盖Transform的值,所以直接修改transform.translation不会产生任何效果。
解决方案
有两种常见的修复方式:
方式1:通过Style的位置属性控制移动
因为你已经设置了PositionType::Absolute,可以直接修改Style中的left和top属性来移动UI元素:
修改你的移动系统:
fn move_node( mut query: Query<(&mut Style, &Node)>, keyboard_input: Res<ButtonInput<KeyCode>>, time: Res<Time>, ) { let (mut style, _node) = query.single_mut(); // 用delta time让移动速度与帧率无关 let speed = 100.0 * time.delta_seconds(); if keyboard_input.pressed(KeyCode::KeyD) { style.left = Val::Px(style.left.as_px().unwrap_or(0.0) + speed); } if keyboard_input.pressed(KeyCode::KeyA) { style.left = Val::Px(style.left.as_px().unwrap_or(0.0) - speed); } if keyboard_input.pressed(KeyCode::KeyS) { style.top = Val::Px(style.top.as_px().unwrap_or(0.0) - speed); } if keyboard_input.pressed(KeyCode::KeyW) { style.top = Val::Px(style.top.as_px().unwrap_or(0.0) + speed); } }
说明:把
else if改成独立if可以支持多方向同时移动(比如斜向),加入Time资源能让移动速度更流畅,不受帧率波动影响。
方式2:禁用UI布局控制,使用Transform移动
如果坚持想用Transform控制位置,给UI实体添加IgnoreLayout组件即可,这样UI系统就不会覆盖Transform的值:
修改你的实体生成代码:
commands.spawn(( NodeBundle { style: Style { position_type: PositionType::Absolute, height: Val::Px(100.0), width: Val::Px(100.0), ..Default::default() }, transform: Transform::from_translation(Vec3::ZERO), visibility: Visibility::Visible, background_color: BackgroundColor(Color::BLACK), ..Default::default() }, Node { x: 0, y: 0, kind: NodeKind::Player, }, IgnoreLayout, // 添加该组件 ));
添加后你的原move_node系统就能正常工作,但要注意:该UI元素将不再参与父节点的布局计算,位置需要完全手动控制。
内容的提问来源于stack exchange,提问作者Edgar
相关产品推荐
相关产品推荐

