Reputation: 1
I want to check if four on-screen buttons are being pressed/released using 2 eventListeners and 2 functions, I know I can make each button press/unpress point to a different function but that leaves me 8 eventListeners and 8 functions in total, the CODE shown here I could make it work using keyboard keys instead of buttons (changing mouseEvent to keyboardEvent etc) but I need it to be buttons
All my buttons have different instances for each one, they are all inside another big instance.
Using keyboard I filled the (I_DONT_KNOW_WHAT_GOES_HERE) with (event.keycode == EXAMPLE.KEY) so it worked perfectly.
but I need to know how to change (KEYBOARD_KEY_BEING_PRESSED) to (INSTANCE_OF_BUTTON_BEING_PRESSED)
Is there any way to achieve this? or I have to make the whole thing in other way?
CODE:
stage.addEventListener(MouseEvent.MOUSE_DOWN, mousePressed);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);
function mousePressed(event:MouseEvent):void
{
if (I_DONT_KNOW_WHAT_GOES_HERE)
{
buttonOnePressed = true;
}
else if (I_DONT_KNOW_WHAT_GOES_HERE)
{
buttonTwoPressed = true;
}
else if (I_DONT_KNOW_WHAT_GOES_HERE)
{
buttonThreePressed = true;
}
else if (I_DONT_KNOW_WHAT_GOES_HERE)
{
buttonFourPressed = true;
}
}
function mouseReleased(event:MouseEvent):void
{
if (I_DONT_KNOW_WHAT_GOES_HERE)
{
buttonOnePressed = false;
}
else if (I_DONT_KNOW_WHAT_GOES_HERE)
{
buttonTwoPressed = false;
}
else if (I_DONT_KNOW_WHAT_GOES_HERE)
{
buttonThreePressed = false;
}
else if (I_DONT_KNOW_WHAT_GOES_HERE)
{
buttonFourPressed = false;
}
}
Upvotes: 0
Views: 1019
Reputation: 8149
You're looking for Event.currentTarget
, which is the object which most recently dispatched the event (Event.target
is the original dispatcher of the event). So you check if Event.currentTarget
is equal to each button.
Example (not a working set of code, but it will give you an idea of how to do this)
var btn1:Sprite, btn2:Sprite, btn3:Sprite, btn4:Sprite;
function mouseDownHandler(e:Event):void {
if (e.currentTarget == btn1) {
}
else if (e.currentTarget == btn2) {
}
else if (e.currentTarget == btn3) {
}
else if (e.currentTarget == btn4) {
}
}
Event.currentTarget
documentation
Upvotes: 1