-
Notifications
You must be signed in to change notification settings - Fork 0
Handling resources
Resources such as image files and sound files must be downloaded when the game starts, so the game won't have to wait for them to download when they are actually needed. To handle this, the images and sound files to use are stated in configuration files, as described here. Strings can also be places in a configuration file, to make it easier to handle different languages of the game.
Image files must be located in the /res/img/ folder. To be able to access the images from the game, edit the file /src/res/Images.js, and add the image to the MW.ImageSources object.
To add the image myimage.png, it is placed in /res/img/myimage.png. In src/res/Images.js, the following is added in the MW.ImagesSources object:
MW.ImageSources = {
// ...
/** @const */ MY_IMAGE: "myimage.png"
};
This image will then be loaded when the game starts, and can then be used in for example a Kinetic.Image object like this:
var myImage = new Kinetic.Image({
image: MW.Images.MY_IMAGE
});
layer.add(myImage);
Sound files must be placed in the /res/sound folder. The must be in both mp3 and ogg format to be able to be played in all major web browsers. Both versions of the sound file must have the same name, only the extension must differ. (More information about this.) Then, edit the file src/res/Sound.js, and add a member to the MW.Sounds object, like this:
MW.Sounds = {
// ...
MY_SOUND: new MW.SoundEntry("my_sound", null)
};
Note that no file extension is used in the configuration file.
To play the sound, use:
MW.Sound.play(MW.Sounds.MY_SOUND);
In /res/Strings.js, you can place strings in different languages:
MW.Strings = (function() {
// ...
"MY_STRING": {
"sv": "Min sträng!",
"en": "My string!"
}
// ...
})();
It is then used like this:
MW.Strings.get("MY_STRING");
The current language can be changed in the MW.GlobalSettings object in MW.js.