73 preload - JcerelusDev/CanvasGameJs GitHub Wiki
assets.preload
Preloading assets is used to prevent failure, when animating sprite, you don't need to continuously requesting new images ir sounds over and over. They are preloading when page load.
How to use it ?
There is an instance of a class named (assets) it is a global variable.
You have to the method preload, which has two arguments :
1 .- the assetId
2 .- the assetPath
Important
The assetId is useful for later usage.
assets.preload("bgMusic", "audio/landof8bit.mp3")
assets.preload("jumpEffect", "audio/retro-jump.mp3")
assets.preload("key", "images/key1.png")
assets.preload("burger", "images/burger.png")
assets.preload("croissant", "images/croissant.png")
assets.preload("playerIdle", "images/idle.png")
assets.preload("playerRun", "images/run.png")
assets.preload("playerJump", "images/jump.png")
How to use the assetId
For sprite :
// This is an example
let player = new SpriteSheet(0,0,32,32,150,50,32,32,0,0)
player.useImage("playerIdle") // to use the idle image sprite
You can do the above anytime you need to use another type of animation
How to use the assetId
For music :
// example for background music
let bgSound = new PlaySound(assets.getAudio("bgMusic"), 0.1, "loop")
learn about PlaySound