//Minimal
function dibujarAsteroide(x, y){
var fondo = document.getElementById("fondo");
var ctx = fondo.getContext("2d");
var asteroide = document.getElementById("asteroide");
y = 0;
x = Math.floor((Math.random() * 600) + 1);
ctx.drawImage(asteroide, x, y);
}
function moverAsteroide(){
setInterval(dibujarAsteroide, 500);
var datosAsteroide = document.getElementById("asteroide");
datosAsteroide.style.top = 700 + "px";
}
//Verificable
<body>
<section>
<canvas id="fondo" width = "600" height = "600"></canvas>
<div>
<img src="img/nave.png" id="nave"/>
<img src="img/asceroide.png" id="asteroide"/>
</div>
</section>
</body>
<script>
fondo=document.getElementById("fondo");
fondo.onclick=moverNave;
function moverNave(evento)
{
var nave=document.getElementById("nave");
x=evento.clientX;
y=evento.clientY;
if (x>=700 || y>=500){
alert("fuera del espacio");
}
else
{
nave.style.left=x+"px";
nave.style.top=y+"px";
}
}
function dibujarAsteroide(x, y){
var fondo = document.getElementById("fondo");
var ctx = fondo.getContext("2d");
var asteroide = document.getElementById("asteroide");
y = 0;
x = Math.floor((Math.random() * 600) + 1);
ctx.drawImage(asteroide, x, y);
}
function moverAsteroide(){
setInterval(dibujarAsteroide, 500);
var datosAsteroide = document.getElementById("asteroide");
datosAsteroide.style.top = 700 + "px";
}
</script>
/*Complete
This is the script*/
<!DOCTYPE html>
<html lang = "es">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="css/estilos.css">
<title>Nave</title>
</head>
<body>
<section>
<canvas id="fondo" width = "600" height = "600"></canvas>
<div>
<img src="img/nave.png" id="nave"/>
<img src="img/asceroide.png" id="asteroide"/>
</div>
</section>
</body>
<script>
fondo=document.getElementById("fondo");
fondo.onclick=moverNave;
function moverNave(evento)
{
var nave=document.getElementById("nave");
x=evento.clientX;
y=evento.clientY;
if (x>=700 || y>=500){
alert("fuera del espacio");
}
else
{
nave.style.left=x+"px";
nave.style.top=y+"px";
}
}
function dibujarAsteroide(x, y){
var fondo = document.getElementById("fondo");
var ctx = fondo.getContext("2d");
var asteroide = document.getElementById("asteroide");
y = 0;
x = Math.floor((Math.random() * 600) + 1);
ctx.drawImage(asteroide, x, y);
}
function moverAsteroide(){
setInterval(dibujarAsteroide, 500);
var datosAsteroide = document.getElementById("asteroide");
datosAsteroide.style.top = 700 + "px";
}
</script>
</html>
//This the css code
#fondo
{
width: 600px;
height: 600px;
background-image: url("../img/space.jpg");
background-repeat: no-repeat;
}
#nave{
position: absolute;
left:375px;
top: 300px;
transition-duration: 1s;
}
#asteroide{
width:20px;
height: 20px;
transition-duration: 5s;
}
The first function is supposed to draw the asteroid in a random location on top of the canvas. The second one has to move it simulating a fall, I'm using transition property in my css to make the duration of the fall 3 seconds.
The problem
The asteroids didn't appeare randomly, it was only one and far out of the canvas. Is there anything wrong with the drawing function?