AS3 can custom Event class extend MouseEvent?

mankis
mankis used Ask the Experts™
on
I need to pass variables in to a function with MouseEvent.CLICK.
How can I do that, is there a way to make custom Event class that extends MouseEvent?
Do you know some tutorial?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Hi, I have already seen this searching the google and is not working. Here is the part of my code and it always trace the same number (array length -> numbr in for a loop).

function createPage(id:int) {
		 trace("mc id = "+id);
}


function addButtons() {
			for (a=0; a<numbr; a++) {
				br = br+n[a];
				try {
					kvadrati[a + br].addChild(forattach[a]);

					kvadrati[a+br].getChildAt(1).getChildByName("insidebtn").alpha = 0;
					TweenLite.to(kvadrati[a+br].getChildAt(1).getChildByName("insidebtn"), 0.5, {alpha:1});

					kvadrati[a + br].addEventListener(MouseEvent.CLICK, function(e:MouseEvent){createPage((a + br))});
					

				} catch (e:ArgumentError) {
					trace(e);
				}
			}

		}

Open in new window

Author

Commented:
actually once I have clicked very quickly and I get penultimate number (array.length-1), but when I click again (on any button on the stage) I get the last number (array.length)
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Author

Commented:
no, sorry I'm telling it wrong, I'm getting the last (a + br) number, but it is important that is not working

Author

Commented:
I have changed my function a little bit so now I have objects in array that contains movieclip and other vars
so now I have object id like this:
kvadrati[a + br].id
and movieclip like this:
kvadrati[a + br].kvadarmc
but I still don't know how to pass kvadrati[a + br].id into function when i click on kvadrati[a + br].kvadarmc?

Author

Commented:
do I need a class that extends MovieClip instead of this Object that I used?

Author

Commented:
I have created new Class that extends MovieClip and now I can access id property from function. So I wasn't able to pass the variable to the function, I just found another solution. If you have a solution for my question (how to pass variable to a function with MouseEvent.CLICK) I would still like to hear it.
Top Expert 2009

Commented:
the code snippet you showed above, which of the solutions are you supposed to be using there from the link halfbloodprince posted?

Author

Commented:
hi, sorry, no, I now have something like this
function createPage(e:MouseEvent) {
		 trace("mc id = "+e.target.id);
}


function addButtons() {
			for (a=0; a<numbr; a++) {
				br = br+n[a];
				try {
					kvadrati[a + br].addChild(forattach[a]);

					kvadrati[a+br].getChildAt(1).getChildByName("insidebtn").alpha = 0;
					TweenLite.to(kvadrati[a+br].getChildAt(1).getChildByName("insidebtn"), 0.5, {alpha:1});

					kvadrati[a + br].addEventListener(MouseEvent.CLICK, createPage);
					

				} catch (e:ArgumentError) {
					trace(e);
				}
			}

		}

Open in new window

Author

Commented:
this is my extended MovieClip class that I use for creating this movie clip
package {
	import flash.display.*;
	import flash.events.*;
	
	public class Kvadarmc extends MovieClip {
		
		public var pozicijax:Number;
		public var pozicijay:Number;
		public var id:int;

		public function Kvadarmc() {
			
			
			this.pozicijax=0;
			this.pozicijay=0;
			this.id=0;
		}
		
		
		
	}
}

Open in new window

Author

Commented:
so when I create my array of moviclips I do it like this
for (j=0; j<=numlength; j++) {
   kvadrati[j]=new Kvadarmc();
					addChild(kvadrati[j]);
 kvadrati[j].id=j;
}

Open in new window

Top Expert 2009

Commented:
ok it might be easier if you zip everything up and upload it then all the bits and pieces.

Author

Commented:
i can't do that, it's the whole project that I'm working on, it's too big and I don't wont to share everything

Author

Commented:
I just find out that the problem is not in my or halfbloodprince solution,  both work.
The problem is that I have MovieClip that is child of another MovieClip and that MovieClip is a child of another MovieClip, and although I have put listener only for parent MovieClip like this
kvadrati[a + br].addEventListener(MouseEvent.CLICK, createPage);
function createPage traces every time a different MovieClip, somethimes it's the parent, somethimes it's the child MovieClip, or the second child. I don't understand why is this happening. I will accept halfbloodprince solution because I get variables passed, but the problem is in target, and that is for some other question.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial