Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何提高Javascript游戏引擎的速度_Javascript_Html - Fatal编程技术网

如何提高Javascript游戏引擎的速度

如何提高Javascript游戏引擎的速度,javascript,html,Javascript,Html,所以,最近,我一直在开发一个JS/HTML5游戏引擎。现在我称之为DimSumJs,因为like并不是一顿丰盛的晚餐,我的框架仍然运行得太慢,无法制作一个完整的游戏(它只能运行大约250个“对象”,尽管在减速之前,它在300个“对象”左右变得非常明显)。它在iframe中使用div 游戏样本可在 只需使用google chrome查看资源,就可以找到dimsum.js文件 //DimSumJS - Open Source Game Engine //DimSumJS (C) Ruochen Ta

所以,最近,我一直在开发一个JS/HTML5游戏引擎。现在我称之为DimSumJs,因为like并不是一顿丰盛的晚餐,我的框架仍然运行得太慢,无法制作一个完整的游戏(它只能运行大约250个“对象”,尽管在减速之前,它在300个“对象”左右变得非常明显)。它在iframe中使用div

游戏样本可在

只需使用google chrome查看资源,就可以找到dimsum.js文件

//DimSumJS - Open Source Game Engine
//DimSumJS (C) Ruochen Tang
//Can be used commerically, but please give credit
//Constants
var RIGHTKEY = 37;
var UPKEY = 38;
var LEFTKEY = 39;
var DOWNKEY = 40;
var SPACEKEY = 32;
var MASTER_WIDTH = 480;
var MASTER_HEIGHT = 600;

var Game = window.frames[0].document.body;
Game.setAttribute("width",MASTER_WIDTH + "px");
Game.setAttribute("height",MASTER_HEIGHT + "px");
var gl = setInterval("gameLoop();",15);

//Global Vars
var keyDown = new Array();
for (var i = 0; i < 256; i++){
    keyDown[i] = false;
}
var gameState = 1;

//Settings
Game.style.backgroundColor = "#000";

//Key
processKeyEvent = function(event){
        // MSIE hack
        if (window.event)
        {
            event = window.event;
        }

        keyDown[event.keyCode] = true;      
};

releaseKey = function(event){
    // MSIE hack
        if (window.event)
        {
            event = window.event;
        }

    keyDown[event.keyCode] = false;
}
Game.onkeydown = processKeyEvent;
Game.onkeyup = releaseKey;


var GameObjects = new Array();

function GameObject(xx, yy, w, h, i, inc, gs, name, img){

    GameObjects.push(this);

    this.width = w;
    this.height = h;
    this.index = i;
    this.currIndex = 0;
    this.increment = inc;
    this.currInc = 0;
    this.x = xx;
    this.y = yy;
    this.depth = 0;
    this.objType = name;
    this.image = img;
    this.xScale = 1;
    this.yScale = 1;
    this.scaleString = "scale(" + this.xScale + "," + this.yScale + ")";
    this.speed = 0;
    this.direction = 0;
    this.gravity = 0;
    this.gravityDirection = 0;
    this.active = true;
    this.visible = true;
    this.bindToRoom = false;
    this.text = "";
    this.color = "#FFF";
    this.gameState = gs;

    this.div = document.createElement("div");
    this.div.className=this.objType;
    this.div.style.position="absolute";
    this.div.style.left= this.x + "px";
    this.div.style.top= this.y + "px";
    this.div.style.width= this.width + "px";    
    this.div.style.height= this.height + "px";  
    this.div.style.backgroundImage = "url(images/" + this.image + ")";

    this.div.style[getTransformProperty(this.div)] = this.scaleString;

    Game.appendChild(this.div);
    this.isDiv = true;
    this.classChanged = false;

    this.move = move;
    this.anim = anim;
    this.setScale = setScale;
    this.checkCollisionAt = checkCollisionAt;
    this.objectAt = objectAt;
    this.objectTypeAt = objectTypeAt;
    this.toggleActive = toggleActive;
    this.extend = extend;
    this.unextend = unextend;
    this.isType = isType;
    this.update = update;


    function move(xx,yy){
        this.x += xx;
        this.y += yy;
    }

    function anim(){
        this.currInc += 1;
        if (this.currInc >= this.increment){
            this.currInc -= this.increment;
            this.currIndex += 1;
            if (this.currIndex >= this.index){
                this.currIndex -= this.index;
            }

        }

    }
    function extend(type) {
        this.objType += " " + type;
        this.classChanged = true;
    }

    function unextend(type) {
        this.objType = this.objType.replace( /(?:^|\s)type(?!\S)/ , '' );
        this.classChanged = true;
    }

    function isType(type) {
            return ((' ' + this.objType + ' ').indexOf(' ' + type + ' ') > -1);
    }

    function setScale(xx,yy){
        this.xScale = xx;
        this.yScale = yy;
        this.scaleString = "scale(" + this.xScale + "," + this.yScale + ")";    
    }


    function checkCollisionAt(xx,yy,other){
        //Check For Collision
        xx += this.x;
        yy += this.y;

        if ((xx + this.width > other.x) && (xx < other.x + other.width) && (yy + this.height > other.y) && (yy < other.y + other.height)){
            return true;
        }
        else{
            return false;
        }
    }

    function objectAt(xx,yy,solid){
        //Loop All Objects
        for (var i = 0; i < GameObjects.length; i++){
            if (GameObjects[i] != this && this.isDiv){
                if (this.checkCollisionAt(xx,yy,GameObjects[i])){
                    console.log(i);
                    return true;
                }
            }           
        }
        return false;
    }

    function objectTypeAt(xx,yy,type){
        //Loop All Objects
        for (var i = 0; i < GameObjects.length; i++){
            if (GameObjects[i] != this && GameObjects[i].isType(type) && this.isDiv){
                if (this.checkCollisionAt(xx,yy,GameObjects[i])){
                    return true;
                }
            }
        }
        return false;
    }

    function toggleActive(a){
        this.visible = a;
        this.update();
        this.active = a;
    }

    function update(){      
        if ((this.active == false || this.gameState != gameState) && this.isDiv){
            this.isDiv = false;
            Game.removeChild(this.div);
            return;
        }
        else if(!this.isDiv){
            this.isDiv = true;
            Game.appendChild(this.div);
        }

        this.div.style.display = "inline";

        if (this.speed != 0){
            this.x += this.speed*Math.cos(this.direction*Math.PI/180);
            this.y += this.speed*Math.sin(this.direction*Math.PI/180);
        }

        if (this.bindToRoom == true){
            if (this.x < 0){
                this.x = 0;
            }

            if (this.y < 0){
                this.y = 0;
            }

            if (this.x > MASTER_WIDTH-this.width){
                this.x = MASTER_WIDTH-this.width;
            }

            if (this.y > MASTER_HEIGHT-this.height){
                this.y = MASTER_HEIGHT-this.height;
            }
        }

        if (!this.visible && this.isDiv){
            this.isDiv = false;
            Game.removeChild(this.div);
            return;
        }
        if (this.classChanged){
            this.div.className = this.objType;
        }

        this.div.style.zIndex = this.depth;

        this.div.style.color = this.color;
        this.div.innerHTML = this.text;

        this.div.style.left= this.x + "px";
        this.div.style.top= this.y + "px";

        this.div.style[getTransformProperty(this.div)] = this.scaleString;
        this.div.style.backgroundPosition = this.currIndex * this.width +"px 0";


    }
}
function getTransformProperty(element) {

    // Note that in some versions of IE9 it is critical that
    // msTransform appear in this list before MozTransform
    // By ZachAstronaut

    var properties = [
        'transform',
        'WebkitTransform',
        'msTransform',
        'MozTransform',
        'OTransform'
    ];
    var p;
    while (p = properties.shift()) {
        if (typeof element.style[p] != 'undefined') {
            return p;
        }
    }
    return false;
}
//DimSumJS-开源游戏引擎
//迪姆苏姆杰(C)若晨堂
//可用于商业用途,但请注明信用
//常数
var-RIGHTKEY=37;
var-UPKEY=38;
var-LEFTKEY=39;
var-DOWNKEY=40;
var-SPACEKEY=32;
var MASTER_WIDTH=480;
var MASTER_高度=600;
var Game=window.frames[0].document.body;
Game.setAttribute(“宽度”,MASTER_宽度+“px”);
Game.setAttribute(“高度”,MASTER_高度+“px”);
var gl=setInterval(“gameLoop();”,15);
//全局变量
var keyDown=新数组();
对于(变量i=0;i<256;i++){
keyDown[i]=假;
}
var配子状态=1;
//背景
Game.style.backgroundColor=“#000”;
//钥匙
processKeyEvent=函数(事件){
//MSIE黑客
if(window.event)
{
event=window.event;
}
keyDown[event.keyCode]=true;
};
releaseKey=功能(事件){
//MSIE黑客
if(window.event)
{
event=window.event;
}
keyDown[event.keyCode]=false;
}
Game.onkeydown=processKeyEvent;
Game.onkeyup=releaseKey;
var GameObjects=新数组();
功能游戏对象(xx,yy,w,h,i,inc,gs,name,img){
游戏对象。推(这个);
这个宽度=w;
这个高度=h;
该指数=i;
该指数=0;
this.increment=inc;
该值为0;
这是x=xx;
这个。y=yy;
这个深度=0;
this.objType=名称;
this.image=img;
这个.xScale=1;
这个.yScale=1;
this.scaleString=“scale(“+this.xScale+”,“+this.yScale+”);
这个速度=0;
这个方向=0;
这个重力=0;
这个重力方向=0;
this.active=true;
可见=真实;
this.bindToRoom=false;
this.text=“”;
this.color=“#FFF”;
this.gameState=gs;
this.div=document.createElement(“div”);
this.div.className=this.objType;
this.div.style.position=“绝对”;
this.div.style.left=this.x+“px”;
this.div.style.top=this.y+“px”;
this.div.style.width=this.width+“px”;
this.div.style.height=this.height+“px”;
this.div.style.backgroundImage=“url(images/“+this.image+”);
this.div.style[getTransformProperty(this.div)]=this.scaleString;
Game.appendChild(this.div);
this.isDiv=真;
this.classChanged=false;
this.move=移动;
this.anim=anim;
this.setScale=setScale;
this.checkCollisionAt=checkCollisionAt;
this.objectAt=objectAt;
this.objectTypeAt=objectTypeAt;
this.toggleActive=toggleActive;
this.extend=extend;
这个。未趋势=未趋势;
this.isType=isType;
this.update=更新;
功能移动(xx,yy){
这个.x+=xx;
这个.y+=yy;
}
函数anim(){
该值为0.inc+=1;
如果(this.currenc>=this.increment){
this.currenc-=this.increment;
该指数+=1;
如果(this.currendex>=this.index){
this.currendex-=this.index;
}
}
}
功能扩展(类型){
this.objType+=“”+类型;
this.classChanged=true;
}
功能未扩展(类型){
this.objType=this.objType.replace(/(?:^|\s)类型(?!\s)/,“”);
this.classChanged=true;
}
函数isType(类型){
返回(“”+this.objType+“”).indexOf(“”+type+“”)>-1);
}
功能设置刻度(xx,yy){
这个.xScale=xx;
这个.yScale=yy;
this.scaleString=“scale(“+this.xScale+”,“+this.yScale+”);
}
功能检查冲突(xx、yy、其他){
//检查有无碰撞
xx+=这个.x;
yy+=这个.y;
如果((xx+this.width>other.x)和&(xxother.y)和&(yyMA
function isType(type) {
    return this.objType && new RegExp("(^|\\s)" + type + "(\\s|$)").test(this.objType);
} 
var priorTypeRegexps = {};
function isType(type) {
    var aRegexp;
    if (! priorTypeRegexps[type]) {
        priorTypeRegexps[type] = new RegExp("(^|\\s)" + type + "(\\s|$)");
    }
    aRegexp = priorTypeRegexps[type];        
    return this.objType && aRegexp.test(this.objType);
}