PDA

View Full Version : Function run MouseEvent.CLICK to pass parameters?



ThinkKirupa
May 7th, 2009, 05:56 AM
Hi.


This will work:


addVolvo.addEventListener(MouseEvent.CLICK, addCar);

function addCar(evt:MouseEvent)
{
var car1:Car = new Car( 1, 1, classVolvo)
}

// MouseEvent is automatically passed


... but I want to pass the a value when calling the function:



addVolvo.addEventListener(MouseEvent.CLICK, addCar(classVolvo));

function addCar(evt:MouseEvent, newClass:Class)
{
var car1:Car = new Car( 1, 1, classVolvo)
}

// I will get this error:
// 1136: Incorrect number of arguments. Expected 2. [addCar(classVolvo)]


This won't work. Because it then just passes the Class and not the MouseEvent. How can I solve this? What should I write in the addCar(...) call function? Ie how to I manually pass an MouseEvent, if that's what I'm supposed to do?

ThinkKirupa
May 7th, 2009, 06:51 AM
I have a new idee.

I will have say 20 buttons { bVolvo, bAudi, bBMW, bFerrari, etc.}. I will have them all to trigger addCar(). Then in that function I want to detect which button triggered the function, so that I can create a new Car of that type (volvo, audi, bmw, etc.).

Please say if there's something you don't understand?

Iamthejuggler
May 7th, 2009, 06:52 AM
You can't pass any parameters with events, unless you use custom events (which you can't here as you are using mouse events). You can however get the mouseevent's target, which will be addVolvo in this case. I assume you want to know which type of car sent the click event? In which case you can do a switch statement to see what kind of add button triggered the event, and then add the relevant type of car. Something like this.



switch(evt.target){
case (addVolvo):
// add a volvo
break;
case (addAnotherType):
// add whatever type here
break;
}

ThinkKirupa
May 7th, 2009, 07:02 AM
You can't pass any parameters with events, unless you use custom events (which you can't here as you are using mouse events). You can however get the mouseevent's target, which will be addVolvo in this case. I assume you want to know which type of car sent the click event? In which case you can do a switch statement to see what kind of add button triggered the event, and then add the relevant type of car. Something like this.



switch(evt.target){
case (addVolvo):
// add a volvo
break;
case (addAnotherType):
// add whatever type here
break;
}


Aha. Thanks you. I don't know if you read my second post when you answered but I think that was what I was looking for.

I'll try and see if I manage. Thanks!

Dimonte
May 7th, 2009, 07:39 AM
The switch statement is one way of doing this, but I think that it's kinda clunky. You may want to use Dictionary class to hold additional properties for your buttons. I've attached an example project, but here's the basic idea:


private var buttonDictionary:/*String*/Dictionary;

public function Main()
{
buttonDictionary = new Dictionary(true);
for (var i:int = 0; i < 5; i++)
{
var button:SomeButton = new SomeButton("Button" + i);
addChild(button);
buttonDictionary[button] = "Button of the number " + String(i);
button.addEventListener(MouseEvent.CLICK, buttonClickHandler);
}
}

private function buttonClickHandler(e:MouseEvent):void
{
trace(buttonDictionary[e.target]);
}
The Dictionary class is very similar to Array, but the indices of Dictionary aren't typed. So you can use your very objects as indices for your dictionary and then access your values by using the event target as dictionary index.

ThinkKirupa
May 7th, 2009, 10:00 AM
Hmm. That helped a bit although I'm still a little confused about dictionaries.


I now have a piece of code (doing what I requested in post #2) but I want to know if there is a more delicate way to do this. I don't really know why it works but it does:



addAudi.addEventListener(MouseEvent.CLICK, addCar);

function addCar(evt:MouseEvent)
{
var car1:Car = new Car( 1, 1, Class(getDefinitionByName("mc" + evt.target.name.substring(3, 20)))); // = Class(mcAudi)
addChild(car1);
car1.x = 250;
car1.y = 250;
}

// When I press addAudi it will call the function addCar and find the target.
// It will then extract the car model from addAudi and add "mc" in front and make a class of it.
// It then creates a new car of that model.