• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Android Load image from JPG (solved)

Mataca

Member
Hi. I am currently making an android game in which I need to load 1 different image (480x600px) in each level. To ease the weight of the game, I have them in JPG. What I currently do is first delete the previous sprite in each level and then add a sprite from its respective JPG image, but this eventually causes me texture errores and the game crashes. Does anyone know how I can make this method more effective or change it to a better one? Putting them in PNG is not a solution, since they are 630 levels and the game would weigh a lot. I show you part of the code so that you understand it better.


sprite_delete(sprite_index)
switch(global.level){
case 001:
sprite_index=sprite_add("img505/a001.jpg", 1, false, true, 0, 0);
break;
case 002:
sprite_index=sprite_add("img505/a002.jpg", 1, false, true, 0, 0);
break;
case 003:
sprite_index=sprite_add("img505/a003.jpg", 1, false, true, 0, 0);
break;
case 004:
sprite_index=sprite_add("img505/a004.jpg", 1, false, true, 0, 0);
break;
case 005:
sprite_index=sprite_add("img505/a005.jpg", 1, false, true, 0, 0);
break;
case 006:
sprite_index=sprite_add("img505/a006.jpg", 1, false, true, 0, 0);
break;
case 007:
sprite_index=sprite_add("img505/a007.jpg", 1, false, true, 0, 0);
break;
case 008:
sprite_index=sprite_add("img505/a008.jpg", 1, false, true, 0, 0);
break;
case 009:
sprite_index=sprite_add("img505/a009.jpg", 1, false, true, 0, 0);
break;
case 010:
sprite_index=sprite_add("img505/a010.jpg", 1, false, true, 0, 0);
break;
case 011:
sprite_index=sprite_add("img505/a011.jpg", 1, false, true, 0, 0);
break;
case 012:
sprite_index=sprite_add("img505/a012.jpg", 1, false, true, 0, 0);
break;
}​



Mataca
 

FrostyCat

Redemption Seeker
You are asking a crash because you are freeing a resource that is still in use. Set sprite_index away to something else first (e.g. -1), then delete+reload the sprite or replace it.
 

gnysek

Member
When switching rooms, if that object isn't marked as persistent, it's deleted. As such, the new one have sprite_index == -1 at start, so none of the sprites is deleted, and the old one is kept in memory, without any reference to it anymore. That's a memory leak, and after some time RAM might use gigabytes because of it.

I would highly suggest using global. variable for it, so you can be sure that it's value is kept between rooms. Also don't forget to check if sprite_exists(global.myvariable), before deleting it.
 

Mataca

Member
When switching rooms, if that object isn't marked as persistent, it's deleted. As such, the new one have sprite_index == -1 at start, so none of the sprites is deleted, and the old one is kept in memory, without any reference to it anymore. That's a memory leak, and after some time RAM might use gigabytes because of it.

I would highly suggest using global. variable for it, so you can be sure that it's value is kept between rooms. Also don't forget to check if sprite_exists(global.myvariable), before deleting it.

I get it. So if I delete the sprite at the end of the level with the object still existing it would work the same, right?
 

gnysek

Member
There's a special event "Clean Up" in objects, which is performed either on delete or on room end (if they not exists). Yep, that's the one you should use (as Destroy is called only when instance_destroy() is called by code).
 

Mataca

Member
There's a special event "Clean Up" in objects, which is performed either on delete or on room end (if they not exists). Yep, that's the one you should use (as Destroy is called only when instance_destroy() is called by code).
I added a "ROOM END" event in the object in which I put "sprite_delete (sprite_index)". With this in theory it would be solved. Thank you very much for the help.

Mataca
 

gnysek

Member
I added a "ROOM END" event in the object in which I put "sprite_delete (sprite_index)". With this in theory it would be solved. Thank you very much for the help.

Mataca
If you destroy/deactivate instance before room end, then it will be not cleared, so using "Clean up" event is more safe than "room end" or "instance destroy".
 
Top