我正在使用HTML5的canvas API和Javascript制作一款游戏,但现在遇到了一个问题。
游戏中有一个导弹沿y轴向上移动的效果,它会受到风扇(发射风力)在x轴方向上的影响。我已经成功地制作了一个风扇,但当它们的数量增加到3个时,我遇到了麻烦。 在新的关卡中,我将这些风扇实例化为对象,然后将它们推入一个数组中,如下所示:
现在,在它们被呈现之前,它们也会通过updateTurbine函数进行更新。这个函数的作用只是确保涡轮之间没有重叠,并根据需要沿y轴向上或向下移动它们(通过循环遍历数组并比较其中的每个对象)。我尝试了一下写这个函数,但是在所有循环中完全迷失了。这是我做到的最远的地方,我有一种感觉我走错了路:
你可以在www.techgoldmine.com找到源代码,并且我没有使用这个东西也能成功运行。
游戏中有一个导弹沿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找到源代码,并且我没有使用这个东西也能成功运行。
if(turbine.height <= 100){
应该改为while(turbine.height <= 100){
。 - Jeffrey Sweeney100 + Math.random()*100
... - Andrew