gpt4 book ai didi

javascript - html5/javascript 游戏在 ie8 中运行缓慢

转载 作者:太空宇宙 更新时间:2023-11-04 15:27:26 25 4
gpt4 key购买 nike

我们用 html/javascript 制作了一个游戏。不幸的是,这个项目有很多 secret ,所以我不能透露太多代码。

然而本质是它在所有浏览器上都运行得非常好,除了ie。通过使用 <meta http-equiv="X-UA-Compatible" content="IE=9">标签;现在唯一的问题是 ie8,游戏需要与之兼容。我知道它几乎没有集成 html5,但游戏确实可以运行,只是速度非常慢。

请记住,ie9 和 ie10 在使用该标记之前也非常慢。该游戏在某种意义上是先进的或任何东西。所以我想知道是否有人知道什么会导致此类问题?

我将尝试在此处放置一些经过混淆的代码,请记住这不是完整的代码,只是主循环:

    // Global constants:
var PLAYGROUND_WIDTH = 1000;
var PLAYGROUND_HEIGHT = 1000;
var REFRESH_RATE = 30;

//Constants for the gameplay
var smallStarSpeed = 1; //pixels per frame

var mediumStarSpeed = 3; //pixels per frame

var bigStarSpeed = 5; //pixels per frame

var percent = 1;

// --------------------------------------------------------------------
// -- the main declaration: --
// --------------------------------------------------------------------

$(function(){
//Calculate playground width and height:
PLAYGROUND_WIDTH = $(window).width() - 20;
PLAYGROUND_HEIGHT = $(window).height() - 20;

//Calculate Layour for responsive Design.
//Calculate Area:


// Animations declaration:
// The background:

var DM = new DeckManager;
var IM = new ImageManager;

IM.Create("Image1");


//DEBUG: Loading images for demo, this should be done using the image manager in the actual game.
var background1 = new $.gameQuery.Animation({
imageURL: "http://gamequeryjs.com/demos/3/background1.png"});
var background2 = new $.gameQuery.Animation({
imageURL: "http://gamequeryjs.com/demos/3/background2.png"});
var background3 = new $.gameQuery.Animation({
imageURL: "http://gamequeryjs.com/demos/3/background3.png"});
var background4 = new $.gameQuery.Animation({
imageURL: "http://gamequeryjs.com/demos/3/background4.png"});
var background5 = new $.gameQuery.Animation({
imageURL: "http://gamequeryjs.com/demos/3/background5.png"});
var background6 = new $.gameQuery.Animation({
imageURL: "http://gamequeryjs.com/demos/3/background6.png"});



var Face = new Array();
IM.Load("image2");
IM.Load("image3");
IM.Load("image4");
IM.Load("image5");


var resizeTimer;

//Event to handle resizing
//This event should fire under any circimstance, except when safari is NOT maximized, and windows resolution is changed (WTF?)
$(window).resize(function ()
{
clearTimeout(resizeTimer);
resizeTimer = setTimeout(Resized, 100);
});

//Actual Resizing Event
function Resized()
{
//Your function goes here


//Calculate playground width and height:
PLAYGROUND_WIDTH = $(window).width() - 20;
PLAYGROUND_HEIGHT = $(window).height() - 20;

//Calculate Layout for responsive Design.
};


// Initialize the game:
$("#playground").playground({
height: PLAYGROUND_HEIGHT,
width: PLAYGROUND_WIDTH,
keyTracker: true,
mouseTracker: true});

// Initialize the background
$.playground()
.addGroup("background", {width: PLAYGROUND_WIDTH,
height: PLAYGROUND_HEIGHT})
.addSprite("background1", {animation: background1,
width: PLAYGROUND_WIDTH,
height: PLAYGROUND_HEIGHT})
.addSprite("background2", {animation: background2,
width: PLAYGROUND_WIDTH,
height: PLAYGROUND_HEIGHT,
posx: PLAYGROUND_WIDTH})
.addSprite("background3", {animation: background3,
width: PLAYGROUND_WIDTH,
height: PLAYGROUND_HEIGHT})
.addSprite("background4", {animation: background4,
width: PLAYGROUND_WIDTH,
height: PLAYGROUND_HEIGHT,
posx: PLAYGROUND_WIDTH})
.addSprite("background5", {animation: background5,
width: PLAYGROUND_WIDTH,
height: PLAYGROUND_HEIGHT})
.addSprite("background6", {animation: background6,
width: PLAYGROUND_WIDTH,
height: PLAYGROUND_HEIGHT,
posx: PLAYGROUND_WIDTH})
$("#background").addSound(bgmusic);
//Setup obects so they can be reached randomly
var Vals = new Array();
var Vals2 = new Array();
var Turned = 0;
var TurnedMax = 2;

//This will ensure that two cards of each are added to the deck
//This function will be handled by the imagemanager at later stages.
for (var i = 0; i < NumberOfCards; ++i)
{
Vals[i] = Math.floor(i/2);
}

Vals2[0]=3;

DM.Create(Vals, Vals2, NumberOfCards, 1);

//Generate the actual object

$.playground()
.addGroup("object", {width: PLAYGROUND_WIDTH,
height: PLAYGROUND_HEIGHT})

for (var i = 0; i < NumberOfCards+1; ++i)
{
//Generate unique ID for the card
var name = "object"+i;

val = DM.Pushobject();

//Add the actual card to the playground, we spawn them in a responsive awy based on the resolution of the game.
$("#object").addSprite(name, {animation: IM.GetBack(), width: 208, height: 303, posx: (i%(Math.ceil(noc*Ratio))) *SpaceX + SpaceX - 104 + LastYOff * ( i>= (NumberOfCards + NumberOfCardsBonus) - ((NumberOfCards + NumberOfCardsBonus)%(Math.ceil(noc*Ratio))) ) , posy: Math.floor( i / (Math.ceil(noc*Ratio)) ) * SpaceY + SpaceY - 152 });//

//Add a class to the object, this, does nothing except make us able to search for objects with the same class later in the code.
$("#"+name).addClass("object");

//Create the actual class for the object, this will add logic to the object.
$("#"+name)[0].Cards = new Cards($("#"+name));
$("#"+name)[0].Cards.Create(val, IM.GetImage(val), IM.GetImage(3), IM.GetBack(), DM.LastBonus(), Scale);


//Add a mousedown event for the card, this mousedown will be run in the main
//environment rather than the class environment to make sure that we have access
//to all the data we need access to.
$("#"+name).mousedown(function(e)
{
var Ready = 0;
$(".object").each(function()
{
if (this.object.visible && (this.object.Turning==true || this.object.FlippedV==true))
{
Ready++;
}
});
//Find all the objects with the tag/class card.
$(".object").each(function()
{
//Check if the mouse clicked the object, if it's still part of the game, and if it has not been flipped.
if (e.clickedobject
&& Ready<TurnedMax)
{
//Run the clicked event for the card, this will start events etc.
this.object.Clicked();
//Increase the turned counter, if we have turned the correct amount of objectto be compared
//then compare them.
if (this.Cards.Bonus == false)
{
Turned++;
if (Turned==TurnedMax)
{
//We have turned the amount of object needed
//Find out which value the first object has, and use this as a base to compare if cards match.
//Also instantiate a counter for the amount of cards actually matching.
//It's done this way if you want a variable number of object needed for a match.
var Correct = this.object.value;
var CorrectAmount = 0;
$(".object").each(function()
{
//For each card, if they are flipped, are not going into hiding/deletion, and has the
//Correct value, increase the counter for the number of objects matching.
if (this.object.Flipped == true && this.object.Hiding==0 && this.object.value == Correct)
CorrectAmount++;

});

//If we have a correct match
if (CorrectAmount==TurnedMax)
{
$(".object").each(function()
{
//Foreach object that is flipped and not in hiding, delete them (aka. yay, you got a match).
if (this.object.Flipped==true && this.object.Hiding==0)
this.object.SetVisible(false);
});
}

$(".object").each(function()
{
//Foreach objectthat was not in hiding and was not part of the match, unflip them again.
if (this.object.Flipped==true && this.object.Hiding==0)
this.object.Hide();
});

Turned=0;
}
}
}
});
});


}

// this sets the id of the loading bar (NOT USED YET):
$.loadCallback(function(percent){
$("#loadingBar").width(400*percent);
});
//initialize the start button
$("#startbutton").click(function(){
$.playground().startGame(function(){
$("#welcomeScreen").remove();
});
})


//This is for the background animation (DEBUG)
$("#playground").registerCallback(function(){

$("#background1").x(($("#background1").x() + smallStarSpeed +PLAYGROUND_WIDTH) % (2 * PLAYGROUND_WIDTH) - PLAYGROUND_WIDTH);
$("#background2").x(($("#background2").x() + smallStarSpeed +PLAYGROUND_WIDTH) % (2 * PLAYGROUND_WIDTH) - PLAYGROUND_WIDTH);

$("#background3").x(($("#background3").x() + mediumStarSpeed +PLAYGROUND_WIDTH) % (2 * PLAYGROUND_WIDTH) - PLAYGROUND_WIDTH);
$("#background4").x(($("#background4").x() + mediumStarSpeed +PLAYGROUND_WIDTH) % (2 * PLAYGROUND_WIDTH) - PLAYGROUND_WIDTH);

$("#background5").x(($("#background5").x() + bigStarSpeed +PLAYGROUND_WIDTH) % (2 * PLAYGROUND_WIDTH) - PLAYGROUND_WIDTH);
$("#background6").x(($("#background6").x() + bigStarSpeed +PLAYGROUND_WIDTH) % (2 * PLAYGROUND_WIDTH) - PLAYGROUND_WIDTH);


//Basic Game Engine!!

$(".object").each(function()
{
//For each card, perform their step event.
this.object.Step();
});

}, REFRESH_RATE);
});

最佳答案

你说你正在使用 <meta http-equiv="X-UA-Compatible" content="IE=9"> .

这根本不可能有任何帮助。

在 IE9 或 IE8 中完全没有效果。

它将在 IE10 和 IE11 中产生影响,但这种影响将把浏览器拉低到向后兼容模式,这基本上意味着你告诉 IE10 和更高版本关闭它们的一些新功能并且伪装成 IE9。

那真的毫无意义;事实上,它可能弊大于利,因此我建议完全删除该行代码。

如果你必须有一个 X-UA-Compatible标志,唯一合理的值是 IE=edge .这将强制所有 IE 版本使用它们可用的最佳模式。在大多数情况下,这是默认设置,但在默认设置为其他内容的少数情况下,它可能会有所帮助。

但您在这里提示的真正问题是性能,我可以明确地告诉您,这与 X-UA-Compatible 完全无关。 flag -- 正如我所说,您设置它的方式无论如何都不会对 IE8 产生任何影响,所以它不是让事情变慢的原因。

最终,您对 IE8 的问题可能会归结为一个简单的事实,即它只是一个比更新版本慢得多的浏览器。您可能根本无法让 IE8 的性能足以达到您想要的效果。

IE9 和 IE10 要好得多,所以您应该能够通过一些调整从它们获得合理的性能,所以您最好的希望可能是进行一些性能调整。

性能测试的最佳选择是升级到 IE11,它具有比 IE10 好得多的开发工具,包括一些非常好的用于分析网站性能的工具。

性能调整本身就是一个大话题(超出了本回答的范围),但最终目标是找出导致最大瓶颈的原因,并解决这些问题。在大多数情况下,通常只有一两个主要问题阻碍了一切,处理它们通常足以改变现状。

所以我的建议是:下载 IE11 并尝试新开发工具中的新性能测试功能。希望这足以让您入门,但如果您仍然找不到问题,请尝试提出一个更具体的问题,了解为什么您的代码的特定部分很慢。

我建议的另一件事是尝试减少对 jQuery 等库的使用。

jQuery 非常有用,但它确实会影响性能。尽可能尝试切换到 native Javascript。

如果您做不到,请尝试减少调用同一电话的次数。例如:

$("#background1").x(($("#background1").x() + smallStarSpeed +PLAYGROUND_WIDTH) % (2 * PLAYGROUND_WIDTH) - PLAYGROUND_WIDTH);

这调用了 $("#background1")两次。改为这样做:

var $bg1 = $("#background1");
$bg1.x(($bg1.x() + smallStarSpeed +PLAYGROUND_WIDTH) % (2 * PLAYGROUND_WIDTH) - PLAYGROUND_WIDTH);

更好的是,如果你能用 document.getElementByTagName 做到这一点而不是 jQuery,您将为自己节省更多的性能。

我还注意到您在该代码中重复了一些计算。显然,如果您可以整理一下以减少重复,也会有所帮助。

我希望这能给你一些思考的方向。

关于javascript - html5/javascript 游戏在 ie8 中运行缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18351465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com