Foros del Web » Programando para Internet » Javascript »

Clases en javascript

Estas en el tema de Clases en javascript en el foro de Javascript en Foros del Web. Hola estoy haciendo un programita de una pelota que rebota dentro de un div. El problema viene cuando intento añadir otra pelota y entoces solo ...
  #1 (permalink)  
Antiguo 01/12/2015, 13:47
 
Fecha de Ingreso: septiembre-2012
Mensajes: 4
Antigüedad: 11 años, 7 meses
Puntos: 0
Clases en javascript

Hola estoy haciendo un programita de una pelota que rebota dentro de un div.
El problema viene cuando intento añadir otra pelota y entoces solo se me mueve una de ellas.
Esta es la pagina principal:
Código:
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Practica 24</title>
	<script type="text/javascript" src="clasePelota.js"></script>
</head>
<body>
	<div style="width:600px;height:600px;border:1px orange solid;" id="campo">
		
	</div>
	<div style="background-color:gray;width:600px; height:50px;" id="info">
		
	</div>
	<script type="text/javascript">
		var pelota1 = new ClasePelota(3,2);
		var pelota2 = new ClasePelota(2,2);
		var intervalId = setInterval(function(){pelota1.mover();pelota2.mover()}, 100);
	</script>
</body>
</html>
Y esta es mi clase js:

Código:
function ClasePelota(ix,iy){
	this.x = 0;
	this.y = 0;
	this.minX = 0;
	this.minY = 0;
	this.maxX = 600;
	this.maxY = 600;
	this.ancho = 64
	this.alto=64;
	this.incrementoX = ix;
	this.incrementoY = iy;
	this.contrarioX = false;
	this.contrarioY = false;
	campo.innerHTML+="<img src='img/pelota.png' id='imgPelota"+ ix + iy +"' style='position:absolute;top:0px;left:0px;width:64px;height:64px;'>";
	this.pelota = document.getElementById("imgPelota" + ix + iy);
}
ClasePelota.prototype.mover = function(){
	if (!this.contrarioX){
		this.pelota.style.top = this.x + this.incrementoX + "px";
		this.x +=this.incrementoX;
	}else{
		this.pelota.style.top = this.x - this.incrementoX + "px";
		this.x -=this.incrementoX;
	}
	if (!this.contrarioY){
		this.pelota.style.left = this.y + this.incrementoY + "px";
		this.y +=this.incrementoY;
	}else{
		this.pelota.style.left = this.y - this.incrementoY + "px";
		this.y -=this.incrementoY;
	}
	if (this.x + this.ancho >= this.maxX){
		this.contrarioX= true;
	}
	if (this.x <= this.minX){
		this.contrarioX = false;
	}
	if (this.y + this.ancho >= this.maxY){
		this.contrarioY= true;
	}
	if (this.y <= this.minY){
		this.contrarioY = false;
	}
	info.innerHTML="Coordenadas X(" + this.x + ") - Y(" + this.y + ")";
}
  #2 (permalink)  
Antiguo 01/12/2015, 18:26
Avatar de hackjose  
Fecha de Ingreso: abril-2010
Ubicación: Edo Mexico
Mensajes: 1.178
Antigüedad: 14 años
Puntos: 131
Respuesta: Clases en javascript

No veo porque solo se inserta uno, podrias intentar con insertadjacenthtml

saludos
  #3 (permalink)  
Antiguo 01/12/2015, 20:03
 
Fecha de Ingreso: septiembre-2012
Mensajes: 4
Antigüedad: 11 años, 7 meses
Puntos: 0
Respuesta: Clases en javascript

La cuestion es que si que me inserta las dos pero una se mueve y la otra no. Hago un monton de alerts y parace que todo esta bien pero en una imagen se me actualiza el top y el left y en la otra no.

Etiquetas: clases, html, js
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:29.