Comunidad de diseño web y desarrollo en internet online

associative array problem with loop

Citar            
MensajeEscrito el 16 Jul 2011 04:43 pm
Hi. This is not html code by the way - I just used that html tag because I think it formatted my post???
I have been following tutorials but they don't seem to cover exactly what I'm doing.

I have an associative array of values called definitionsArray which I use as references to instantiate classes in a run time shared library.
I instantiate and push the object into a new array. The problem is when I try to access or do anything basically with that new array.
As they are objects they don't seem to like being in a display list for example.

Código ActionScript :

var definitionsArray:Array;
var bubblesArray:Array;
var currentBubble:*;
var correct:uint;
var incorrect:uint;



this.definitionsArray = new Array();
this.definitionsArray.push({ sound:"AppleSound",mc:"Apple" });
this.definitionsArray.push({ sound:"BananaSound",mc:"Banana" });
this.definitionsArray.push({ sound:"BreadSound",mc:"Bread" });
this.definitionsArray.push({ sound:"CakeSound",mc:"Cake"  });
this.definitionsArray.push({ sound:"WaterSound",mc:"Water" });
this.definitionsArray.push({ sound:"TomatoSound", mc:"Tomato" });
this.definitionsArray.push({ sound:"SandwichSound",mc:"Sandwich" });
this.definitionsArray.push({ sound:"PizzaSound",mc:"Pizza" });
this.definitionsArray.push({ sound:"PearSound",mc:"Pear" });
this.definitionsArray.push({ sound:"OrangeSound",mc:"Orange" });

getData("library/food.swf", definitionsArray);

function getData(libreria:String, matriz:Array)
{
    this.definitionsArray = matriz;
    var context:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);

    var loader:Loader = new Loader();
    var req:URLRequest = new URLRequest(libreria);

    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onAssetsLoaded);
    try
    {
        loader.load(req);
    }
    catch (e:Error)
    {
        trace("Asset load error: " + e);
    }
}



function onAssetsLoaded(e:Event):void
{
    this.cartelArray = new Array();
    var loader:Loader = Loader(e.target.loader);

    this.libreria = loader.content;//this isn't used

    //var mezclaMAtriz:Array = this.definitionsArray.slice();
    bubblesArray = [];
    for (var i:uint = 0; i < this.definitionsArray.length; i++)
    {
        var libraryDomain:ApplicationDomain = loader.contentLoaderInfo.applicationDomain;
        var mcClass:Class = libraryDomain.getDefinition(definitionsArray[i].mc) as Class;
        var soundClass:Class = libraryDomain.getDefinition(definitionsArray[i].sound) as Class;

        var obj:Object = new Object();
        obj.mc = new mcClass();
        obj.sound = new soundClass();

        this.bubblesArray.push(obj);
        placeBubbles();
    }
}

function placeBubbles():void
{
    var bubble:MovieClip;
    for (var i:int = 0; i < bubblesArray.length; i++)
    {
        bubble = bubblesArray[i];
        bubble.addEventListener(MouseEvent.CLICK, onBubbleClick);
        bubble.x = 100 + i * 150;
        bubble.y = 100;
        addChild(bubble);
    }
    // start game
    nextChoice();
}

Por codeBeast

5 de clabLevel



Genero:Masculino  

firefox
Citar            
MensajeEscrito el 16 Jul 2011 07:06 pm
Hi codeBeast, probably you didn't note, but this is a spanish Board, please post in spanish, otherwise use any english Board like http://www.flash-db.com/Board/

Jorge

Por solisarg

BOFH

13669 de clabLevel

4 tutoriales
5 articulos

Genero:Masculino   Bastard Operators From Hell Premio_Secretos

Argentina

firefox
Citar            
MensajeEscrito el 16 Jul 2011 09:31 pm
As they are objects they don't seem to like being in a display list for example.

Perdona - menos mal que soy bilingue - si no.
Empiezo con un array indexado que usa para proporcionar las referencias para instanciar clases de una libraria compartido.
Se instancia y uso .push para meter mcs y sonidos en un Object.
Para acceder a este Object después se ve que me lio - en el loop precisamente.

Esta linea da error porque bubble es declarado como movieclip pero bubblesArray[i] será un object.
bubble = bubblesArray[i];

Se que lo hago mal pero es que no se hacer otra cosa. Solo quiero acceder y manipular ese Object que sé que contiene mis mcs y sonidos

Por codeBeast

5 de clabLevel



Genero:Masculino  

firefox
Citar            
MensajeEscrito el 16 Jul 2011 09:38 pm
Primero: estas instanciando un MovieClip, pero seguramente en el getDefinitionByName usas la clase que tiene en la librería, que si bien hereda de MovieClip puede estar dando un error, usualmente para evitar esto se usa una interfaz.
Luego estás haciendo un addChild de una clase que supongo es un sonido, es decir no desciende de DisplayObject, con lo cual también da error.

Jorge

Por solisarg

BOFH

13669 de clabLevel

4 tutoriales
5 articulos

Genero:Masculino   Bastard Operators From Hell Premio_Secretos

Argentina

firefox
Citar            
MensajeEscrito el 17 Jul 2011 05:41 pm
Hola Jorge que tal?
El problema es que tengo un object pero lo trataba como un movieclip. Lo he solucionado accediendo al mc dentro del object.
Luego he creado un contenedor mc para contener a mis mcs - banana, apple etc...
al cual he añadido addEventListener PERO solo me funciona cuando le doy al banana etc... y tiene que funcionar cuando yo le de al contenedor osea su padre mc y no al child mc.

Por codeBeast

5 de clabLevel



Genero:Masculino  

firefox
Citar            
MensajeEscrito el 17 Jul 2011 05:42 pm
Perdona - se me olvidó meter el codigo

Hi.
I have taken on board all the advice and I have read up on how to access the object I created and now it's working fine. I added a bubbleHolder to put my mcs inside which also has the addEventListener attached. There will be various bubbles floating around with a banana, apple etc... in each.
I hear the sound eg: Apple so I have to click the bubbleHolder with the apple inside.
Unforunately when I click on the bubbleHolder I traced
CURRENT TARGET=[object MovieClip]
CURRENT BUBBLE=[object Cake]
So obviously they are not the same.

Only when I click the actual apple do I get the correct result
CURRENT TARGET=[object Apple]
CURRENT BUBBLE=[object Apple]

BUT it needs to be the bubbleHolder I click NOT its child.

Código :

function onAssetsLoaded(e:Event):void
{
   var loader:Loader = Loader(e.target.loader);

   bubblesArray = [];
   for (var i:uint = 0; i < this.definitionsArray.length; i++)
   {
      var libraryDomain:ApplicationDomain = loader.contentLoaderInfo.applicationDomain;
      var mcClass:Class = libraryDomain.getDefinition(definitionsArray[i].mc) as Class;
      var soundClass:Class = libraryDomain.getDefinition(definitionsArray[i].sound) as Class;

      var obj:Object = new Object();
      obj.mc = new mcClass();
      obj.sound = new soundClass();

      this.bubblesArray.push(obj);

   }
   placeBubbles();
}

function placeBubbles():void
{

   var i:int = 0;//remove if use for loop

   while (i<bubblesArray.length)

   {
      trace( bubblesArray[i].mc);
      
      bubble = bubblesArray[i].mc
      [B]bubbleHolder = new Bubble ;[/B]
      
      [B]bubbleHolder[/B] .addEventListener(MouseEvent.CLICK, onBubbleClick);
      
      [B]bubbleHolder[/B] .x = 100 + i * 150;
      [B]bubbleHolder[/B] .y = 100;
      
      [B]bubbleHolder[/B] .addChild(bubble);
      addChild([B]bubbleHolder[/B]);
      i++;//remove if you use for loop
   }

   nextChoice();
}


 function nextChoice():void
      {

         currentBubble = bubblesArray[int(Math.random() * bubblesArray.length)];

         trace(currentBubble.sound);
         currentBubble.sound.play();
      }
      
function onBubbleClick(e:MouseEvent):void
{
   trace("CURRENT TARGET=" + e.target);
   trace("CURRENT BUBBLE="+currentBubble.mc);
   //if ((currentBubble.mc && currentBubble.mc == e.currentTarget))
   if ((currentBubble.mc && currentBubble.mc == e.target))
   {
      trace("CORRECT!");
      if (correct == 2)
      {
         //endGame();

      }
      correct = correct + 1;
      nextChoice();
   }
   else
   {
      trace("WRONG!");
      incorrect = incorrect + 1;
   }

Por codeBeast

5 de clabLevel



Genero:Masculino  

firefox
Citar            
MensajeEscrito el 17 Jul 2011 11:22 pm
El contenedor ahora captura todos los eventos, no se cual es tu objetivo ... ¿instanciar botones clickables? Cuentanos que quieres hacer sin tecnicismos, el código es consecuencia, no causa

Jorge

PD: ahorrate el inglés, el foro es en castellano

Por solisarg

BOFH

13669 de clabLevel

4 tutoriales
5 articulos

Genero:Masculino   Bastard Operators From Hell Premio_Secretos

Argentina

firefox
Citar            
MensajeEscrito el 17 Jul 2011 11:28 pm
Perdoname por el ingles - estaba posteando en muchos foros.
Bueno te enseño el juego que estoy copiando y lo verás
www.cambridgekids.es/virtualWorld
Ingresas sin contraseñas ni nada. Clickea delante del gato para que ande - va un poco lento pero no tienes que andar mucho. EL primer icono de arriba "Whack it" no - el siguiente llamado bubbles - aprieta y ese es el juego.
Hay imagenes como platano, manzana etc... dentro de unas burbujas - se oye un nombre y tienes que apretar la burbuja correspondiente.
Pues quiero saber el codigo de meter a mis imagenes/swfs dentro de esa burbuja y que sea el target de mis clicks.
Gracias

Por codeBeast

5 de clabLevel



Genero:Masculino  

firefox

 

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