球弹出墙壁 - 暂停&恢复

时间:2017-03-01 02:34:30

标签: javascript

我正在研究这个JavaScript代码,让一个球从墙上反弹并暂停&点击继续。我已经让球停下来,并想要使用布尔值启用恢复的潜在选项的反馈。 setTimer()& stopTimer()是预定义的函数。

var ball;
var dx = 4;
var dy = 4;
var isPaused = false;


/* This program has a ball bounce around the screen. */
function start(){
    ball = new Circle(20);
    ball.setPosition(100, 100);
    add(ball);

    setTimer(draw, 20);
    if(isPaused == true){
        mouseClickMethod(resume);
    }
}

function draw(){
    checkWalls();
    ball.move(dx, dy);
    mouseClickMethod(pause);
}

function checkWalls(){
    // Bounce off right wall
    if(ball.getX() + ball.getRadius() > getWidth()){
        dx = -dx;
    }

    // Bounce off left wall
    if(ball.getX() - ball.getRadius() < 0){
        dx = -dx;
    }

    // Bounce off bottom wall
    if(ball.getY() + ball.getRadius() > getHeight()){
        dy = -dy;
    }

    // Bounce off top wall
    if(ball.getY() - ball.getRadius() < 0){
        dy = -dy;
    }
}

function pause(e){
    var isPaused = true;
    if(isPaused == true){
        stopTimer(draw);
    }
}

function resume(e){
    var isPaused = false;
    if(isPaused == false){
        setTimer(draw, 20);
    }
}

4 个答案:

答案 0 :(得分:0)

刚想通了。这就完成了工作。

var ball;
var dx = 4;
var dy = 4;
var isPaused;


/* This program has a ball bounce around the screen. */
function start(){
    ball = new Circle(20);
    ball.setPosition(100, 100);
    add(ball);

    setTimer(draw, 20);
}

function draw(){
    checkWalls();
    ball.move(dx, dy);
    mouseClickMethod(pause);
}

function checkWalls(){
    // Bounce off right wall
    if(ball.getX() + ball.getRadius() > getWidth()){
        dx = -dx;
    }

    // Bounce off left wall
    if(ball.getX() - ball.getRadius() < 0){
        dx = -dx;
    }

    // Bounce off bottom wall
    if(ball.getY() + ball.getRadius() > getHeight()){
        dy = -dy;
    }

    // Bounce off top wall
    if(ball.getY() - ball.getRadius() < 0){
        dy = -dy;
    }
}

function pause(){
    isPaused = true;
    if(isPaused == true){
        stopTimer(draw);
    }
    mouseClickMethod(resume);
}

function resume(){
    isPaused = false;
    if(isPaused == false){
        setTimer(draw, 20);
    }
}

答案 1 :(得分:0)

这是我如何做到的。它适用于那些只是寻找快速答案的人,为非常规的命名道歉,我讨厌拼写。

var ball;
var dx = 4;
var dy = 4;
var move = true;
/* This program has a ball bounce around the screen. */
function start(){
    ball = new Circle(20);
    ball.setPosition(100, 100);
    add(ball);
    mouseClickMethod(stp);
    setTimer(draw, 20);
}

function draw(){
    if(move){
        checkWalls();
        ball.move(dx, dy);
    
    }

}

function checkWalls(){
    // Bounce off right wall
    if(ball.getX() + ball.getRadius() > getWidth()){
        dx = -dx;
    }

    // Bounce off left wall
    if(ball.getX() - ball.getRadius() < 0){
        dx = -dx;
    }

    // Bounce off bottom wall
    if(ball.getY() + ball.getRadius() > getHeight()){
        dy = -dy;
    }

    // Bounce off top wall
    if(ball.getY() - ball.getRadius() < 0){
        dy = -dy;
    }
}
function stp(){
    if(move){
         move = false;
    }else{
        move = true;
    }
}

答案 2 :(得分:0)

var isPaused = false;

/* This program has a ball bounce around the screen. */
function start(){
    ball = new Circle(20);
    ball.setPosition(100, 100);
    add(ball);
    mouseClickMethod(pause);
    setTimer(draw, 20);
}
function pause(e){
    isPaused = !isPaused;
    if(isPaused == true){
        stopTimer(draw);
    }else{
        setTimer(draw, 20);
    }
}

    

答案 3 :(得分:-1)

大约三年后,那些代码因为它不能正常工作,提示第一个加速每次点击,第二个甚至无法整体工作。