Home  >  Article  >  Web Front-end  >  HTML5 game framework cnGameJS development record-detailed explanation of resource loading module code

HTML5 game framework cnGameJS development record-detailed explanation of resource loading module code

黄舟
黄舟Original
2017-03-24 16:01:511434browse

1. Functions  

This module is the entrance to the game. We load resources through this module and call the entry function of the game object after the resources are loaded. In addition, this module also includes switching between game scenes, as well as the calculation and display of loading percentage.

When starting the game, first pass in the list of resources that need to be loaded, then pass in the game object, and finally pass in the function that is called after each resource is loaded. This function can get the loading percentage. As follows:

cnGame.loader.start(["src1","src2","src3"],gameObj,function(loadedPercent){});

In this case, the three image resources passed in earlier will be loaded first, and each time a picture is loaded, the following callback function will be called, The The function can obtain the loading percentage, implement the loading interface, tell the user the current loading progress and other functions. When loading is complete, call the initialize method of the game object gameObj to start the game.

2. Specific implementation:

First, let’s look at the code of the loader:

/**
     *图像加载器
    **/    
    var loader={
        sum:0,            //图片总数
        loadedCount:0,    //图片已加载数
        loadingImgs:{}, //未加载图片集合
        loadedImgs:{},    //已加载图片集合
        /**
         *图像加载,之后启动游戏
        **/    
        start:function(src,gameObj,onLoad){//可传入src数组或单个src "xxx.jpg" or ["xxx.jpg","ggg,gif","www.png"]
        
            if(cg.core.isArray(src)){ 
                this.sum=src.length;
                for(var i=0,len=src.length;i<len;i++){
                    this.gameObj=gameObj;
                    this.onLoad=onLoad;
                    this.loadingImgs[src[i]]=new Image();
                    this.loadingImgs[src[i]].onload=imgLoad(this);
                    this.loadingImgs[src[i]].src=src[i];
                    this.loadingImgs[src[i]].srcPath=src[i];//没有经过自动变换的src
                }
                    
            }
            
        }
        
    }

First, the resource loader saves the following Fields: list of loaded resources, list of unloaded resources, total number of resources, total number of loaded resources. When the start method is called, the loader starts traversing the image srcarray and generates an image object for loading. In addition, we need to save srcPath for each image object, which is the original src parameter (because by default the browser will convert the src parameter into a complete image path) . The next step is to add an onLoad handler for each image. We need to calculate the loading percentage in this handler and save the loaded image object into the loadedImgs object to facilitate subsequent use by users. The processing procedure for image loading is as follows:

/**
     *图像加载完毕的处理程序
    **/    
    var imgLoad=function(self){
        return function(){
            self.loadedCount+=1;
            self.loadedImgs[this.srcPath]=this;
            this.onLoad=null;                    //保证图片的onLoad执行一次后销毁
            self.loadedPercent=Math.floor(self.loadedCount/self.sum*100);
            self.onLoad&&self.onLoad(self.loadedPercent);
            if(self.loadedPercent===100){
                self.loadedCount=0;
                self.loadedPercent=0;
                loadingImgs={};
                if(self.gameObj&&self.gameObj.initialize){
                    self.gameObj.initialize();
                    if(cg.loop&&!cg.loop.stop){//结束上一个循环
                        cg.loop.end();
                    }
                    cg.loop=new cg.GameLoop(self.gameObj);//开始新游戏循环
                    cg.loop.start();
                }
                
            }
            
        
        }
    }

After each image is loaded, the loading quantity is increased by 1, the image object is saved, and the percentage of loading completion is calculated. Finally, you need to delete the handler of the image (because the image has been loaded, the handler is useless and can be released to save memory resources). When the loading percentage is 100%, all values ​​are reset and loadingImgs is released for the next loading of resources. In addition, the game loop (the game loop is responsible for all game objects in each frame) The update and draw)

/**
 *
 *资源加载器
 *
**/
cnGame.register("cnGame",function(cg){

    /**
     *图像加载完毕的处理程序
    **/    
    var imgLoad=function(self){
        return function(){
            self.loadedCount+=1;
            self.loadedImgs[this.srcPath]=this;
            this.onLoad=null;                    //保证图片的onLoad执行一次后销毁
            self.loadedPercent=Math.floor(self.loadedCount/self.sum*100);
            self.onLoad&&self.onLoad(self.loadedPercent);
            if(self.loadedPercent===100){
                self.loadedCount=0;
                self.loadedPercent=0;
                loadingImgs={};
                if(self.gameObj&&self.gameObj.initialize){
                    self.gameObj.initialize();
                    if(cg.loop&&!cg.loop.stop){//结束上一个循环
                        cg.loop.end();
                    }
                    cg.loop=new cg.GameLoop(self.gameObj);//开始新游戏循环
                    cg.loop.start();
                }
                
            }
            
        
        }
    }
    /**
     *图像加载器
    **/    
    var loader={
        sum:0,            //图片总数
        loadedCount:0,    //图片已加载数
        loadingImgs:{}, //未加载图片集合
        loadedImgs:{},    //已加载图片集合
        /**
         *图像加载,之后启动游戏
        **/    
        start:function(src,gameObj,onLoad){//可传入src数组或单个src "xxx.jpg" or ["xxx.jpg","ggg,gif","www.png"]
        
            if(cg.core.isArray(src)){ 
                this.sum=src.length;
                for(var i=0,len=src.length;i<len;i++){
                    this.gameObj=gameObj;
                    this.onLoad=onLoad;
                    this.loadingImgs[src[i]]=new Image();
                    this.loadingImgs[src[i]].onload=imgLoad(this);
                    this.loadingImgs[src[i]].src=src[i];
                    this.loadingImgs[src[i]].srcPath=src[i];//没有经过自动变换的src
                }
                    
            }
            
        }
        
    }
    
    
    this.loader=loader;
});

The above is the detailed content of HTML5 game framework cnGameJS development record-detailed explanation of resource loading module code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn