
Facundo German Torreyra
PreguntaHola compañeros, les comparto mi codigo que todavia no funciona para que se mueva el cerdo. El tema es que en la consola tampoco me tira error.
Si alguien me puede ayudar a encontrar el error se lo agradezco.
Muchas gracias!!
var vp = document.getElementById(“villaplatzi”);
var papel = vp.getContext(“2d”);
var x = 1;
var y = 1;
var teclas = {
UP: 38,
DOWN: 40,
LEFT: 37,
RIGHT: 39
};
var fondo = {
url: “tile.png”,
cargaOK: false
}
var vaca = {
url: “vaca.png”,
cargaOK: false
}
var cerdo = {
url: “cerdo.png”,
cargaOK: false
}
var pollo = {
url: “pollo.png”,
cargaOK: false
}
var cantidad = aleatorio(5, 25);
fondo.imagen = new Image();
fondo.imagen.src = fondo.url;
fondo.imagen.addEventListener(“load”, cargarFondo);
vaca.imagen = new Image();
vaca.imagen.src = vaca.url;
vaca.imagen.addEventListener(“load”, cargarVacas);
cerdo.imagen = new Image();
cerdo.imagen.src = cerdo.url;
cerdo.imagen.addEventListener(“load”, cargarCerdos);
pollo.imagen = new Image();
pollo.imagen.src = pollo.url;
pollo.imagen.addEventListener(“load”, cargarPollos);
function cargarFondo()
{
fondo.cargaOK = true;
dibujar();
}
function cargarVacas()
{
vaca.cargaOK = true;
dibujar();
}
function cargarCerdos()
{
cerdo.cargaOK = true;
dibujar();
}
function cargarPollos()
{
pollo.cargaOK = true;
dibujar();
}
function dibujar()
{
if(fondo.cargaOK)
{
papel.drawImage(fondo.imagen, 0, 0);
}
if (vaca.cargaOK)
{
for(var v=0; v < cantidad; v++)
{
var x = aleatorio(0, 6);
var y = aleatorio(0, 6);
var x = x * 70;
var y = y * 70;
papel.drawImage(vaca.imagen, x, y);
}
}
if (pollo.cargaOK)
{
for(var v=0; v < cantidad; v++)
{
var x = aleatorio(0, 8);
var y = aleatorio(0, 8);
var x = x * 50;
var y = y * 50;
papel.drawImage(pollo.imagen, x, y);
}
}
if (cerdo.cargaOK) { for(var v=0; v < 1; v++) { var x = aleatorio(0, 5); var y = aleatorio(0, 5); var x = x * 80; var y = y * 80; papel.drawImage(cerdo.imagen, x, y); document.addEventListener("keydown", moverCerdo); function moverCerdo(color, xinicial, yinicial, xfinal, yfinal, lienzo) { } function moverCerdo (evento) { var movimiento = 1; switch (evento.keyCode) { case teclas.UP: moverCerdo(x, y, x, y - movimiento); y = y - movimiento; break; case teclas.DOWN: moverCerdo(x, y, x, y + movimiento); y = y + movimiento; break; case teclas.LEFT: moverCerdo(x, y, x - movimiento, y); x = x - movimiento; break; case teclas.RIGHT: moverCerdo(x, y, x + movimiento, y); x = x + movimiento; break; default: console.log("otra tecla"); } } } }
}
function aleatorio(min, maxi)
{
var resultado;
resultado = Math.floor(Math.random() * (maxi - min + 1)) + min;
return resultado;
}

Sergio Arturo Enriquez Nava
Hola, disculpa pero tienes que ver que el codigo que pusiste para moverCerdo, es como si fuera una linea que tiene un punto inicial y uno final xi-yi = a un punto, xf-yf es otro punto, el cerdito solo tiene dos cordenadas x - y, por lo cual el codigo de tu funcion moverCerdo no va a funcionar.Saludos