PDA

View Full Version : cannot call button function within another event function


derekframpton
07-24-2008, 05:02 AM
Hopefully someone can help me out with this one. I'm importing XML in order to draw images into an animated scrollbar, and would like to nest a button function within the function urlLoaded as listed below:

import caurina.transitions.*;

var urlCall:URLRequest = new URLRequest("pics/hotel_dreams/hotel_dreams.xml");
var urlLoader:URLLoader = new URLLoader();
var xml:XML;
var xmlList:XMLList;
urlLoader.load(urlCall);
urlLoader.addEventListener(Event.COMPLETE,urlLoade d);

var arrayThumb:Array = new Array();
var photoContainer:Sprite = new Sprite();
addChild(photoContainer);
photoContainer.mask=thumb_holder;


function urlLoaded(event:Event):void {


xml = XML(event.target.data);
xmlList = xml.children();
trace(xmlList.length())
for (var i:int=0; i<xmlList.length(); i++) {
var thumb:Thumbnail = new Thumbnail(xmlList[i].url);
arrayThumb.push(thumb);
arrayThumb[i].y = 67.5;
arrayThumb[i].x = xml.children().x_position[i];
photoContainer.addChild(thumb);
arrayThumb[i].gallery = xml.children().gallery[i];

trace(arrayThumb[i].gallery);


}


}

my hope is to create a button that looks something similar to the following in order to execute an if then statement within the function controlling the visibility of images from the XML based on their gallery property value.

btn_gallery_2.addEventListener(MouseEvent.CLICK, open_gallery_2);
function open_gallery_2(event:MouseEvent) {

if (arrayThumb[i].gallery == 2) {
arrayThumb[i].visible = true
}

}

I have been trying to nest within the urlLoaded function to no avail. By running trace statements I can see that [i] is inaccessible when nested within another function within urlLoaded and when completely outside of the urlLoaded function.

The Thumbnail class referenced is in an .as file that looks like this:

package {
import flash.display.Sprite;
import flash.display.Loader;
import flash.net.URLRequest;

public class Thumbnail extends Sprite {

private var url:String;
private var loader:Loader;
private var urlRequest:URLRequest;
public var gallery:Number

function Thumbnail(source:String):void {
gallery = 0
url=source;
drawLoader();

}

{
private function drawLoader():void {

urlRequest=new URLRequest(url);
loader=new Loader ;
loader.mouseEnabled=false;
loader.load(urlRequest);
loader.x=-0;
loader.y=-68
;
addChild(loader);

}}}}

I have the suspicion that I am missing something very basic. If anyone out there might be able to give me some insight, then I would be appreciative.

thanks very much

mariposafuriosa
07-25-2008, 02:35 AM
I think I found the answer!!!
here it is:
is the sepparate .as I declared a public variable:

public var named:String;

then in my fla:

thumb = new Thumbnail(xmlList[i].url);
picName = xmlList[i].big_url;
picTitle = String(xmlList[i].attribute("name"));
arrayName.push(picName);
arrayThumb.push(thumb);
arrayThumb[i].addEventListener(MouseEvent.CLICK,onClick);
arrayTitulo.push(picTitle);
arrayCarteles.push(picTitle);
arrayThumb[i].y = 67.5;
arrayThumb[i].x = i*186+90;
arrayThumb[i].name = arrayName[i];
arrayThumb[i].named = arrayTitulo[i];

in the last line you can see the variable populated by an array.
that way you can pass variables in order to use the onclick listener

I hope this helps!!
Indira