Skip to content

Commit f991c73

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 ce752d2 commit f991c73

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

Cargo.toml

+4
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

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

examples/README.md

+1
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)