The Image will be placed at the center of the window in default, which is the origin of the camera. We can move the Image to other places by the Transform of the SpriteBundle that possesses the Image.
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
commands.spawn(SpriteBundle {
texture: asset_server.load("bevy_bird_dark.png"),
transform: Transform::from_xyz(300., 100., 0.),
..default()
});
}
The function Transform::from_xyz takes three parameters x
, y
and z
.
Positive x
moves the sprite to the right and positive y
moves the sprite to the top.
In Camera2dBundle, z
is for the drawing order of images and other 2-dimensional objects.
Objects with larger z
will be drawn on top of objects with smaller z
.
The full code is as follows:
use bevy::{
app::{App, Startup},
asset::AssetServer,
core_pipeline::core_2d::Camera2dBundle,
ecs::system::{Commands, Res},
sprite::SpriteBundle,
transform::components::Transform,
utils::default,
DefaultPlugins,
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.run();
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn(Camera2dBundle::default());
commands.spawn(SpriteBundle {
texture: asset_server.load("bevy_bird_dark.png"),
transform: Transform::from_xyz(300., 100., 0.),
..default()
});
}
Result:
➡️ Next: Rotation
📘 Back: Table of contents