Dynamic sprite loading

Get help using Construct 2

Post » Thu Apr 27, 2017 12:47 pm

I have been playing around with the ability to dynamically load sprites from a configuration file onto a layout, the goal being to pre-load a scene before displaying it.

The loading part when the layout is the current one works fine (see attached code):

When you click on the first button, an image is loaded into the first frame of the sprite, then clicking the second button creates another instance
of the sprite, then loads another image to its second frame (and so on for each image is the idea). After the last image is loaded, a flag is set, which
once the On Image loaded triggers, makes all sprites visible.

My problem is trying to do this on a layout which is not currently visible. I tried creating a layout 2, having the sprite on this layout (instead of layout 1) and using the same code I would load images, then move to layout 2 and make all sprites visible. As we need to move to another layout, I made the sprite on layout 2 persistent (otherwise all loaded images would be removed) and have both layouts using the same single event sheet.

For some reason, the On Image loaded event never seems to trigger in this second case (and no errors are shown in the browser's console). Does anyone have an idea why this happens or another idea on how to workaround this issue?

Perhaps @ashley could provide some insights onto how the load from url / on image URL loaded can/can't work between layouts?

https://www.dropbox.com/s/ahgx439h6an2bwn/LoadURLcode.png?dl=0
B
10
S
3
Posts: 87
Reputation: 957

Post » Thu Apr 27, 2017 8:22 pm

You seem to be operating under some mistaken assumptions regarding layouts and persistence.

You cannot affect anything on a different layout than the one you are currently on with events, regardless if they share event sheets.

The persist behavior doesn't have anything to do with memory, it just saves the state of that object for when you return to the layout.

If a sprite is already loaded into memory in the current layout and is also present on the following layout, it will not get unloaded from memory between the layouts.

So to do a proper loader layout, you simply load the sprites that you need in the following layout in the current layout. They can be invisible if you want. When you switch layouts, those objects will already be in memory.
Mistakes were made.
B
49
S
24
G
107
Posts: 1,581
Reputation: 60,303

Post » Mon May 01, 2017 8:56 am

@oosyrag Thanks for the insight regarding sprites remaining in memory between layouts.

My issue is that I need to create a different number of sprites on different layouts (the layout background and sprite images are specified in an external XML file and the number of sprite objects can change from one layout to another). If I use a sprite on the current layout and create new instances of it which I reposition to x,y coordinates and into which I load different images, these new sprites won't be present on the following layout when I move to it (the only way of having this work is to have the same number of sprites already existent in both layouts).

Perhaps the only solution is to remain on the same layout, load a new background into an invisible sprite, load all objects for the "next scene" into multiple invisible sprites created on the fly and to then destroy all currently visible sprites and to make the ones currently invisible visible to the player.

Any thoughts on other ways of doing this?
B
10
S
3
Posts: 87
Reputation: 957

Post » Mon May 01, 2017 6:02 pm

Regardless of if they are positioned or created or not, as long as they are in memory they should be able to be created/positioned immediately. Loading into memory is generally what takes time.

Otherwise, you can just create a "loading layer" that covers your layout while images are being loaded, then make that layer invisible when the scene is ready.
Mistakes were made.
B
49
S
24
G
107
Posts: 1,581
Reputation: 60,303


Return to How do I....?

Who is online

Users browsing this forum: jkmalau, terence951, vegapomme27 and 9 guests