如果球触及挡块的顶部和底部,我怎么能改变它的dy?现在它只是一个摆动效果,当它击中顶部或底部时被困在块内。 这是我的Jsfiddle:https://jsfiddle.net/6qh70wdo/
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge" >
<!--... more here...-->
</head>
<body>
<!--... more here...-->
<input type="text" id="wall_color_picker" onclick="startColorPicker(this)" title="Wall Color" value="#FFFFFF">
<!--... more here...-->
</body>
</html>
答案 0 :(得分:0)
我会把它分成两部分。首先检测它是否被击中然后担心它击中的位置。我想你可以这样做:
var bool = ball.x - ball.radius < block.x + block.w &&
ball.x + ball.radius > block.x &&
ball.y - ball.radius < block.y + block.h &&
ball.y + ball.radius > block.y) {
ball.dx = -ball.dx;
if(bool) // if true, meaning a hit, find which direction.
if(ball.x - ball.radius < block.x + block.w || ball.x + ball.radius > block.x){
ball.dx = -ball.dx; //hit detected on left/right...change left/right direction
}else{
ball.dy = -ball.dy; //hit detected on top/bottom...change top/bottom direction
}
}