Javascript游戏麻烦

时间:2012-02-07 19:49:58

标签: javascript arrays html5 canvas

我正在使用HTML5的canvas API在Javascript中制作游戏并遇到障碍。

其中一个游戏效果是导弹沿着y轴向上行进,受到阵风(由涡轮机发出)的影响,沿着x轴移动。制作其中一台涡轮机我没有问题,但当它们的数量增加到3时,我遇到了麻烦。 在一个新的级别上,我将这些涡轮机实例化为对象,然后将其推入一个数组中,如下所示:

function gameStateNewLevel(){

    for (var i = 0; i < 2; i++){
        turbine = {};
        turbine.width = 10;
        turbine.height = Math.floor(Math.random()*200); //turbine height
        turbine.y = Math.floor(Math.random()*600) //turbine y-axis 
        if (Math.random()*10 > 5){ //indicates turbine side of canvas
            turbine.side = leftSide;
        }else{
            turbine.side = rightSide;
        }
        if(turbine.height <= 100){ //Increases turb. size if it's too small
            turbine.height += Math.floor(Math.random()*100);
        }

        turbines.push(turbine);

    }

    context.fillStyle = "#FFFFFF"       
    switchGameState(GAME_STATE_PLAYER_START);
}

现在,在渲染之前,它们也会通过updateTurbine函数进行更新。所有这些功能应该确保涡轮机不相互重叠,并根据需要在y轴上向上或向下移动(通过循环数组并比较其中的每个对象)。我已经开始制作这个功能,但是我在所有循环之间完全迷失了。这就是我所拥有的,我感觉自己走错了路线:

function updateTurbines(){
    tempTurbine = {}
    turbinePositionTop = tempTurbine.y;
    turbinePositionBottom = tempTurbine.y + tempTurbine.height;
    for (var i = turbineArrayLength; i < 2; i++){
        tempTurbine = turbines[i];
        for (var i = turbineArrayLength; i < 2; i++){
            if (tempTurbine !== tempTurbines[i]){
                while (){

                }
            }   
        }
    }
}

你可以在www.techgoldmine.com上找到源代码和我得到的最远的东西

3 个答案:

答案 0 :(得分:5)

您的代码中存在错误,请参阅此处的评论:

function updateTurbines(){
    tempTurbine = {}
    turbinePositionTop = tempTurbine.y; // tempTurbine is an empty object right now so .y is undefined
    turbinePositionBottom = tempTurbine.y + tempTurbine.height; // same problem here
    for (var i = turbineArrayLength; i < 2; i++){ // why am I starting at the end of the array? What's the 2 for?
        tempTurbine = turbines[i];
        for (var i = turbineArrayLength; i < 2; i++){ // why am I doing this twice? I'm also clobbering the value of "i"
            if (tempTurbine !== tempTurbines[i]){
                while (){

                }
            }   
        }
    }
}

以下是我在不知道它在做什么的情况下重写它的方法:

function updateTurbines(){
    var l = turbines[i].length; // get the turbine length directly from the array[1]
    for (var i = 0; i < l; i++){ // go through all of the turbines
        var tempTurbine = turbines[i];
        turbinePositionTop = tempTurbine.y; // now .y is defined because tempTurbine is not an empty object
        turbinePositionBottom = tempTurbine.y + tempTurbine.height;
        for (var j = 0; j < l; j++) { // NOT i but j here
            if (tempTurbine !== tempTurbines[j]){
                while (){
                     // ...
                }
            }   
        }
    }
}

[1]如果修改数组,这可能会导致错误。我假设你现在只添加它。

答案 1 :(得分:2)

您的问题很可能是索引重叠;内部循环正在改变外部循环的i值。

我在内循环中将i更改为j

function updateTurbines(){
    tempTurbine = {}
    turbinePositionTop = tempTurbine.y;
    turbinePositionBottom = tempTurbine.y + tempTurbine.height;
    for (var i = turbineArrayLength; i < 2; i++){
        tempTurbine = turbines[i];
        for (var j = turbineArrayLength; j < 2; j++){
            if (tempTurbine !== tempTurbines[j]){
                while (){
                    //What the heck is going on here?
                }
            }   
        }
    }
}

如果修复它,请告诉我。

答案 2 :(得分:0)

回复很有帮助,但没有一个有效......这是我最终得到的代码。

function updateTurbines(){
    var l = turbines.length; 
    for (var i = 0; i < l; i++){ // go through all of the turbines
        var tempTurbine1 = turbines[i];
        tempTurbine1.PositionTop = tempTurbine1.y; // now .y is defined because tempTurbine is not an empty object
        tempTurbine1.PositionBottom = tempTurbine1.y + tempTurbine1.height;
        for (var j = 0; j < l; j++) { // NOT i but j here
            var tempTurbine2 = turbines[j];
            if (tempTurbine1 !== tempTurbine2 && tempTurbine1.PositionBottom >= tempTurbine2.PositionTop){

    }
   }
  }
 }
相关问题