当点击一个按钮时使球弹跳 [英] making a ball to bounce when a button is clicked
本文介绍了当点击一个按钮时使球弹跳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我点击了一个按钮后,编写了一个程序来移动球。它是bigegr项目实验的一部分:
以下是截图:
这里是代码:
<!DOCTYPE html> ;
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title> HTML5随机弹跳球< / title>
< style type =text / css>
<! -
body {background-color:#ededed; font:normal 12px / 18px Arial,Helvetica,sans-serif; }
h1 {display:block;宽度:600px的; margin:20px auto;填充底:20像素; font:normal 24px / 30px格鲁吉亚,Times New Roman,Times,serif;颜色:#333; text-shadow:1px 2px 3px #ccc; border-bottom:1px solid #cbcbcb; }
#myCanvas {background:#fff; border:1px solid #cbcbcb; }
#nav {display:block;宽度:100%;文本对齐:中心; }
#nav li {display:block;字体重量:粗体;行高:21px; text-shadow:1px 1px 1px #fff;宽度:100像素;高度:21px;填充:5像素;保证金:0 10px;背景:#e0e0e0; border:1px solid #ccc; -moz-边界半径:4PX; -webkit-边界半径:4PX;边界半径:4PX;向左飘浮; }
#nav li a {color:#000;显示:块;文字修饰:无;宽度:100%;高度:100%; }
- >
< / style>
< script type =text / javascript>
函数myFunction(){
if(document.getElementById('button')。clicked == true)
{
var context;
var dx = 4;
var dy = 4;
var y = 150;
var x = 10;
function draw(){
context = myCanvas.getContext('2d');
context.clearRect(0,0,400,400);
context.beginPath();
context.fillStyle =#000000;
context.arc(x,y,10,0,Math.PI * 2,true);
context.closePath();
context.fill();
if(x <0 || x> 400)
dx = -dx;
if(y <0 || y> 300)
dy = -dy;
x + = dx;
y + = dy;
}
setInterval(draw,10);
}
}
< / script>
< / head>
< body>
< div id =container>
< canvas id =myCanvaswidth =400height =300>< / canvas>
< / div>
< input id =buttontype =submitname =buttonvalue =enteronclick =myFunction();/>
< / body>
< / html>
问题是:当我点击该按钮时,球就不会被动画或移动。我错在哪里?
解决方案
第19行的代码返回undefined
document.getElementById('button')。clicked == true
if语句和它的工作原理
我做了一个jsbin: http ://jsbin.com/sozicuruja/1/
function myFunction(){var context; var dx = 4; var dy = 4; var y = 150; var x = 10;函数draw(){context = myCanvas.getContext('2d'); context.clearRect(0,0,400,400); context.beginPath(); context.fillStyle = #000000; context.arc(X,Y,10,0,Math.PI * 2,TRUE); context.closePath(); context.fill();如果(x <0 || x> 400)dx = -dx;如果(y <0 || y> 300)dy = -dy; X + = DX; Y + = DY; } setInterval(draw,10); }
body {background-color:#ededed; font:normal 12px / 18px Arial,Helvetica,sans-serif; } h1 {display:block;宽度:600px的; margin:20px auto;填充底:20像素; font:normal 24px / 30px格鲁吉亚,Times New Roman,Times,serif;颜色:#333; text-shadow:1px 2px 3px #ccc; border-bottom:1px solid #cbcbcb; } #myCanvas {background:#fff; border:1px solid #cbcbcb; } #nav {display:block;宽度:100%;文本对齐:中心; } #nav li {display:block;字体重量:粗体;行高:21px; text-shadow:1px 1px 1px #fff;宽度:100像素;高度:21px;填充:5像素;保证金:0 10px;背景:#e0e0e0; border:1px solid #ccc; -moz-边界半径:4PX; -webkit-边界半径:4PX;边界半径:4PX;向左飘浮; } #nav li a {color:#000;显示:块;文字修饰:无;宽度:100%;高度:100%; }
< div id =container> < canvas id =myCanvaswidth =400height =300>< / canvas>< / div>< input id =buttontype =submitname =buttonvalue = enteronclick =myFunction();/>
I wrote a program to move a ball when a button is clicked. It is a part of experiment for a bigegr project:
Here is the screen shot:
And here is the code:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTML5 Random Bouncing Ball</title>
<style type="text/css">
<!--
body { background-color:#ededed; font:normal 12px/18px Arial, Helvetica, sans-serif; }
h1 { display:block; width:600px; margin:20px auto; padding-bottom:20px; font:normal 24px/30px Georgia, "Times New Roman", Times, serif; color:#333; text-shadow: 1px 2px 3px #ccc; border-bottom:1px solid #cbcbcb; }
#myCanvas { background:#fff; border:1px solid #cbcbcb; }
#nav { display:block; width:100%; text-align:center; }
#nav li { display:block; font-weight:bold; line-height:21px; text-shadow:1px 1px 1px #fff; width:100px; height:21px; padding:5px; margin:0 10px; background:#e0e0e0; border:1px solid #ccc; -moz-border-radius:4px;-webkit-border-radius:4px; border-radius:4px; float:left; }
#nav li a { color:#000; display:block; text-decoration:none; width:100%; height:100%; }
-->
</style>
<script type="text/javascript">
function myFunction () {
if(document.getElementById('button').clicked == true)
{
var context;
var dx= 4;
var dy=4;
var y=150;
var x=10;
function draw(){
context= myCanvas.getContext('2d');
context.clearRect(0,0,400,400);
context.beginPath();
context.fillStyle="#000000";
context.arc(x,y,10,0,Math.PI*2,true);
context.closePath();
context.fill();
if( x<0 || x>400)
dx=-dx;
if( y<0 || y>300)
dy=-dy;
x+=dx;
y+=dy;
}
setInterval(draw,10);
}
}
</script>
</head>
<body>
<div id="container">
<canvas id="myCanvas" width="400" height="300"></canvas>
</div>
<input id="button" type="submit" name="button" value="enter" onclick="myFunction();"/>
</body>
</html>
The problem is: When I click that button, the ball won't be animated or moved. Where am I wrong?
解决方案
The code on line 19 returns undefined
document.getElementById('button').clicked == true
remove that if statement and it works
I made a jsbin: http://jsbin.com/sozicuruja/1/
function myFunction () {
var context;
var dx= 4;
var dy=4;
var y=150;
var x=10;
function draw(){
context= myCanvas.getContext('2d');
context.clearRect(0,0,400,400);
context.beginPath();
context.fillStyle="#000000";
context.arc(x,y,10,0,Math.PI*2,true);
context.closePath();
context.fill();
if( x<0 || x>400)
dx=-dx;
if( y<0 || y>300)
dy=-dy;
x+=dx;
y+=dy;
}
setInterval(draw,10);
}
body { background-color:#ededed; font:normal 12px/18px Arial, Helvetica, sans-serif; }
h1 { display:block; width:600px; margin:20px auto; padding-bottom:20px; font:normal 24px/30px Georgia, "Times New Roman", Times, serif; color:#333; text-shadow: 1px 2px 3px #ccc; border-bottom:1px solid #cbcbcb; }
#myCanvas { background:#fff; border:1px solid #cbcbcb; }
#nav { display:block; width:100%; text-align:center; }
#nav li { display:block; font-weight:bold; line-height:21px; text-shadow:1px 1px 1px #fff; width:100px; height:21px; padding:5px; margin:0 10px; background:#e0e0e0; border:1px solid #ccc; -moz-border-radius:4px;-webkit-border-radius:4px; border-radius:4px; float:left; }
#nav li a { color:#000; display:block; text-decoration:none; width:100%; height:100%; }
<div id="container">
<canvas id="myCanvas" width="400" height="300"></canvas>
</div>
<input id="button" type="submit" name="button" value="enter" onclick="myFunction();"/>
这篇关于当点击一个按钮时使球弹跳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文