//! Creates a hierarchy of parents and children entities. use std::f32::consts::*; use bevy::{color::palettes::css::*, prelude::*}; fn main() { App::new() .add_plugins(DefaultPlugins) .add_systems(Startup, setup) .add_systems(Update, rotate) .run(); } fn setup(mut commands: Commands, asset_server: Res) { commands.spawn(Camera2d); let texture = asset_server.load("branding/icon.png"); // Spawn a root entity with no parent let parent = commands .spawn(( Sprite::from_image(texture.clone()), Transform::from_scale(Vec3::splat(0.75)), )) // With that entity as a parent, run a lambda that spawns its children .with_children(|parent| { // parent is a ChildBuilder, which has a similar API to Commands parent.spawn(( Transform::from_xyz(250.0, 0.0, 0.0).with_scale(Vec3::splat(0.75)), Sprite { image: texture.clone(), color: BLUE.into(), ..default() }, )); }) // Store parent entity for next sections .id(); // Another way is to use the add_child function to add children after the parent // entity has already been spawned. let child = commands .spawn(( Sprite { image: texture, color: LIME.into(), ..default() }, Transform::from_xyz(0.0, 250.0, 0.0).with_scale(Vec3::splat(0.75)), )) .id(); // Add child to the parent. commands.entity(parent).add_child(child); } // A simple system to rotate the root entity, and rotate all its children separately fn rotate( mut commands: Commands, time: Res