


HTML5 game framework cnGameJS development record - code example of core function module
Return to directory
##1.cnGameJs framework code organization
CoreFunction The main function of the module is to facilitate subsequent framework development and user game development. The entire framework is in a closure to avoid contamination of the global scope. After that, each different module is in its own closure, making the separation of different modules clearer. Therefore, the module division of our framework will be like this:
(function(win,undefined){//最大的闭包 var fun1=function(){//各模块公用的方法 } //这里放各个小模块,它们有各自的闭包 }(window,undefined)
namespace and in its own closure, we have added a register method, which can expand its own module under different namespaces , the first thing we need to pass in is the name of the namespace, this method generates the namespace object for us, and then we execute our own method to perform the corresponding expansion operation for the namespace object:
/** *生成命名空间,并执行相应操作 **/ register:function(nameSpace,func){ var nsArr=nameSpace.split("."); var parent=win; for(var i=0,len=nsArr.length;i<len;i++){ (typeof parent[nsArr[i]]=='undefined')&&(parent[nsArr[i]]={}); parent=parent[nsArr[i]]; } if(func){ func.call(parent,this); } return parent; }
cnGame.register("cnGame.core",function(){this.func=function(){}});
(function(win,undefined){ var cnGame={ register:function(nameSpace,handler){ } } /*core模块*/ cnGame.register("core",function(){ //添加该模块内容 }) /*input模块*/ cnGame.register("input",function(){ //添加该模块内容 }) win["cnGame"]=cnGame; })(window,undefined);
2. Initialization of the framework
/** *初始化 **/ init:function(id,options){ options=options||{}; this.canvas = this.core.$(id||"canvas"); this.context = this.canvas.getContext('2d'); this.width = options.width||800; this.height = options.height||600; this.title = this.core.$$('title')[0]; canvasPos=getCanvasPos(this.canvas); this.x=canvasPos[0]||0; this.y=canvasPos[1]||0; this.canvas.width=this.width; this.canvas.height=this.height; this.canvas.style.left=this.x +"px"; this.canvas.style.top=this.y +"px"; },
/** *获取canvas在页面的位置 **/ var getCanvasPos=function(canvas){ var left = 0; var top = 0; while (canvas.offsetParent) { left += canvas.offsetLeft; top += canvas.offsetTop; canvas = canvas.offsetParent; } return [left, top]; }
3. Tool function module
After that, we can use the register method above to add the first module: core module. This module is also very simple. Its main function is to add tool functions to facilitate subsequent framework development and user game development. This contains some commonly used tool functions, such as getting elements by id, prototypeinheritance, object copy, eventbinding, etc.. Note that if there are compatibility issues with different browsers, we can set the function according to the browser from the beginning instead of judging the browser type every time and then perform corresponding operations, which will be more efficient. Take event binding as an example:
/** 事件绑定 **/ this.bindHandler=(function(){ if(window.addEventListener){ return function(elem,type,handler){ elem.addEventListener(type,handler,false); } } else if(window.attachEvent){ return function(elem,type,handler){ elem.attachEvent("on"+type,handler); } } })();
/** * *基本工具函数模块 * **/ cnGame.register("cnGame.core",function(cg){ /** 按id获取元素 **/ this.$=function(id){ return document.getElementById(id); }; /** 按标签名获取元素 **/ this.$$=function(tagName,parent){ parent=parent||document; return parent.getElementsByTagName(tagName); }; /** 按类名获取元素 **/ this.$Class=function(className,parent){ var arr=[],result=[]; parent=parent||document; arr=this.$$("*"); for(var i=0,len=arr.length;i0){ result.push(arr[i]); } } return result; }; /** 事件绑定 **/ this.bindHandler=(function(){ if(window.addEventListener){ return function(elem,type,handler){ elem.addEventListener(type,handler,false); } } else if(window.attachEvent){ return function(elem,type,handler){ elem.attachEvent("on"+type,handler); } } })(); /** 事件解除 **/ this.removeHandler=(function(){ if(window.removeEventListerner){ return function(elem,type,handler){ elem.removeEventListerner(type,handler,false); } } else if(window.detachEvent){ return function(elem,type,handler){ elem.detachEvent("on"+type,handler); } } })(); /** 获取事件对象 **/ this.getEventObj=function(eve){ return eve||win.event; }; /** 获取事件目标对象 **/ this.getEventTarget=function(eve){ var eve=this.getEventObj(eve); return eve.target||eve.srcElement; }; /** 禁止默认行为 **/ this.preventDefault=function(eve){ if(eve.preventDefault){ eve.preventDefault(); } else{ eve.returnValue=false; } }; /** 获取对象计算的样式 **/ this.getComputerStyle=(function(){ var body=document.body; if(body.currentStyle){ return function(elem){ return elem.currentStyle; } } else if(document.defaultView.getComputedStyle){ return function(elem){ return document.defaultView.getComputedStyle(elem, null); } } })(); /** 是否为undefined **/ this.isUndefined=function(elem){ return typeof elem==='undefined'; }, /** 是否为数组 **/ this.isArray=function(elem){ return Object.prototype.toString.call(elem)==="[object Array]"; }; /** 是否为Object类型 **/ this.isObject=function(elem){ return elem===Object(elem); }; /** 是否为字符串类型 **/ this.isString=function(elem){ return Object.prototype.toString.call(elem)==="[object String]"; }; /** 是否为数值类型 **/ this.isNum=function(elem){ return Object.prototype.toString.call(elem)==="[object Number]"; }; /** *复制对象属性 **/ this.extend=function(destination,source,isCover){ var isUndefined=this.isUndefined; (isUndefined(isCover))&&(isCover=true); for(var name in source){ if(isCover||isUndefined(destination[name])){ destination[name]=source[name]; } } return destination; }; /** *原型继承对象 **/ this.inherit=function(child,parent){ var func=function(){}; func.prototype=parent.prototype; child.prototype=new func(); child.prototype.constructor=child; child.prototype.parent=parent; }; });
The above is the detailed content of HTML5 game framework cnGameJS development record - code example of core function module. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.
