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 <mcanders1@gmail.com>
This commit is contained in:
jak6jak 2022-02-02 02:44:51 +00:00
parent ce752d2522
commit f991c73bdf
3 changed files with 46 additions and 0 deletions

View file

@ -119,6 +119,10 @@ path = "examples/2d/contributors.rs"
name = "many_sprites" name = "many_sprites"
path = "examples/2d/many_sprites.rs" path = "examples/2d/many_sprites.rs"
[[example]]
name = "move_sprite"
path = "examples/2d/move_sprite.rs"
[[example]] [[example]]
name = "2d_rotation" name = "2d_rotation"
path = "examples/2d/rotation.rs" path = "examples/2d/rotation.rs"

View file

@ -0,0 +1,41 @@
use bevy::prelude::*;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_startup_system(setup)
.add_system(sprite_movement)
.run();
}
#[derive(Component)]
enum Direction {
Up,
Down,
}
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.spawn_bundle(OrthographicCameraBundle::new_2d());
commands
.spawn_bundle(SpriteBundle {
texture: asset_server.load("branding/icon.png"),
transform: Transform::from_xyz(100., 0., 0.),
..Default::default()
})
.insert(Direction::Up);
}
fn sprite_movement(time: Res<Time>, mut sprite_position: Query<(&mut Direction, &mut Transform)>) {
for (mut logo, mut transform) in sprite_position.iter_mut() {
match *logo {
Direction::Up => transform.translation.y += 150. * time.delta_seconds(),
Direction::Down => transform.translation.y -= 150. * time.delta_seconds(),
}
if transform.translation.y > 200. {
*logo = Direction::Down;
} else if transform.translation.y < -200. {
*logo = Direction::Up;
}
}
}

View file

@ -84,6 +84,7 @@ Example | File | Description
--- | --- | --- --- | --- | ---
`contributors` | [`2d/contributors.rs`](./2d/contributors.rs) | Displays each contributor as a bouncy bevy-ball! `contributors` | [`2d/contributors.rs`](./2d/contributors.rs) | Displays each contributor as a bouncy bevy-ball!
`many_sprites` | [`2d/many_sprites.rs`](./2d/many_sprites.rs) | Displays many sprites in a grid arragement! Used for performance testing. `many_sprites` | [`2d/many_sprites.rs`](./2d/many_sprites.rs) | Displays many sprites in a grid arragement! Used for performance testing.
`move_sprite` | [`2d/move_sprite.rs`](./2d/move_sprite.rs) | Changes the transform of a sprite.
`mesh2d` | [`2d/mesh2d.rs`](./2d/mesh2d.rs) | Renders a 2d mesh `mesh2d` | [`2d/mesh2d.rs`](./2d/mesh2d.rs) | Renders a 2d mesh
`mesh2d_manual` | [`2d/mesh2d_manual.rs`](./2d/mesh2d_manual.rs) | Renders a custom mesh "manually" with "mid-level" renderer apis. `mesh2d_manual` | [`2d/mesh2d_manual.rs`](./2d/mesh2d_manual.rs) | Renders a custom mesh "manually" with "mid-level" renderer apis.
`rect` | [`2d/rect.rs`](./2d/rect.rs) | Renders a rectangle `rect` | [`2d/rect.rs`](./2d/rect.rs) | Renders a rectangle