i is a local var of the tracePath() method; it can't be accessed outside
this method, and it doesn't even exist after tracePath() has finished
executing.

 

What you are trying to do is dynamically create N instances of a
UIComponent, and when you click one of them, you want to know whether it
is the 1st, 2nd, ... one that you created. (Actually, I suspect that
what you really want to get when you click is the 'results' data,
because otherwise I don't know why you calculated it.)

 

The best way to do this is to write a trivial subclass of UIComponent
which can store 'i' and/or 'results' as an instance var of each
UIComponent that you create:

 

public class MyComponent extends UIComponent

{

    public class MyComponent()

    {

        super();

    }

 

    public var i:int;

 

    public var results:XMLList;

}

 

Inside tracePath(), create instances of MyComponent and set 'i' and/or
'results' on each one:

 

var myUIComponent:MyComponent = new MyComponent();

myUIComponent.i = i;

myUIComponent.results = results;

myUIComponent.graphics...

 

Then inside mouseClickHandler() you can access 'i' and 'results' as
event.currentTarget.i and event.currentTarget.results. You can't access
them as this.i and this.results because 'this' is always whatever
component or application your <mx:Script> is inside of; it isn't the
object that dispatched or listened to the MouseEvent.

 

BTW, you could probably accomplish of all this more easily by using the
<mx:Repeater> tag, but that's another topic.

 

- Gordon

 

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Jeremy Tooley
Sent: Monday, November 27, 2006 7:46 PM
To: [email protected]
Subject: [flexcoders] accessing variables through events

 

Quick question regarding accessing variables.

I have the following code

public function tracePath():void {

for (var i:int = 0; i < photoFeed.menu.submenu.length(); i++) {
trace([EMAIL PROTECTED])
var results:XMLList = 
[EMAIL PROTECTED];
trace(results);
trace (i);
var myUIComponent:UIComponent = new UIComponent();

myUIComponent.graphics.beginFill(0xFFCC00);
myUIComponent.graphics.drawCircle(60*i, 30, 30);
myUIComponent.buttonMode = true;
myUIComponent.useHandCursor = true;
myUIComponent.addEventListener(MouseEvent.CLICK, 
mouseClickHandler);

this.addChild(myUIComponent);

}
trace(photoFeed.menu.submenu.length() +"hello");
}

and I access using this function
public function mouseClickHandler(event:MouseEvent):void{
trace(this.i);
}

My question is... how do I access the "i" variable from within the 
mouseClickHandler() function
so I could find out certain properties. The above code does not seem to 
find the "i" based on the dynamically created component.

I really want to be able to find out the list collection based on click.

Thanks
Jeremy

 

Reply via email to