gpt4 book ai didi

javascript - HTML5/Javascript 游戏

转载 作者:行者123 更新时间:2023-12-03 04:21:03 24 4
gpt4 key购买 nike

我目前正在开发一款游戏,我需要一堆随机的气泡落下,并且在其下方有一个可以移动以“爆破”这些气泡的矩形。我目前有一个代码不会丢弃这些气泡。有人能告诉我哪里出了问题吗?这是我的代码:

var canvasColor;
var x,y,radius,color;
var x=50, y=30;
var bubbles= [];
var counter;
var lastBubble=0;
var steps=0, burst=0, escaped=0;
var xx=200;
var moveRectangleRight=false;
var moveRectangleLeft=false;


function startGame()
{
var r, g, b;
canvasColor= '#f1f1f1';
x=10;
y=10;
radius=10;
clearScreen();
counter=0;

while (counter<100)
{
x= Math.floor(450*Math.random()+1);

r = Math.floor(Math.random()*256);
g = Math.floor(Math.random()*256);
b = Math.floor(Math.random()*256);

color='rgb('+r+','+g+','+b+')';

bubbles[counter]=new Bubble(x,y,radius,color);
counter+=1;
}
setInterval('drawEverything()',50);
}

function Bubble(x,y,radius,color)
{
this.x=x;
this.y=y;
this.radius=radius;
this.color=color;
this.active=false;
}

function drawEverything()
{
var canvas=document.getElementById('myCanvas');
var context= canvas.getContext('2d');

steps+=1;
clearScreen();

if (steps%20==0 && lastBubble <100) {
bubbles[lastBubble].active=true;
lastBubble+=1;
}

drawRectangle();
counter=0;

while(counter<100)
{
if (bubbles[counter].active==true) {
context.fillStyle= bubbles[counter].color;
context.beginPath();
context.arc(bubbles[counter].x, bubbles[counter].y,
bubbles[counter.radius], 0, 2*Math.PI);
context.fill();
bubbles[counter].y+=2;
}

y=bubbles[counter].y;
x=bubbles[counter].x;

if (y>=240 && y<=270 && x>=xx-10 && x<=xx+60)
{
bubbles[counter]=false;
}


else if (y>=450)
{
bubbles[counter]=false;
}
counter+=1;
}
}

function clearScreen ()
{
var canvas=document.getElementById('myCanvas');
var context= canvas.getContext('2d');
context.fillStyle= canvasColor;
context.fillRect(0,0,450,300);
}


function drawRectangle()
{ var canvas, context;

if (moveRectangleRight==true && xx<400){
xx+=20;
}

if (moveRectangleLeft==true && xx>0){
xx-=20;
}

canvas=document.getElementById('myCanvas');
context= canvas.getContext('2d');
context.fillStyle = 'blue';
context.fillRect(xx,250,50,10);

}

function moveLeft () {
moveRectangleLeft=true;
}

function moveRight() {
moveRectangleRight=true;
}

function stopMove () {
moveRectangleRight=false;
moveRectangleLeft=false;
}

最佳答案

您的问题是您将 counter 初始化为 1,因此当您向 counter 添加项目时,您会从 1 的索引开始,这实际上是第二个nd项目。因此,当您尝试访问bubbles[0]时,它会返回未定义。相反,将 counter 初始化为 0

关于javascript - HTML5/Javascript 游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43943921/

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