Last active
October 14, 2016 09:21
-
-
Save mentorkadriu/cc6df16552e6ed5f68955fc2e5e320ca to your computer and use it in GitHub Desktop.
Prelaod in Phaser with Loading Events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SimpleText from 'objects/SimpleText'; | |
class PreloadState extends Phaser.State { | |
init(){ | |
console.log('Init'); | |
this.x = null; | |
this.y = null; | |
this.loadingText = new SimpleText(this.game, this.game.world.centerX, this.game.world.centerY, 'Loading'); | |
this.loadingText.anchor.set(0.5); | |
this.game.load.onLoadStart.add(this.loadStart, this); | |
this.game.load.onFileComplete.add(this.fileComplete, this); | |
this.game.load.onLoadComplete.add(this.loadComplete, this); | |
} | |
preload() { | |
this.game.load.spritesheet('button', 'assets/buttons/button_sprite_sheet.png', 193, 71); | |
this.game.load.image('picture1', 'assets/pics/mighty_no_09_cover_art_by_robduenas.jpg'); | |
this.game.load.image('picture2', 'assets/pics/cougar_dragonsun.png'); | |
this.game.load.image('picture3', 'assets/pics/trsipic1_lazur.jpg'); | |
this.game.load.image('picture4', 'assets/pics/archmage_in_your_face.png'); | |
this.game.load.image('picture5', 'assets/pics/acryl_bladerunner.png'); | |
this.game.load.image('picture6', 'assets/pics/acryl_bobablast.png'); | |
this.game.load.image('picture7', 'assets/pics/alex-bisleys_horsy_5.png'); | |
} | |
create(){ | |
this.game.stage.backgroundColor = '#182d3b'; | |
} | |
update() { | |
//show loading in percentage, magic | |
if(this.loadingProcess != null && this.loadingProcess.loadingProcessInPercentage != null) { | |
this.loadingProcess.loadingProcessInPercentage.text = this.game.load.progress + ' %'; | |
} | |
} | |
loadStart(){ | |
console.log('Loading...'); | |
this.loadingText.setText('Loading...'); | |
} | |
fileComplete(progress, cacheKey, success, totalLoaded, totalFiles) { | |
this.loadingText.setText("File Complete: " + progress + "% - " + totalLoaded + " out of " + totalFiles); | |
var newImage = this.game.add.image(this.x, this.y, cacheKey); | |
newImage.scale.set(0.3); | |
this.x += newImage.width + 20; | |
if (this.x > 700) | |
{ | |
this.x = 32; | |
this.y += 332; | |
} | |
} | |
loadComplete(){ | |
console.log('Loading Complete!') | |
//this.game.state.start("GameTitleState"); | |
} | |
} | |
export default PreloadState; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment