Skip to content

Commit 5acd11f

Browse files
jak6jakcart
andcommitted
Add move sprite example. (#2414)
## Objective There is no bevy example that shows how to transform a sprite. At least as its singular purpose. This creates an example of how to use transform.translate to move a sprite up and down. The last pull request had issues that I couldn't fix so I created a new one ### Solution I created move_sprite example. Co-authored-by: Carter Anderson <[email protected]>
1 parent 6a499b1 commit 5acd11f

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ path = "examples/2d/contributors.rs"
119119
name = "many_sprites"
120120
path = "examples/2d/many_sprites.rs"
121121

122+
[[example]]
123+
name = "move_sprite"
124+
path = "examples/2d/move_sprite.rs"
125+
122126
[[example]]
123127
name = "2d_rotation"
124128
path = "examples/2d/rotation.rs"

examples/2d/move_sprite.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use bevy::prelude::*;
2+
3+
fn main() {
4+
App::build()
5+
.add_plugins(DefaultPlugins)
6+
.add_startup_system(setup)
7+
.add_system(sprite_movement)
8+
.run();
9+
}
10+
11+
enum Direction {
12+
Up,
13+
Down,
14+
}
15+
16+
fn setup(
17+
mut commands: Commands,
18+
asset_server: Res<AssetServer>,
19+
mut materials: ResMut<Assets<ColorMaterial>>,
20+
) {
21+
let texture_handle = asset_server.load("branding/icon.png");
22+
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
23+
commands
24+
.spawn_bundle(SpriteBundle {
25+
material: materials.add(texture_handle.into()),
26+
transform: Transform::from_xyz(100., 0., 0.),
27+
..Default::default()
28+
})
29+
.insert(Direction::Up);
30+
}
31+
32+
fn sprite_movement(time: Res<Time>, mut sprite_position: Query<(&mut Direction, &mut Transform)>) {
33+
for (mut logo, mut transform) in sprite_position.iter_mut() {
34+
match *logo {
35+
Direction::Up => transform.translation.y += 150. * time.delta_seconds(),
36+
Direction::Down => transform.translation.y -= 150. * time.delta_seconds(),
37+
}
38+
39+
if transform.translation.y > 200. {
40+
*logo = Direction::Down;
41+
} else if transform.translation.y < -200. {
42+
*logo = Direction::Up;
43+
}
44+
}
45+
}

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ Example | File | Description
8484
--- | --- | ---
8585
`contributors` | [`2d/contributors.rs`](./2d/contributors.rs) | Displays each contributor as a bouncy bevy-ball!
8686
`many_sprites` | [`2d/many_sprites.rs`](./2d/many_sprites.rs) | Displays many sprites in a grid arragement! Used for performance testing.
87+
`move_sprite` | [`2d/move_sprite.rs`](./2d/move_sprite.rs) | Changes the transform of a sprite.
8788
`mesh2d` | [`2d/mesh2d.rs`](./2d/mesh2d.rs) | Renders a 2d mesh
8889
`mesh2d_manual` | [`2d/mesh2d_manual.rs`](./2d/mesh2d_manual.rs) | Renders a custom mesh "manually" with "mid-level" renderer apis.
8990
`rect` | [`2d/rect.rs`](./2d/rect.rs) | Renders a rectangle

0 commit comments

Comments
 (0)