Sprite won't be visible until the next sprite spawns #8288
-
Hi! I'm running into a strange issue where the sprite will spawn "invisibly" and then only become visible once the next sprite spawns. I have code to reproduce this and have included a video. I am very much a beginner with Bevy, so I wouldn't know if this is a bug with my code or an issue with the engine. In the video, watch when the score goes from 160 -> 165. You'll see that a banana appears to materialize in the middle of the screen. But it had spawned at the top of the screen like the rest of the bananas. I would very much appreciate to hear any thoughts about this issue. Please let me know if I can be of any further assistance!
Screencast.from.03-28-2023.08.53.47.PM.webm |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This looks like an asset unloading bug:
What's interesting is that, if the new sprite is spawned in the same frame or 3 or more frames after the last banana sprite is despawned (step 1), then everything works fine. Anyway, you should cache your images/assets to stop them from unloading: fn main() {
App::new()
// ...
.add_startup_system(load_and_cache_images)
// ...
}
#[derive(Resource)]
struct ImageCache {
banana: Handle<Image>,
bunch_of_bananas: Handle<Image>
}
fn load_and_cache_images(
mut commands: Commands,
asset_server: Res<AssetServer>,
) {
commands.insert_resource(ImageCache {
banana: asset_server.load("sprites/banana.png"),
bunch_of_bananas: asset_server.load("sprites/bananabunch.png")
});
} then use the cached images when spawning sprites: fn spawn_falling_objects_over_time(
mut commands: Commands,
window_query: Query<&Window, With<PrimaryWindow>>,
falling_object_spawn_timer: Res<FallingObjectSpawnTimer>,
image_cache: Res<ImageCache>,
) {
if falling_object_spawn_timer.timer.finished() {
let window = window_query.single();
let bounds_width = window.width() - (2.0 * BOUND_SIZE);
let random_x = (random::<f32>() * bounds_width) + BOUND_SIZE;
let (kind, points, texture) = if random::<f32>() < 0.1 {
(FallingObjectKind::BananaBunch, 5, image_cache.bunch_of_bananas.clone_weak())
} else {
(FallingObjectKind::Banana, 1, image_cache.banana.clone_weak())
};
commands.spawn((
SpriteBundle {
transform: Transform::from_xyz(random_x, window.height(), 0.0),
texture,
..default()
},
FallingObject {
kind,
points,
},
));
}
} edit: This seems to be the same bug as: #6626 |
Beta Was this translation helpful? Give feedback.
This looks like an asset unloading bug:
0:14
the last single banana is caught and its entity is despawnedbanana.png
image for removal as there's no sprite using itbanana.png
imagebanana.png
image (even though it shouldn't), and your banana sprite disappears from the screen (the entity is still there and moving down - you just don't see it)banana.png
image againWhat's interesting is that, if the new sprite is spawned in the same frame or 3 or more frames after the last banana sprite is despa…