Correct way to spawn sprite #9190
Answered
by
mockersf
bfg-coding
asked this question in
Q&A
-
I just finished a Bevy 0.10 tutorial series and wanted to apply my knowledge and failed instantly on trying to spawn my player like I did in the tutorial. What is the most correct way to spawn something into the engine for 2D games. Code below for starting point fn spawn_player(
mut commands: Commands,
window_query: Query<&Window, With<PrimaryWindow>>,
asset_server: Res<AssetServer>
) {
let window = window_query.get_single().unwrap();
commands.spawn(
(
SpriteBundle {
transform: Transform::from_xyz(window.width() / 2.0, window.height() / 2.0, 0.0),
texture: asset_server.load("sprites/player/player.png"),
..default()
},
Player {}
)
);
} |
Beta Was this translation helpful? Give feedback.
Answered by
mockersf
Jul 18, 2023
Replies: 1 comment 1 reply
-
I'm not sure why you're using the window to move your sprite. I think that transform would move it just at the border of the screen. You can look at the sprite example for how to spawn a sprite. Lines 12 to 18 in 8ba9571 |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
bfg-coding
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not sure why you're using the window to move your sprite. I think that transform would move it just at the border of the screen.
You also need to spawn a camera.
You can look at the sprite example for how to spawn a sprite.
bevy/examples/2d/sprite.rs
Lines 12 to 18 in 8ba9571