A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: associative array problem with a loop

  1. #1
    Member
    Join Date
    Oct 2004
    Location
    Valencia Spain
    Posts
    48

    associative array problem with a loop

    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.

    Code:
    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();
    }

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    The things you are putting into your bubblesArray are Objects with mc and sound properties, but you're trying to get MovieClips out of bubblesArray. When do you think they magically turn into MovieClips? The instances in the mc properties of those objects may be MovieClips, but there's no guarantee of that based on the code you posted.

    There is no associative array in the code you posted. Well, there's no such thing as an associative array at all in AS3, but you're not even using an array as a dynamic object which is what most people mean when they say associative array. Your definitionsArray is a normal array filled with anonymous Objects. Those objects "associate" property names with values, which is probably what you meant by associative.

  3. #3
    Member
    Join Date
    Oct 2004
    Location
    Valencia Spain
    Posts
    48
    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.

    Code:
    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
    		bubbleHolder = new Bubble ;
    		
    		bubbleHolder .addEventListener(MouseEvent.CLICK, onBubbleClick);
    		
    		bubbleHolder .x = 100 + i * 150;
    		bubbleHolder .y = 100;
    		
    		bubbleHolder .addChild(bubble);
    		addChild(bubbleHolder);
    		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;
    	}

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Use currentTarget rather than target. The currentTarget will be the instance you added the listener to, whereas the target is the dispatching instance, which may be (and is, in this case) a display child.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center