#sprite not displayed

22 messages · Page 1 of 1 (latest)

versed hare
#
use bevy::prelude::*;
use bevy::window::PrimaryWindow;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_systems(Startup, (spawn_player, spawn_camera))
        .run()
}

#[derive(Component)]
struct Player {}

fn spawn_player(mut commands: Commands, window_query: Query<&Window, With<PrimaryWindow>>, asset_server: Res<AssetServer>, ) {
    let window: &Window = window_query.get_single().unwrap();

    commands.spawn((
        SpriteBundle {
            transform: Transform::from_xyz(window.width() / 2.0, window.height() / 2.0, 0.0),
            texture: asset_server.load("assets/sprites/ball_blue_large.png"),
            ..default()
        },
        Player {},
    ));
}

fn spawn_camera(mut commands: Commands, window_query: Query<&Window, With<PrimaryWindow>>) {
    let window: &Window = window_query.get_single().unwrap();

    commands.spawn(
        Camera2dBundle {
            transform: Transform::from_xyz(window.width() / 2.0, window.height() / 2.0, 1.0),
            ..default()
    });
}

I followed allong a tutorial and according to the tutorial, this should display a blue ball in the middle of the window. Id doesn't though.
What could have been my issue?

lilac yarrow
#

@versed hare I think your image didn't load correctly, and the reason for that is that you don't need to/shouldn't put the "assets" folder in the path: asset_server.load("sprites/ball_blue_large.png")

versed hare
lilac yarrow
#

I don't think it's optional, if you put it it will search in [root of your project]/assets/assets/sprites/ball_blue_large.png
How do you launch your game?

versed hare
#

Shift + F10 in RustRover

#

worked for the command line stuff

lilac yarrow
#

Can you try a cargo run in a terminal at the root of your project just to check?

versed hare
#

2023-09-19T10:34:50.415568Z WARN bevy_asset::asset_server: encountered an error while reading an asset: path not found: /home/over/Documents/Coding/Rust/bevy-ball-game/assets/sprites/ball_blue_large.png

hmmm

lilac yarrow
#

Is your file really there, does it have another name?

versed hare
#

it is there

lilac yarrow
#

What you can try to do also to check if it's really an image loading problem and not a camera setting problem, is to put a color to your sprite:

SpriteBundle {
            transform: Transform::from_xyz(window.width() / 2.0, window.height() / 2.0, 0.0),
            //texture: asset_server.load("assets/sprites/ball_blue_large.png"),
            sprite: Sprite {
              color: Color::BLUE,
              custom_size: Some(Vec2::new(50.,50.)),
              ..default()
            }
            ..default()
        },
#

But it does look like an image loading problem judging by the error above yeah

versed hare
#

its a path issue

#

It works using the absolute path

lilac yarrow
#

Normally if you run cargo run from bevy-ball-game and the file is in ./assets/sprites/ball_blue_large.png it should work
Do you have an environment variable like CARGO_MANIFEST set?

#

The error message tells you where it's looking so if your image is really there it's weird

#

You're on version 0.11.2 right?

versed hare
#

aaah

#

/home/over/Documents/Coding/Rust/bevy-ball-game/src/assets/sprites
thats the path
I put assets in src

lilac yarrow
#

You should put it in the same directory as src not under src

versed hare
#

now it works, thank you for helping me debug this