为了账号安全,请及时绑定邮箱和手机立即绑定

速度越来越快怎么办?

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<meta http-equiv="X-UA-Compatible" content="ie=edge">

<title>Canvas</title>

</head>

<body>

<canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto; ">

当前浏览器不支持Canvas,请更换浏览器

</canvas>

</body>

<script>

var ball = {x:512, y:100, r:20, g:2, vx:-4, vy:0, color:"#005588" }

var timer = null;

window.onload = function(){

var canvas = document.getElementById("canvas");

canvas.width = 1024;

canvas.height = 768;


if(canvas.getContext){

var context = canvas.getContext("2d");

}

timer = setInterval(

function(){

render(context);

update();

},50

)


}


function update(){

ball.x += ball.vx;

ball.y += ball.vy;

ball.vy += ball.g;


if( ball.y >= 768-ball.r ){

ball.y = 768-ball.r;

ball.vy = -ball.vy

}

if( ball.y <= ball.r ){

ball.y = ball.r;

ball.vy = -ball.vy

}


if( ball.x <= ball.r ){

ball.x = ball.r;

ball.vx = -ball.vx;

}

if( ball.x >= 1024-ball.r ){

ball.x = 1024-ball.r;

ball.vx = -ball.vx;

}


}


function render(cxt){

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


cxt.fillStyle = ball.color;

cxt.beginPath();

cxt.arc(ball.x,ball.y,ball.r,0,2*Math.PI);

cxt.closePath();


cxt.fill();


}

</script>

</html>


正在回答

1 回答

ball.x += ball.vx;

ball.y += ball.vy;

ball.vy += ball.g;

小球的速度没有任何的减损 ,随着时间的增长小球的速度自然越来越快

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

速度越来越快怎么办?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信