Comunidad de diseño web y desarrollo en internet online

Resumir código

Citar            
MensajeEscrito el 07 Mar 2011 07:00 pm
Hola gente, estoy haciendo un juego en el que dentro de un mc (targets_mc) multiplico otro (llamado "target1_mc") 12 veces hasta llegar a "target12_mc". Estos 12 MCs van cayendo de forma aleatoria y deben ser capturados. Hasta ahi todo ok... En el mc que uso de "capturador" tengo el siguiente código:

Código ActionScript :

onClipEvent(enterFrame){
if (this.hitTest(_root.targets_mc.target1_mc)){
            _root.targets_mc.target1_mc._y= -random (500)
            _root.targets_mc.target1_mc._x= _root.posx[random(14)]
            _root.targets_mc.target1_mc.gotoAndStop(random(7));
            _root.targets_mc.target1_mc._rotation = random(180)
            _root.targets_mc.target1_mc.speed=random(10)+4;   
         }
         
         iif (this.hitTest(_root.targets_mc.target2_mc)){
            _root.puntos+= (_root.targets_mc.target2_mc._currentframe*5)
            _root.targets_mc.target2_mc._y= -random (500)
            _root.targets_mc.target2_mc._x= _root.posx[random(14)]
            _root.targets_mc.target2_mc.gotoAndStop(random(7));
            _root.targets_mc.target2_mc._rotation = random(180)
            _root.targets_mc.target2_mc.speed=random(10)+4;
            _root.puntos_txt.text=_root.puntos
            
         }
         if (this.hitTest(_root.targets_mc.target3_mc)){
            _root.puntos+= (_root.target3_mc._currentframe*5)
            _root.targets_mc.target3_mc._y= -random (500)
            _root.targets_mc.target3_mc._x= _root.posx[random(14)]
            _root.targets_mc.target3_mc.gotoAndStop(random(7));
....
....
.... asi hasta target12_mc


Como notarán, el codigo es siempre el mismo, sólo cambia el indice del mc. Lo que quiere es una función que se aplique a los 12 movieclips asi no tengo 12 códigos casi iguales. Intenté mediante un for algo como esto:

Código ActionScript :

for (i=1;i<=12;i++){
                                if (this.hitTest(_root.targets_mc.target"+i+"_mc")){
            _root.targets_mc."target"+i+"_mc"._y= -random (500)
            _root.targets_mc."target"+i+"_mc"_x= _root.posx[random(14)]
            _root.targets_mc."target"+i+"_mc".gotoAndStop(random(7));
....
....
....etc


Pero evidentemente no me toma la parte de "target"+i+"_mc" como un mc y por lo tanto no le aplica lo que sige...
También intenté sacar los nombres de los MCs de un array, con el mismo resultado.

¿¿Como se puede hacer??

Por alexisevega

20 de clabLevel



Genero:Masculino  

firefox
Citar            
MensajeEscrito el 09 Mar 2011 02:51 pm
Para referencias dinámicas tienes que usar notación de array, sería algo así:

_root.targets_mc.["target"+i+"_mc"]

Jorge

Por solisarg

BOFH

13669 de clabLevel

4 tutoriales
5 articulos

Genero:Masculino   Bastard Operators From Hell Premio_Secretos

Argentina

firefox
Citar            
MensajeEscrito el 09 Mar 2011 03:15 pm

solisarg escribió:

Para referencias dinámicas tienes que usar notación de array, sería algo así:

_root.targets_mc.["target"+i+"_mc"]

Jorge


Creo que va sin el puntito:

Código ActionScript :

_root.targets_mc["target" + i + "_mc"]

Por Acreonte

Claber

1543 de clabLevel

5 tutoriales
6 articulos

Genero:Masculino  

Digital Scientist

firefox
Citar            
MensajeEscrito el 09 Mar 2011 03:20 pm
Gracias a los 2! Funcionó perfecto, es como dice Acreonte, sin el punto.

De nuevo, muchas gracias, me sirve mucho! ;)

Por alexisevega

20 de clabLevel



Genero:Masculino  

firefox

 

Cristalab BabyBlue v4 + V4 © 2011 Cristalab
Powered by ClabEngines v4, HTML5, love and ponies.