Re: [Flashcoders] Adding Listeners in a loop
Thanks Juan... That was so useful. Cheers On Wed, Sep 17, 2008 at 6:40 PM, Juan Pablo Califano < [EMAIL PROTECTED]> wrote: > Hi, the problem is that by the time the event fires and your inline > function > is called, R1 points to the last object created in the loop. > > However, an event object is passed to the handler function. The target > property of that object cointains a reference to the object that fired it, > so you can do something like this. > > > R1.addEventListener(MouseEvent.CLICK, function(evt:Event):void { > trace(evt.target.Label) > }); > Cheers > Juan Pablo Califano > PD: A was about to give you some heads up on using inline functions but I > bet in minutes someone else will do take care of that, anyway ;) > > > 2008/9/17 Omar Fouad <[EMAIL PROTECTED]> > > > Hi, > > I got this loop that creates some MovieClips one under another. > > for (var i:uint = 0; i < data.length; i++) { > >var R1:ResultLink = new ResultLink(data[i].CompanyName, > > data[i].CompanyId); // > >R1.addEventListener(MouseEvent.CLICK, function():void > > {trace(R1.Label)}); > >BlackBox.addChild(R1); > >//x and y here > > } > > Where ResultLink is a Class that extends MovieClip and holds The Label, > and > > The Id property from a Dataset fetched from a SQLite database and > returned > > into the array 'data'. > > > > Now when I click to each one of the created MovieClips, they trace the > last > > Label and not the corresponding label for each MovieClip Clicked. At this > > point I think that the event listener is added to the same 'instace' that > > is > > overridden everytime. > > > > How can I fix this? > > > > Thanks > > > > > > -- > > Omar M. Fouad - www.omar-fouad.net > > Cellular: (+20) 1011.88.534 > > Mail: [EMAIL PROTECTED] > > > > This e-mail and any attachment is for authorised use by the intended > > recipient(s) only. It may contain proprietary material, confidential > > information and/or be subject to legal privilege. It should not be > copied, > > disclosed to, retained or used by, any other party. If you are not an > > intended recipient then please promptly delete this e-mail and any > > attachment and all copies and inform the sender. Thank you. > > ___ > > Flashcoders mailing list > > Flashcoders@chattyfig.figleaf.com > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > -- Omar M. Fouad - www.omar-fouad.net Cellular: (+20) 1011.88.534 Mail: [EMAIL PROTECTED] This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Adding Listeners in a loop
one easy way : function fn(i:uint) { var R1:ResultLink = new ResultLink(data[i].CompanyName, data[i].CompanyId); // R1.addEventListener(MouseEvent.CLICK, function():void {trace(R1.Label)}); BlackBox.addChild(R1); //x and y here } } --- On Wed, 9/17/08, Omar Fouad <[EMAIL PROTECTED]> wrote: From: Omar Fouad <[EMAIL PROTECTED]> Subject: [Flashcoders] Adding Listeners in a loop To: "Flash Coders List" Date: Wednesday, September 17, 2008, 5:47 PM Hi, I got this loop that creates some MovieClips one under another. for (var i:uint = 0; i < data.length; i++) { var R1:ResultLink = new ResultLink(data[i].CompanyName, data[i].CompanyId); // R1.addEventListener(MouseEvent.CLICK, function():void {trace(R1.Label)}); BlackBox.addChild(R1); //x and y here } Where ResultLink is a Class that extends MovieClip and holds The Label, and The Id property from a Dataset fetched from a SQLite database and returned into the array 'data'. Now when I click to each one of the created MovieClips, they trace the last Label and not the corresponding label for each MovieClip Clicked. At this point I think that the event listener is added to the same 'instace' that is overridden everytime. How can I fix this? Thanks -- Omar M. Fouad - www.omar-fouad.net Cellular: (+20) 1011.88.534 Mail: [EMAIL PROTECTED] This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Adding Listeners in a loop
Hi, the problem is that by the time the event fires and your inline function is called, R1 points to the last object created in the loop. However, an event object is passed to the handler function. The target property of that object cointains a reference to the object that fired it, so you can do something like this. R1.addEventListener(MouseEvent.CLICK, function(evt:Event):void { trace(evt.target.Label) }); Cheers Juan Pablo Califano PD: A was about to give you some heads up on using inline functions but I bet in minutes someone else will do take care of that, anyway ;) 2008/9/17 Omar Fouad <[EMAIL PROTECTED]> > Hi, > I got this loop that creates some MovieClips one under another. > for (var i:uint = 0; i < data.length; i++) { >var R1:ResultLink = new ResultLink(data[i].CompanyName, > data[i].CompanyId); // >R1.addEventListener(MouseEvent.CLICK, function():void > {trace(R1.Label)}); >BlackBox.addChild(R1); >//x and y here > } > Where ResultLink is a Class that extends MovieClip and holds The Label, and > The Id property from a Dataset fetched from a SQLite database and returned > into the array 'data'. > > Now when I click to each one of the created MovieClips, they trace the last > Label and not the corresponding label for each MovieClip Clicked. At this > point I think that the event listener is added to the same 'instace' that > is > overridden everytime. > > How can I fix this? > > Thanks > > > -- > Omar M. Fouad - www.omar-fouad.net > Cellular: (+20) 1011.88.534 > Mail: [EMAIL PROTECTED] > > This e-mail and any attachment is for authorised use by the intended > recipient(s) only. It may contain proprietary material, confidential > information and/or be subject to legal privilege. It should not be copied, > disclosed to, retained or used by, any other party. If you are not an > intended recipient then please promptly delete this e-mail and any > attachment and all copies and inform the sender. Thank you. > ___ > Flashcoders mailing list > Flashcoders@chattyfig.figleaf.com > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Adding Listeners in a loop
one easy way : function fn(i:uint) { var R1:ResultLink = new ResultLink(data[i].CompanyName, data[i].CompanyId); // R1.addEventListener(MouseEvent.CLICK, function():void {trace(R1.Label)}); BlackBox.addChild(R1); //x and y here } } for (var i:uint = 0; i < data.length; i++) { fn(i); } --- On Wed, 9/17/08, Omar Fouad <[EMAIL PROTECTED]> wrote: From: Omar Fouad <[EMAIL PROTECTED]> Subject: [Flashcoders] Adding Listeners in a loop To: "Flash Coders List" Date: Wednesday, September 17, 2008, 5:47 PM Hi, I got this loop that creates some MovieClips one under another. for (var i:uint = 0; i < data.length; i++) { var R1:ResultLink = new ResultLink(data[i].CompanyName, data[i].CompanyId); // R1.addEventListener(MouseEvent.CLICK, function():void {trace(R1.Label)}); BlackBox.addChild(R1); //x and y here } Where ResultLink is a Class that extends MovieClip and holds The Label, and The Id property from a Dataset fetched from a SQLite database and returned into the array 'data'. Now when I click to each one of the created MovieClips, they trace the last Label and not the corresponding label for each MovieClip Clicked. At this point I think that the event listener is added to the same 'instace' that is overridden everytime. How can I fix this? Thanks -- Omar M. Fouad - www.omar-fouad.net Cellular: (+20) 1011.88.534 Mail: [EMAIL PROTECTED] This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
[Flashcoders] Adding Listeners in a loop
Hi, I got this loop that creates some MovieClips one under another. for (var i:uint = 0; i < data.length; i++) { var R1:ResultLink = new ResultLink(data[i].CompanyName, data[i].CompanyId); // R1.addEventListener(MouseEvent.CLICK, function():void {trace(R1.Label)}); BlackBox.addChild(R1); //x and y here } Where ResultLink is a Class that extends MovieClip and holds The Label, and The Id property from a Dataset fetched from a SQLite database and returned into the array 'data'. Now when I click to each one of the created MovieClips, they trace the last Label and not the corresponding label for each MovieClip Clicked. At this point I think that the event listener is added to the same 'instace' that is overridden everytime. How can I fix this? Thanks -- Omar M. Fouad - www.omar-fouad.net Cellular: (+20) 1011.88.534 Mail: [EMAIL PROTECTED] This e-mail and any attachment is for authorised use by the intended recipient(s) only. It may contain proprietary material, confidential information and/or be subject to legal privilege. It should not be copied, disclosed to, retained or used by, any other party. If you are not an intended recipient then please promptly delete this e-mail and any attachment and all copies and inform the sender. Thank you. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders