Skip to main content
Inspiring
November 16, 2008
Answered

EventListener function specific to generated display object container

  • November 16, 2008
  • 2 replies
  • 654 views
This has to be simple, if only I knew how. I'm an AS3 newbie, and am having difficulty setting up multiple specific EventListeners within a code generated display object container.

I am generating a 'page' (Sprite) with a heap of 'cards' (Sprites) on it. Each of those cards, in turn, contains a selection of text boxes and other objects generated from database output using a 'for' loop.
My hope is to attach an EventListener to each 'card' so that a MouseEvent will let me manipulate the data that lead to the content of that actual card (the i-th iteration of my for loop). I've been playing with everything I can think of (limited repertoire of thoughts though) and the best I've managed is for my EventListener to access the final set of data, whichever 'card' I click on.

I've summarised and attached my code for the function, and would appreciate any advice (however basic) on how to get back to the i-set of data from the EventListener attached to the i-th card. I can handle the PHP and MySQL side of things but am fumbling my way into the OOP of AS3.

Cheers
Dougal


This topic has been closed for replies.
Correct answer _dxw_
You are welcome.
Problem fixed, and something new learnt.
Thanks Andrei1.

Cheers
Dougal

2 replies

Inspiring
November 17, 2008
Another way would be to extend Sprite and add an id value. Something like:

package{
import flash.display.Sprite;
public class MySprite extends Sprite {
private var _id:int;
public function MySprite(id:int){ _id = id; }
public function get id():int { return _id; }
}
}

And then in your code you'd just make instance of MySprite, passing it i:

// build the cards and text boxes and add them to the reviewPage
for (var i:int = 1; i < (cardsRequired+1); i++) {
// setup the locations for all the objects

var displayCard:Sprite = new MySprite(i);

You can then get i in the event listener function by using the id getter:

function accessThisData(e:MouseEvent):void{
trace(e.target.parent.id);
}

--
Dave -
www.offroadfire.com
Head Developer
http://www.blurredistinction.com
Adobe Community Expert
http://www.adobe.com/communities/experts/


_dxw_Author
Inspiring
November 17, 2008
Thanks Dave.

I like that approach and will try it. I had tried to add a dynamic instance variable to each sprite and child object, along the lines you're suggesting, but apparently never got the syntax quite right.

This part of the learning-curve sure is an exciting place to be :-)

Cheers
Dougal
Inspiring
November 16, 2008
If understood you correctly you need to match the clicked sprite and corresponding text fields. You can give them names that contain the same integer that is i in the loop and then extract it in the listener and use it to get to corresponding text fields.

Also, try not to use nested functions - they are not good.

See attached code.
_dxw_Author
Inspiring
November 16, 2008
Thanks Andrei1, on both suggestions.

I had tried incorporating the "i" into the object name like you suggested, but think I'd only done so with the sprite and not the text boxes and movie clips ... thinking that since they were on the sprite they'd be targeted. I will try again along the lines you've suggested.

As for nested functions ... thanks very much. I'd started doing that because it eased some of the other troubles I was having.

Cheers
Dougal
Inspiring
November 16, 2008
You are welcome.