Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.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_Arrays_Html_Canvas - Fatal编程技术网

Javascript游戏麻烦

Javascript游戏麻烦,javascript,arrays,html,canvas,Javascript,Arrays,Html,Canvas,我正在用Javascript和HTML5的画布API制作一个游戏,遇到了一个障碍 游戏效果之一是导弹沿y轴向上飞行,受到阵风(由涡轮机发出)的影响,阵风使导弹沿x轴移动。我在制造一台这样的涡轮机方面没有问题,但当它们的数量增加到3台时,我遇到了麻烦。 在一个新的层次上,我将这些涡轮机实例化为对象,然后将其推送到一个数组中,如下所示: function gameStateNewLevel(){ for (var i = 0; i < 2; i++){ turbine

我正在用Javascript和HTML5的画布API制作一个游戏,遇到了一个障碍

游戏效果之一是导弹沿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);
}
函数gameStateNewLevel(){
对于(变量i=0;i<2;i++){
涡轮={};
涡轮机宽度=10;
turbine.height=Math.floor(Math.random()*200);//涡轮机高度
turbine.y=Math.floor(Math.random()*600)//涡轮机y轴
if(Math.random()*10>5){//表示画布的涡轮机侧
涡轮机侧=左侧;
}否则{
涡轮机侧=右侧;
}

如果(turbine.height您的问题可能是索引重叠;内环正在更改外环的
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?
                }
            }   
        }
    }
}
函数updateTurbines(){
德图尔宾={}
涡轮位置顶部=涡轮位置y;
浊度位置底部=试探性水轮机.y+试探性水轮机.height;
对于(var i=光线长度;i<2;i++){
风轮机=涡轮机[i];
对于(var j=光线长度;j<2;j++){
如果(诱惑都市!==诱惑都市[j]){
而(){
//这到底是怎么回事?
}
}   
}
}
}

请告诉我这是否解决了问题。

您的代码中有错误,请参阅此处的注释:

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 (){

                }
            }   
        }
    }
}
函数updateTurbines(){
德图尔宾={}
turbinePositionTop=TENTURBINE.y;//TENTURBINE现在是一个空对象,因此.y未定义
turbinePositionBottom=TENTURBINE.y+TENTURBINE.height;//这里也有同样的问题
对于(var i=raylength;i<2;i++){//为什么我要从数组的末尾开始?2代表什么?
风轮机=涡轮机[i];
对于(var i=raylength;i<2;i++){//为什么要这样做两次?我也在破坏“i”的值
if(诱惑都市人!==诱惑都市人[i]){
而(){
}
}   
}
}
}
下面是我如何在不知道它在做什么的情况下重写它:

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 (){
                     // ...
                }
            }   
        }
    }
}
函数updateTurbines(){
var l=turbines[i].length;//直接从数组[1]获取turbines长度
对于(var i=0;i

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

回复很有用,但没有一个有效…这是我最后得到的代码

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){

    }
   }
  }
 }
函数updateTurbines(){
var l=长度;
对于(var i=0;i=试探城市2.PositionTop){
}
}
}
}

Suggestion:your
if(turbine.height@Jeffrey或从一开始就使用
100+Math.random()*100
。@安德鲁:是的……这可能是个更好的主意:)谢谢你的回答。我明天会给你一个机会,告诉你结果如何,我真的很感激。嘿,我只是想说你很接近,但这是错误的。var l=turnes[I]。length;这只得到了当前对象的长度。Detrurbine也没有正确定义,在第8行它应该是Detrurbine[j]。不管怎样,它都不起作用。我已在答案中发布了工作代码。同时,谢谢你,这不仅仅是修复的问题,而只是澄清函数的外观。我也会记住你的建议。干杯!
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){

    }
   }
  }
 }