1

go = true; を設定するようにキャンバスをプログラムするにはどうすればよいですか。キャンバス上の「再生」という単語をクリックすると、通常のjsで?go = true の場合、ゲームを開始します。このコードは play をキャンバスに出力します。. これは私のjsコードです:

var canvas = document.getElementById("gameCanvas");
var ctx = canvas.getContext("2d");
canvas.width = 800;
canvas.height = 336;


    var go = false;

    var reset = function() {
        //reset function
    };

    var main = function () {
    //main function
    };

    var menu = function () {

        ctx.clearRect (0 , 0 , canvas.width , canvas.height);

        ctx.fillStyle = "rgb(250, 250, 250)";
        ctx.font = "24px Helvetica";
        ctx.textAlign = "left";
        ctx.textBaseline = "middle";
        ctx.fillText("play", 32, 32);
    }

    if (go == true) {
        reset();
        var then = Date.now();
        setInterval(main, 1);
    }
    else {
        menu();
    }
4

2 に答える 2

1

キャンバスをクリックしたときにマウスがテキストの境界ボックス内にあるかどうかを確認するだけです。クリック時にマウスの位置がテキストのバウンディング ボックス内にある場合、テキストはクリックされており、 に設定goする必要がありますtrue

このようなものが役立ちます。

var bbox = {x:10,y:10, w:100, h:100};
if(mouseX >= bbox.x && mouseY >= bbox.y && mouseX <= bbox.x + bbox.w && mouseY <= bbox.y + bbox.h){
    go = true;
}
于 2013-06-13T23:34:21.653 に答える