Jump to content

preload and create functions


valentinaCom
 Share

Recommended Posts

hi :)

 

I need some help with my game. 

I made a state called "Preload_Game" which load all the files from a json file:

 

preload: function () {
        game.gameData = JSON.parse(game.cache.getText('GameData'));
        game.GameSpritesData = JSON.parse(game.cache.getText('GameSprites'));
        var jsonHandler=new JsonHandler();
        jsonHandler.init(this,game.gameData,game.GameSpritesData);
}
 
and then in the create function, the game start. I notice that the game start before loading all files.
 
here's the code in JsonHander:
 
sonHandler.prototype={
    constructor:JsonHandler,
 
    /** -------- init ---------
     *  Function: Init class variables and call 'loadAll' function.
     *  Parameters: parentClass - The class that json was loaded from
     *              jsonData - All json's data
     *              jsonObj - Json of sprites
     -------------------------*/
    init:function(parentClass,jsonData,jsonObj){
        this._parentClass=parentClass;
        this._jsonData=jsonData;
        console.log("jsonObj "+jsonObj);
        this._jsonObj=jsonObj;
        this.loadAll();
    },
 
    /** ------- loadAll -------
     *  Function: Get graphic array from json and send each file to 'loadFileByType' function
     *  Parameters: -
     -------------------------*/
    loadAll:function(){
        this._graphicsArray = this._jsonData.Graphics;
        for(var i=0; i<this._graphicsArray.length; i++){
            var type = this._graphicsArray.type;
            this.loadFileByType(type,this._graphicsArray);
        }
    },
 
    /** --- loadFileByType ---
     *  Function: Get info of file and load it depending on type.
     *  Parameters: fileType - The file's type (image, sprite, etc)
     *              GraphicData - All file info
     -------------------------*/
    loadFileByType:function(fileType,GraphicData){
        var id = GraphicData.id;
        var src = GraphicData.src;
        switch(fileType) {
            case "image":{
                this._parentClass.load.image(id, src).onLoadComplete.add(function(){console.log("load - "+id);},this);
                break;
            }
            case "sprite":{
                var specificObj = this._jsonObj[id];
                this._parentClass.load.atlas(id, src, null,specificObj).onLoadComplete.add(function(){console.log("load - "+id);},this);
                break;
            }
            case "audio":{
                this._parentClass.load.audio(id,src).onLoadComplete.add(function(){console.log("load - "+id);},this);
                break;
            }
        }
    },

 

I think maybe there is a problem with calling a function placed in another js file in the preload function.

 

someone have a solution for me?

 

Link to comment
Share on other sites

Oh I think i have the same problem, your audio comes late right? I have that problem on android. It's because jsonHandler class loads the files asynchronously, I am not sure though. anyway you have a weird sprite json file, do you define audio, image and sprites in a single json file and do you manage that by hand? 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...