Foros del Web » Programando para Internet » Javascript »

script no funciona en firefox y si en IE

Estas en el tema de script no funciona en firefox y si en IE en el foro de Javascript en Foros del Web. Hola a todos pues como bien dice el titulo este script que he realizado no me funciona en firefox pero si en IE, tengo que ...
  #1 (permalink)  
Antiguo 14/05/2011, 05:25
 
Fecha de Ingreso: marzo-2011
Mensajes: 37
Antigüedad: 13 años, 1 mes
Puntos: 0
script no funciona en firefox y si en IE

Hola a todos pues como bien dice el titulo este script que he realizado no me funciona en firefox pero si en IE, tengo que hacer que funcione en los dos.
El script consiste en algo similar al juego del arkanoid, un pelota que rebota por las paredes y cuando cae tiene que rebotar en una barra que se desplaza con las teclas "Z" y "X", pues resulta que no se me mueve la barra y tampoco la pelota, segun se carga pone terminado en la barra de status.
Os pongo el codigo a ver si me podeis ayudar un poco ya que estoy perdido y tengo que tener este ejercicio entregado el lunes.

<html>

<head>

<title>Arkanaoid</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<style tpe="text/css">

#fondo {width:550; height:385; background-image: url(foto1.gif); margin-left:-8px; margin-top:-15px;}
#imagen {position:absolute;}

</style>

<script language="JavaScript">


// Movimiento Balon

balon = new Image();
balon.src="balon.gif";

barram = new Image();
barram.src="barra1.gif";

var x=0; //Coordenadas del balon
var y=0; //Coordenadas del balon

var direccionv=0;
var direccionh=0;

var barra;



var ie = (document.all)? true:false;



function moverbalon()
{
if (ie)
{
barra=muevebarra.style
}

barra.xpos=parseInt(barra.left);
barra.ypos=parseInt(barra.top);
barra.activeleft=false;
barra.activeright=false;
barra.activeup=false;
barra.activedown=false;
document.onkeydown=pulsartecla;
document.onkeyup=soltartecla;

if(x>=parseInt(document.getElementById("fondo").st yle.height)-balon.height)
{
direccionv=1;
}

if(y>parseInt(document.getElementById("fondo").sty le.width)-balon.width)
{
direccionh=1;
}

if(x<=0)
{
direccionv=0;
}

if(direccionv==0)
{
x+=5;
}

if(direccionv==1)
{
x-=5;
}

if(y<=0)
{
direccionh=0;
}

if(direccionh==0)
{
y+=5;
}

if(direccionh==1)
{
y-=5;
}

document.getElementById("imagen").style.top=x+"px" ;
document.getElementById("imagen").style.left=y+"px ";
setTimeout("moverbalon()",30);
devolverbalon()

}

// Movimiento Barra

function devolverbalon()
{

if(y>=barra.xpos && y<=(barra.xpos+100))
{
if(x>=barra.ypos-30)
{
direccionv=1;
}
}
else
{
if(x>barra.ypos)
{
alert("juego terminado")
x=0;
y=0;
}
}

}

function pulsartecla()
{

if (ie)
{
var tecla=event.keyCode;
}

if ((tecla==90) && !barra.activeleft)
{
barra.activeleft=true;
barra.activeright=false;
moverizquierda()
}

if ((tecla==88) && !barra.activeright)
{
barra.activeright=true;
barra.activeleft=false;
moverderecha()
}

}

function soltartecla()
{

if (ie)
{
var tecla=event.keyCode;
}

if (tecla==90)
{
barra.activeleft=false
}

if (tecla==88)
{
barra.activeright=false
}
}


function moverizquierda()
{
if (barra.xpos==5)
{
barra.activeleft=false;
}
else
{
if (barra.activeleft)
{
barra.xpos-=5
barra.left=barra.xpos
setTimeout("moverizquierda()",10)
}
}
}


function moverderecha()
{
if (barra.xpos==730)
{
barra.activeright=false;
}
else
{
if (barra.activeright)
{
barra.xpos+=5
barra.left=barra.xpos
setTimeout("moverderecha()",10)
}
}
}

</script>

</head>

<body onload="moverbalon()">

<div id="fondo" style="width: 800px; height: 850px;">
<img id="imagen" src="balon.gif">
<div id="muevebarra" style="position:absolute; left:350; top:750; width:70;">
<img src="barra1.gif" width=70 height=20>
</div>

</body>

</html>


Un saludo y gracias.
  #2 (permalink)  
Antiguo 14/05/2011, 07:32
Avatar de Panino5001
Me alejo de Omelas
 
Fecha de Ingreso: mayo-2004
Ubicación: -34.637167,-58.462984
Mensajes: 5.148
Antigüedad: 20 años
Puntos: 834
Respuesta: script no funciona en firefox y si en IE

Hay varios errores, como una etiqueta sin cerrar. Pero el principal error es que estás referenciando mal. Por ejemplo, barra=muevebarra.style. En explorer funciona, pero en otros navegadores hay que usar document.getElementById('muevebarra').style
Otro problema es la captura de teclas. Esta versión con unos pocos cambios funciona:
Código PHP:
<html>

<
head>

<
title>Arkanaoid</title>
<
meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<
style tpe="text/css">

#fondo {width:550; height:385; background-image: url(foto1.gif); margin-left:-8px; margin-top:-15px;}
#imagen {position:absolute;}

</style>

<
script language="JavaScript">


// Movimiento Balon
var fin=0;
balon = new Image();
balon.src="balon.gif";

barram = new Image();
barram.src="barra1.gif";

var 
x=0//Coordenadas del balon
var y=0//Coordenadas del balon

var direccionv=0;
var 
direccionh=0;




var 
ie = (document.all)? true:false;



function 
moverbalon()
{
    if(
fin)return;

barra=document.getElementById('muevebarra').style;

barra.xpos=parseInt(barra.left);
barra.ypos=parseInt(barra.top);
barra.activeleft=false;
barra.activeright=false;
barra.activeup=false;
barra.activedown=false;


if(
x>=parseInt(document.getElementById("fondo").style.height)-balon.height)
{
direccionv=1;
}

if(
y>parseInt(document.getElementById("fondo").style.width)-balon.width)
{
direccionh=1;
}

if(
x<=0)
{
direccionv=0;
}

if(
direccionv==0)
{
x+=5;
}

if(
direccionv==1)
{
x-=5;
}

if(
y<=0)
{
direccionh=0;
}

if(
direccionh==0)
{
y+=5;
}

if(
direccionh==1)
{
y-=5;
}

document.getElementById("imagen").style.top=x+"px" ;
document.getElementById("imagen").style.left=y+"px ";
setTimeout("moverbalon()",30);
devolverbalon()

}

// Movimiento Barra

function devolverbalon()
{
if(
fin)return;
if(
y>=barra.xpos && y<=(barra.xpos+100))
{
if(
x>=barra.ypos-30)
{
direccionv=1;
}
}
else
{
if(
x>barra.ypos)
{
fin=1;    
alert("juego terminado")
x=0;
y=0;
}
}

}

function 
pulsartecla(event
{

var 
tecla=event.keyCode || event.which;

if ((
tecla==90) && !barra.activeleft

barra.activeleft=true;
barra.activeright=false;
moverizquierda()
}

if ((
tecla==88) && !barra.activeright

barra.activeright=true;
barra.activeleft=false;
moverderecha()
}

}

function 
soltartecla(event
{

var 
tecla=event.keyCode || event.which;

if (
tecla==90)
{
barra.activeleft=false
}

if (
tecla==88)
{
barra.activeright=false
}
}


function 
moverizquierda() 
{
if (
barra.xpos==5)
{
barra.activeleft=false;
}
else
{
if (
barra.activeleft
{
barra.xpos-=5
barra
.left=barra.xpos
setTimeout
("moverizquierda()",10)
}
}
}


function 
moverderecha() 
{
if (
barra.xpos==730)
{
barra.activeright=false;
}
else
{
if (
barra.activeright
{
barra.xpos+=5
barra
.left=barra.xpos
setTimeout
("moverderecha()",10)
}
}
}
onload=function(){
    
moverbalon();
    
self.focus();
    
document.onkeydown=pulsartecla;
    
document.onkeyup=soltartecla;
}
</script>

</head>

<body>

<div id="fondo" style="width: 800px; height: 850px;">
<img style="border:1px solid red" id="imagen" src="balon.gif">
<div id="muevebarra" style="position:absolute; left:350; top:750; width:70;">
<img style="border:1px solid red" src="barra1.gif" width=70 height=20>
</div>
</div>
</body>

</html> 
  #3 (permalink)  
Antiguo 14/05/2011, 08:37
 
Fecha de Ingreso: marzo-2011
Mensajes: 37
Antigüedad: 13 años, 1 mes
Puntos: 0
Respuesta: script no funciona en firefox y si en IE

Muchas gracias por contestar me has sido de gran ayuda, pero tengo otro problema al utilizarlo en IE me da un error al mover la barra, este pone que "keyCode es nulo o no es un objeto" y el error lo situa en "var tecla=event.keyCode"; al poner "function soltartecla(event)" no funciona en IE pero si en firefox y si le pongo "function soltartecla()" no funciona en firefox pero si en IE. ¿Como puedo solucionarlo? Un saludo y gracias.
  #4 (permalink)  
Antiguo 14/05/2011, 09:14
 
Fecha de Ingreso: marzo-2011
Mensajes: 37
Antigüedad: 13 años, 1 mes
Puntos: 0
Respuesta: script no funciona en firefox y si en IE

Ya lo solucione muchas gracias por responder. Un saludo.

Etiquetas: firefox
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:48.