I have a node bundle like this:
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,
},
));
and I want to move this node:
fn move_node(
mut node: Query<(&mut Transform, &mut Node)>,
keyboard_input: Res<ButtonInput<KeyCode>>,
) {
let (mut transform, mut node) = node.single_mut();
if keyboard_input.pressed(KeyCode::KeyD) {
transform.translation.x += 100.0;
} else if keyboard_input.pressed(KeyCode::KeyS) {
transform.translation.y += 100.0;
} else if keyboard_input.pressed(KeyCode::KeyA) {
transform.translation.x -= 100.0;
} else if keyboard_input.pressed(KeyCode::KeyW) {
transform.translation.y -= 100.0;
}
}
but I find that I just can not move it ? I am new to bevy, could anyone help me, thanks a lot !!
I believe the layout for UI nodes are continually re-calculated. The Transform
is kept in-sync after the fact so it effectively read-only. Your changes are just being overridden when the UI is rendered.
So if you want to move it, you should do so by modifying the Style
- probably with the left
and top
fields though it uses CSS-based layouts that can be much more complicated.
style: Style {
position_type: PositionType::Absolute,
left: Val::Px(100.0),
top: Val::Px(100.0),
height: Val::Px(100.0),
width: Val::Px(100.0),
..Default::default()
}
fn move_node(
mut node: Query<(&mut Style, &mut Node)>,
keyboard_input: Res<ButtonInput<KeyCode>>,
) {
let (mut style, mut node) = node.single_mut();
if keyboard_input.pressed(KeyCode::KeyD) {
if let Val::Px(left) = &mut style.left {
*left += 10.0;
}
} else if keyboard_input.pressed(KeyCode::KeyS) {
if let Val::Px(top) = &mut style.top {
*top += 10.0;
}
} else if keyboard_input.pressed(KeyCode::KeyA) {
if let Val::Px(left) = &mut style.left {
*left -= 10.0;
}
} else if keyboard_input.pressed(KeyCode::KeyW) {
if let Val::Px(top) = &mut style.top {
*top -= 10.0;
}
}
}
Note: I see this is for you own Node
with NodeKind::Player
; its probably a bad idea to use UI components for the core of your game. There are plenty of examples for doing 2d games.