2

html/javascript でゲームを作成しました。残念ながら、このプロジェクトには多くの秘密が隠されているため、多くのコードを開示することはできません。

ただし、本質的には、すべてのブラウザーで非常にうまく動作するということです。<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);
});
4

2 に答える 2

4

を使用していると言います<meta http-equiv="X-UA-Compatible" content="IE=9">

それは、まったく役に立たない可能性があります。

IE9 または IE8 ではまったく効果がありません。

IE10 と IE11 で効果がありますが、その効果はブラウザーを後方互換モードに引き下げることです。これは基本的に、IE10 以降に新しい機能の一部をオフにして、そのふりをするように指示していることを意味します。 IE9。

それにはまったく意味がありません。実際、これはおそらく良いことよりも悪いことの方が多いので、そのコード行を完全に削除することをお勧めします。

フラグが必要X-UA-Compatibleな場合、適切な値は だけですIE=edge。これにより、IE のすべてのバージョンで、使用可能な最適なモードが強制的に使用されます。とにかく、ほとんどの場合、これがデフォルトですが、デフォルトが他のものであるいくつかのケースでは役立つかもしれません.

しかし、ここであなたが不満を言っている本当の問題はパフォーマンスです。これはX-UA-Compatibleフラグとはまったく関係がないと断言できます。 、だから物事を遅くしているのはそれではありません。

最終的に、IE8 に関する問題は、IE8 が後のバージョンよりもはるかに遅いブラウザーであるという単純な事実に要約される可能性があります。IE8 で目的を達成するのに十分なパフォーマンスを得ることができない可能性があります。

IE9 と IE10 ははるかに優れているため、少し調整するだけで妥当なパフォーマンスを得ることができるはずです。そのため、おそらくパフォーマンス チューニングを行うことが最善の方法です。

パフォーマンス テストの最善策は、 IE10 よりもはるかに優れた開発ツールを備えた IE11 にアップグレードすることです。これには、サイトのパフォーマンスを分析するための非常に優れたツールがいくつか含まれています。

パフォーマンス チューニングはそれ自体が大きなトピックです (この回答の範囲外です) が、最終的な目標は、最大のボトルネックの原因を突き止め、それらを修正することです。ほとんどの場合、すべてを妨げている主要な問題は 1 つまたは 2 つだけであり、通常はそれらに対処するだけで違いが生まれます。

私のアドバイスは、IE11 をダウンロードして、新しい開発ツールの新しいパフォーマンス テスト機能を試してみることです。開始するにはこれで十分だと思いますが、それでも問題が見つからない場合は、コードの特定の部分が遅い理由について、より具体的な質問をしてみてください。

もう 1 つお勧めすることは、jQuery などのライブラリの使用を減らすことです。

jQuery は非常に便利ですが、パフォーマンスに影響を与えます。可能であれば、ネイティブ Javascript に切り替えてみてください。

それができない場合は、同じ電話をかける回数を減らしてみてください。例えば:

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

これは$("#background1")2回呼び出します。代わりにこれを行います:

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

さらに良いことに、jQuery ではなくそれを行うことができれば、document.getElementByTagNameパフォーマンスをさらに節約できます。

また、そのコードでいくつかの計算を繰り返していることにも気付きました。明らかに、繰り返しを減らすためにそれを整理できれば、それも役立ちます.

考えるヒントになれば幸いです。

于 2013-08-21T09:15:29.260 に答える
0

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE9" > html head タグを入れて試してみてください。互換表示をオンに切り替えると、IE が IE8 レンダリングに変わります。

http://html5test.com/compare/browser/ie08/chrome28/ff22.html

于 2013-08-21T08:06:12.847 に答える