
Pelota rebota
Publicado por jecht (9 intervenciones) el 12/05/2014 13:40:35
Buenas tardes, tengo una gran duda con un ejercicio de mi facultad, he estado buscando algun tema relacionado sin exito y he decidido buscar auxilio. El problema consiste en hacer que una pelota rebote por un cuadrado insertando el user las velocidades en coordenadas x e y. He conseguido que la pelota se mueva pero no tengo manera que hacer que esta rebote y no se ver donde está el error. Agradecería consejo, adjunto mi codigo. Muchisimas gracias por adelantado.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<!DOCTYPE HTML>
<html lang="es">
<head>
<meta charset="utf-8"/>
<title>Mover bola en el eje Z con dentro de una caja JS</title>
<style type="text/css">
#imagen{
position:relative;
display:block;
width:20px;
}
#lienzo{
width:300px;
height:300px;
border:1px solid #000;
overflow: hidden;
}
</style>
<script type="text/javascript">
var y=0;
var x=0;
var controlY=0;
var controlX=0;
function funcionar(){
this.velocidadx=document.getElementById("velx").value;
this.velocidady=document.getElementById("vely").value;
}
var intervalo=setInterval("mover()",1);
function mover(){
if(controlY==1){
y+=velocidady;
}else{
y-=velocidady;
}
if(y<=0){
controlY=1;
y=velocidady;
}else if(y>=document.getElementById("lienzo").offsetHeight-20){
controlY=0;
y=document.getElementById("lienzo").offsetHeight-20;
}
if(controlX==1){
x+=velocidadx;
}else{
x-=velocidadx;
}
if(x<=0){
controlX=1;
x=velocidadx;
}else if(x>=document.getElementById("lienzo").offsetWidth-20){
controlX=0;
x=document.getElementById("lienzo").offsetWidth-20;
}
document.getElementById("imagen").style.left=String(x)+"px";
document.getElementById("imagen").style.top=String(y)+"px";
}
function aturar(){
clearInterval(intervalo);
}
</script>
</head>
<body>
<h2></h2>
<div id="exercici">
<div id="parametros">
<h3>Velocitat de l'objecte</h3>
<p>x: <input type="text" value="" id="velx"/> px/s</p>
<p>y: <input type="text" value="" id="vely"/> px/s</p>
<input type="button" value="Iniciar" id="iniciar" onclick="funcionar();"/>
<input type="button" value="Parar" id="parar" onclick="aturar();"/>
<input type="button" value="Aplicar" id="aplicar" onclick="aplicar();"/>
</div>
<div id="lienzo">
<img src="eclipse.png" id="imagen" alt="eclipse"/>
</div>
</div>
</body>
</html>
Valora esta pregunta


0