Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
Posts: 102
Registered: ‎01-12-2011
My Device: Blackberry Playbook
My Carrier: DiGi twitter@TylorPin

How can I shorten this code with a for loop?

Img[1].addEventListener(MouseEvent.CLICK,PopsOut1);
				Img[2].addEventListener(MouseEvent.CLICK,PopsOut2);
				Img[3].addEventListener(MouseEvent.CLICK,PopsOut3);
				Img[4].addEventListener(MouseEvent.CLICK,PopsOut4);
				Img[5].addEventListener(MouseEvent.CLICK,PopsOut5);
				Img[6].addEventListener(MouseEvent.CLICK,PopsOut6);
				Img[7].addEventListener(MouseEvent.CLICK,PopsOut7);
				Img[8].addEventListener(MouseEvent.CLICK,PopsOut8);
				Img[9].addEventListener(MouseEvent.CLICK,PopsOut9);
				Img[10].addEventListener(MouseEvent.CLICK,PopsOut10);
				Img[11].addEventListener(MouseEvent.CLICK,PopsOut11);

 I have a total of 80 pictures which is to be added, but 80 such lines would be very troublesome and messy. How can i shorten this to just one for loop?

Click "like" if my reply helps and click "Accept as Solution" if it solves your problem!

twitter @Twopinstudio
my vendor page https://appworld.blackberry.com/webstore/vendor/7126/?lang=en
Regular Contributor
Posts: 69
Registered: ‎01-18-2011
My Device: Playbook
My Carrier: None

Re: How can I shorten this code with a for loop?

[ Edited ]

Well, the first thing you could do would be:
for(var i:int=0; i< 80; i++){
Img[i].addEventListener(MouseEvent.CLICK, this["PopsOut"+i]);
}
But it would be better if you just had one Listener Function for all of the Items. When the listener function is called, you will be given an Event as a parameter which you can then use to get your currentTarget (e.currentTarget or e.target (dependes on how your structure is)).
Another thing to try would be making Img a Class of your own and add the Listener function inside your class. You could then just create 80 instances of Img in a for loop.

_______ Check out_______
my Apps:
Tilebreaker X - One of the best Tilebreaker Games available for the Playbook ;-)
Tilebreaker X Free - The free version with about 10 levels.
Developer
Posts: 102
Registered: ‎01-12-2011
My Device: Blackberry Playbook
My Carrier: DiGi twitter@TylorPin

Re: How can I shorten this code with a for loop?

longway77 for some reason your code is usable bt isn't functioning well on my pb...

for(var i:int=0; i< 80; i++){
Img[i].addEventListener(MouseEvent.CLICK,PopsOut);
}

 the workaround is create a class for it, I'll do that if the problem still exist. One more question, how can i send the i value to PopsOut function for each Img[] that is clicked?

Click "like" if my reply helps and click "Accept as Solution" if it solves your problem!

twitter @Twopinstudio
my vendor page https://appworld.blackberry.com/webstore/vendor/7126/?lang=en
Developer
Posts: 231
Registered: ‎05-03-2011
My Device: PlayBook V1 and proud of it :-)
My Carrier: Legs

Re: How can I shorten this code with a for loop?

I think you need to do some studying; Google some lessons on Action Script 3 and object oriented programming, looking at your code you are calling a function for each seperate image.

 

With a bit of study on your part, the answer to your question is easy, what you are looking for is how to pass variables to functions.

Keep the Forum productive and useful for all. Click Kudos button if I've helped you. Select Solved to help everyone.

PlayBook qualifying app - Pandoras' Lights
Blog - CoCopia.co.uk Catch me on Twitter cocopia_apps