Link to home
Start Free TrialLog in
Avatar of mankis
mankisFlag for Croatia

asked on

AS3 can custom Event class extend MouseEvent?

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?
ASKER CERTIFIED SOLUTION
Avatar of halfbloodprince
halfbloodprince

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mankis

ASKER

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

Avatar of mankis

ASKER

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)
Avatar of mankis

ASKER

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

ASKER

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?
Avatar of mankis

ASKER

do I need a class that extends MovieClip instead of this Object that I used?
Avatar of mankis

ASKER

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.
the code snippet you showed above, which of the solutions are you supposed to be using there from the link halfbloodprince posted?
Avatar of mankis

ASKER

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

Avatar of mankis

ASKER

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

Avatar of mankis

ASKER

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

ok it might be easier if you zip everything up and upload it then all the bits and pieces.
Avatar of mankis

ASKER

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
Avatar of mankis

ASKER

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.