我正在尝试用javascript制作HTML5游戏。与乒乓球相似,但是一旦气泡碰到桨叶或底部筛网,它就会“破裂”或消失。我似乎无法使气泡消失或在我的代码中破裂。有人可以帮我吗?

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 batonMovement = 200;
var moveBatonRight = false;
var moveBatonLeft = false;
function startGame()
{
    var r,g,b;
    var canvas,color;
    //Initialize
    canvasColor = '#EAEDDC';
    x = 10;
    y = 10;
    radius = 10;
    clearScreen();
    counter=0;


    while (counter <100)
    {

                //make bubble appear randomly
                x=Math.floor(Math.random()*450)

                //Set up a random color
                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('drawForever()',50);
}

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

function drawForever()
{
    var canvas, pen;

    canvas = document.getElementById('myCanvas');
    pen = canvas.getContext('2d');
    steps +=1
    clearScreen();
    if (steps%20==0 && lastBubble < 100){
        bubbles[lastBubble].active=true;
        lastBubble +=1;
    }
    drawBaton();
    counter=0;


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

            bubbles[counter].y+=2;

            }

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

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

    }
}

function clearScreen()
{
    var canvas, pen;

    canvas = document.getElementById('myCanvas');
    pen = canvas.getContext('2d');
    pen.fillStyle = canvasColor;
    pen.fillRect(0,0,450,300);

}
function drawBaton()
{
    var canvas, pen;
    if (moveBatonLeft == true && batonMovement > 0)
    {
        batonMovement -= 20;
    }
    else if (moveBatonRight == true && batonMovement < 400)
    {
        batonMovement += 20;
    }
//draw Baton (rectangle)
    canvas = document.getElementById('myCanvas');
    pen = canvas.getContext('2d');
    pen.fillStyle = '#0000FF';

    pen.fillRect(batonMovement,250,50,10);

}
function moveLeft()
{
        moveBatonLeft=true;
}
function moveRight()
{
        moveBatonRight=true;
}
function stopMove()
{
    moveBatonLeft=false;
    moveBatonRight=false;
}


以下是HTML代码...

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Bubble Burster</title>
        <script type="text/javascript" src="project.js"></script>
    </head>
    <body onload="startGame();">
        <div style="text-align:center;">
            <h2>Bubble Burster</h2>
            <canvas id="myCanvas" width="450" height="300" style="border:5px solid #000000; background-color: #f1f1f1;">
                Your browser does not support the canvas element.
            </canvas><br>
            <button onmousedown="moveLeft();" onmouseup="stopMove();">LEFT</button>
            <button onmousedown="moveRight();" onmouseup="stopMove();">RIGHT</button><br><br>
            <form>
                <input type="radio" name="difficulty" value="easy" onclick="check(this.value);" checked> Easy &nbsp;&nbsp;
                <input type="radio" name="difficulty" value="moderate" onclick="check(this.value)"> Moderate&nbsp;&nbsp;
                <input type="radio" name="difficulty" value="hard" onclick="check(this.value)"> Hard  <br><br>
            </form>
            <span id="burst">Burst:</span> &nbsp;&nbsp;
            <span id="escaped">Escaped: </span> &nbsp;&nbsp;
            <span id="steps">Steps elapsed:</span> &nbsp;&nbsp;
            <h3 id="output"></h3>
        </div>
    </body>
</html>

最佳答案

您所指的是xydrawForever函数中未定义的。 xy的全局值可能未引用正确的气泡。实际上,在y函数中startGame设置为10,并且此后从未更改。您可能还想引用bubbles[counter]而不是bubbles[lastBubble]。如果您进行以下更改,则游戏似乎可以正常工作,请参考bubbles[counter].xbubbles[counter].y而不是xy

function drawForever() {
    var canvas, pen;

    canvas = document.getElementById('myCanvas');
    pen = canvas.getContext('2d');
    steps += 1
    clearScreen();
    if (steps % 20 == 0 && lastBubble < 100) {
        bubbles[lastBubble].active = true;
        lastBubble += 1;
    }
    drawBaton();
    counter = 0;


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

            bubbles[counter].y += 2;

        }
        y = bubbles[counter].y;    // ADDED (y was not defined in original code)
        x = bubbles[counter].x;    // ADDED (x was not defined in original code)
        if (y >= 240 && y <= 270 && x >= batonMovement - 10 && x <= batonMovement + 60) {
            bubbles[counter] = false;   // ALTERED (burst the current bubble, not whatever lastBubble refers to

        } else if (y >= 450) {
            bubbles[counter] = false;   // ALTERED (burst the current bubble, not whatever lastBubble refers to
        }
        counter += 1;

    }
}


https://jsfiddle.net/acLot87q/4/

您还应该在每个函数中使xy局部变量;他们目前没有达到您似乎认为的目的。

10-04 17:03