-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## 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]>
- Loading branch information
Showing
3 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use bevy::prelude::*; | ||
|
||
fn main() { | ||
App::build() | ||
.add_plugins(DefaultPlugins) | ||
.add_startup_system(setup) | ||
.add_system(sprite_movement) | ||
.run(); | ||
} | ||
|
||
enum Direction { | ||
Up, | ||
Down, | ||
} | ||
|
||
fn setup( | ||
mut commands: Commands, | ||
asset_server: Res<AssetServer>, | ||
mut materials: ResMut<Assets<ColorMaterial>>, | ||
) { | ||
let texture_handle = asset_server.load("branding/icon.png"); | ||
commands.spawn_bundle(OrthographicCameraBundle::new_2d()); | ||
commands | ||
.spawn_bundle(SpriteBundle { | ||
material: materials.add(texture_handle.into()), | ||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters