How to wait for image to load?

HugoShelter

Warper
Member
Joined
Nov 8, 2016
Messages
2
Reaction score
0
First Language
French
Primarily Uses
Hello fellow developers,


I'm completely new to Javascript, though I have work experience in C languages.


As a beginner exercise, I want to do a simple thing: resize an image that I load to fit with the current resolution.


So I have a high resolution image (1920*1080) that I put as a battleback1 for testing (it is from Blazblue btw). When I simply load it with MV, the image is cropped, as it's too big to fit the default resolution.


Now, I'm not trying to have a bigger resolution at this point, but only to adapt my image to the screen. So I did this (sorry for the C syntax ^^'):


var alias_Spriteset_Battle_battleback1Bitmap = Spriteset_Battle.prototype.battleback1Bitmap;
Spriteset_Battle.prototype.battleback1Bitmap = function()
{
    var battleback1 = alias_Spriteset_Battle_battleback1Bitmap.call(this);
    battleback1.resize(Graphics.width,Graphics.height);
    console.log(battleback1.width);
    return battleback1;
}


As you can imagine, this does not work, and I came to understand it was because Javascript does not wait for the image to be loaded before doing stuff with it. When testing I realized something interesting (if you want to look at the attached images, please ignore the position of the other sprites, that's my next assignment!). On the first fight, the BG image is cropped, as if there was no resize. But starting the second battle (without restarting the game), the image has been resized. I assume that's because the image was already loaded somewhere, so it could resize it.


Apparently, I'm doing it wrong. I apologize if this seems really silly...


By the way, I've also tried this:


var alias_Spriteset_Battle_battleback1Bitmap = Spriteset_Battle.prototype.battleback1Bitmap;
Spriteset_Battle.prototype.battleback1Bitmap = function()
{
    var battleback1 = alias_Spriteset_Battle_battleback1Bitmap.call(this);
    battleback1._onLoad()
    {
        battleback1.resize(Graphics.width,Graphics.height);
    }
    return battleback1;
}


I was delighted to see that it worked for the first battle... but after that, the BG is just black for the second and on... ;_;


1stBattle.PNG


2ndBattleAndOn.PNG
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Are we allowed to post about non-RPG Maker games?
I should realize that error was produced by a outdated version of MZ so that's why it pop up like that
Ami
i can't wait to drink some ice after struggling with my illness in 9 days. 9 days is really bad for me,i can't focus with my shop and even can't do something with my project
How many hours have you got in mz so far?

A bit of a "sparkle" update to the lower portion of the world map. :LZSexcite:

Forum statistics

Threads
105,883
Messages
1,017,236
Members
137,608
Latest member
Arm9
Top