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
Highlighted
Developer
Posts: 660
Registered: ‎10-08-2009
My Device: Bold 9900
My Carrier: Telus
Accepted Solution

Array of sprite individual addEventListener

Hi, I have run into a problem either its too obvious or not many people run into because I can't seem to find a solution online!

 

The problem is I have array of sprite - 10 of them - and I need to register them all to a click event that changes color of the sprite from black to white.

 

However, I don't want to write:

 

 

a[1].addEventListener(MouseEvent.CLICK, change1);
a[2].addEventListener(MouseEvent.CLICK, change2);
a[3].addEventListener(MouseEvent.CLICK, change3);
... etc

 

 

Since they're all supposed to be doing the same thing (changing color from black to white), is there a way for me to use a single "change" click event and at the same time know which click event the request is coming from i.e. which "a" the user clicked?

 

Please help me!!

----------------------------------------------------------
Twitter: @gyubok
Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: Array of sprite individual addEventListener

Sure there is:

 

 

a[1].addEventListener(MouseEvent.CLICK, onClick);

...

public function onClick(event:MouseEvent):void{
     var source:YourSpriteClass = event.target as YourSpriteClass;
     //Do some wicked stuff here
}

 

 

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Developer
Posts: 660
Registered: ‎10-08-2009
My Device: Bold 9900
My Carrier: Telus

Re: Array of sprite individual addEventListener

Thanks for the reply,

 

What is the "YourSpriteClass"? I've modified my code a little:

 

 

c = new Array();
for (var i:int = 0; i < 10; i++)
{
	c[i] = new Sprite();
	c[i].graphics.beginFill(0x000000);
	c[i].graphics.drawRect(50 + i*20, 50 + i*20, 30, 30);
	c[i].graphics.endFill();
	c[i].addEventListener(MouseEvent.CLICK, change);
}

public function change(event:MouseEvent):void
{
	var source:(What goes in here based on the variables I've used above?) = event.target as YourSpriteClass;
}

 

 

 

----------------------------------------------------------
Twitter: @gyubok
Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: Array of sprite individual addEventListener

I just assumed you had created your own class that extended the Sprite class and you called it YourSpriteClass. In your case your code should be:

 

 

c = new Array();
for (var i:int = 0; i < 10; i++)
{
	c[i] = new Sprite();
	c[i].graphics.beginFill(0x000000);
	c[i].graphics.drawRect(50 + i*20, 50 + i*20, 30, 30);
	c[i].graphics.endFill();
	c[i].addEventListener(MouseEvent.CLICK, change);
}

public function change(event:MouseEvent):void
{
	var source:Sprite = event.target as Sprite;
        //Code here!
}

 

You could also use 

public function change(event:MouseEvent):void
{
	var source:Sprite = Sprite(event.target);
        //Code here!
}

 

The only difference is that the source variable in the first case will be null when event.target is not a Sprite (like when you assigned the EventListener to another object as well) and the second solution will throw a TypeError.

 

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Developer
Posts: 660
Registered: ‎10-08-2009
My Device: Bold 9900
My Carrier: Telus

Re: Array of sprite individual addEventListener

Thanks for the help! It's worked perfectly, you saved me lots of work!!

----------------------------------------------------------
Twitter: @gyubok
Developer
Posts: 1,008
Registered: ‎12-12-2010
My Device: Passport (Red Limited Edition)
My Carrier: Mobile Vikings

Re: Array of sprite individual addEventListener

No problem, I am here to help (and learn).

-------------------------------------------
BlackBerry Certified Builder for Native Application Development -- Proud member of the Belgian BlackBerry Developer group
Samples: Park in Ghent
Feeling generous? Nominate me for BB Elite member!
Developer
Posts: 660
Registered: ‎10-08-2009
My Device: Bold 9900
My Carrier: Telus

Re: Array of sprite individual addEventListener

Is there a way to identify which sprite have been clicked?

----------------------------------------------------------
Twitter: @gyubok
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Array of sprite individual addEventListener

hey gyubok,

 

there was a thread a while back discussing how to get a lock on which object was the one to invoke a method. ill dig up the thread but basically you have two options. objects in AS3 arent like html where they have specific ID's. so what we must do is one of two things. Either create our own objects and add a psuedo "id" field to identify objects or there is a .name property for all objects that some users leverage. I havent taken the .name property approach yet but it seems like it would work.

 

here is the thread discussing the possiblity of creating an array of labelbutton objects and giving them identifying ID properties:

 

http://supportforums.blackberry.com/t5/Tablet-OS-SDK-for-Adobe-AIR/How-to-create-array-of-controls-I...

 

hope that helps. good luck!

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman
Developer
Posts: 660
Registered: ‎10-08-2009
My Device: Bold 9900
My Carrier: Telus

Re: Array of sprite individual addEventListener

Thanks JRab, the method you outlined worked great! Thanks for helping me out all the time!

----------------------------------------------------------
Twitter: @gyubok
Developer
Posts: 2,462
Registered: ‎11-04-2010
My Device: Bold 9700

Re: Array of sprite individual addEventListener

hah thats what everyone in this forum is here for - just crank out those great apps! Smiley Happy

J. Rab (Blog) (Twitter)
--
1. If you liked my post or found it useful please click on the thumbs up and provide a Like!
2. If my post solved your problem please click on the Accept as Solution button. Much appreciated!

Approved Apps: OnTrack | ssShots | Hangman