keycode for MouseEvent? - actionscript-3

So to my understanding, there are keycodes to represent the keystrokes, for example:
public function left(e:KeyboardEvent):void
{
if (e.keycode == 65)
{
leftKey = true;
}
}
I want to do something similar with this logic and apply it for Mouse Events. I've searched on Google but haven't found much results for Flashdevelop AS3. Would there be a keycode to represent Mouse Events?
For example:
stage.addEventListener(MouseEvent.MOUSE_DOWN, down);
stage.addEventListener(MouseEvent.MOUSE_MOVE, move);
stage.addEventListener(MouseEvent.CLICK, click);
public function down(e:MouseEvent):void
{
if (e.keycode == ?)
}
public function move(e:MouseEvent):void
{
if (e.keycode == ?)
}
public function click(e:MouseEvent):void
{
if (e.keycode == ?)
}
Thanks in advance!

When you fire a mouse event that has a listener attached to it, then the function associated with that listener will get called. Period. You don't need to then, again, check to see if the mouse event happened. That's the whole beauty of event listeners. Just to make sure you get it, I'll post an example.
stage.addEventListener(MouseEvent.CLICK,mClick);
Great. We added a listener to the stage. Now, any time you click anywhere on the stage, this listener will cause that mClick function to get called. Now we write the mClick function.
private function mClick(me:MouseEvent):void{
trace("me.target.x",me.target.x);
trace("me.target.y",me.target.y);
}
me is just a variable we chose to represent the event that triggered this function. Event is a class. A MouseEvent is a subclass of Event. So we are saying that me is an Event of sublcass MouseEvent and that this is the expected input for this function. If you were to try to call this function elsewhere in your code it would throw an error saying that this function expected some sort of input.
me.target is the thing that caused the event to get triggered. In this case it is the mouse, so me.target.x will be the x position of the mouse at the time that the mouse was clicked.
That is all there is to it. You have just confused yourself a little bit by trying to apply a specific solution to a different problem, namely, how to register key presses on a keyboard. They are handled slightly fundamentally differently. With a keyboard, we check listen for if the keyboard had a key pressed and then in the event handler, we determine which key was pressed. With mouse events we would have a different listener for mouse move, mouse click, left mouse click and middle mouse click. Since each of those specific events has their own listener, we don't have to evaluate which button was pressed in the handler:
public function left(e:KeyboardEvent):void
{
if (e.keycode == 65)
{
leftKey = true;
}
}
The e is the thing that caused the event. In this case, the Keyboard key. Each key has a keycode. But instead of writing a different listener for every single key on the keyboard, we can just write one listener and one function and then evaluate which key was pressed inside the function. As stated before, with the mouse, the left mouse button gets its own event listener, so no need (or ability) to check that in the event listener. There are properties like ROLL_OVER and MOUSE_UP and MOUSE_DOWN. Look at the documentation for a full list.

Related

transform double click on Z to double click on mouse

When I click 2 times on the touch "z" (keycode 90) on my keyboard, my item is removed.
timer=new Timer(500, 1);
stageRef.addEventListener(KeyboardEvent.KEY_UP, removeDraggedItem);
private function removeDraggedItem(e:KeyboardEvent){
if(timer.running==true)
{
if(e.keyCode==90)
{
stageRef.removeEventListener(MouseEvent.MOUSE_MOVE, dragItem);
stageRef.removeEventListener(Event.ENTER_FRAME, itemHitTest);
draggedItem.removeEventListener(MouseEvent.MOUSE_DOWN, itemClick);
stageRef.removeChild(draggedItem);
toolbar.useText.text = "";
if (stageRef.contains(this))
stageRef.removeChild(this);
Mouse.show();
Engine.playerControl = true;
}
}
else if(e.keyCode==90)
{
timer.start();
}
}
I'd like to change it and when we click 2 times with the mouse the item is removed but I can't figure out how to do it... if mouseDown = true ? it does'nt seem to work...
EDIT :
Ok I've tried to change (e.keyCode==90) by (e.buttonDown). No errors but nothings is happening when I double click...any idea why ?
Your code is only listening for a KEY_UP event: stageRef.addEventListener(KeyboardEvent.KEY_UP, removeDraggedItem);. You should add a KEY_DOWN event as well and then use the KEY_DOWN event in conjunction with the KEY_UP event to check for a double click. The code to add a KEY_DOWN event is stageRef.addEventListener(KeyboardEvent.KEY_DOWN, functionName);.
On another note if your trying to use MouseEvent.DOUBLE_CLICK you have to enable double click for the object you wish to double click. Oh and definitely include flash.events.MouseEvent
object.doubleClickEnabled = true;
object.addEventListener(MouseEvent.DOUBLE_CLICK, functionName);
Also I noticed in your removeDraggedItem function, you start a timer if the keycode equals 90. Then the next time a KEY_UP event is thrown you check if the timer is running and if so you remove a lot of event listeners and objects. You never stop the timer or reset the timer or remove the timer, I was just wondering if that was intentional?

Flash AS3 making it so whenever a button is clicked on screen is like pressing a specific keyboard key

Hello I am working on a game in flash, in the mobile version of this app I am trying to make it so in a mousedown event on a button it will be equal to the Left, Right keycodes etc. being active?
Is there a way in AS3 for a keyboard key to be activated by other means such as mouse clicks?
leftButton.addEventListener(MouseEvent.MOUSE_DOWN, leftKeyPress)
function leftKeyPress(e:MouseEvent){
// Left Key is pressed
Keyboard.LEFT;
trace("trace statement");
}
This code doesent seem to work
EDIT: also this is important this class and the class that uses the keyboard(The player) are in two seperate classes, so Im trying to make it so that when this is click it is recognized as a key press on a keyboard
Sorry but It is all wrong. You can't trigger keyboard events like that. That is the way of capturing events. And your function is also a MouseEvent. Should be keyboard event. I Opened Flash after one year. Anyway. AFAI understand you are trying to disable keyboard until mouse click somewhere.
leftButton.addEventListener(MouseEvent.MOUSE_DOWN, leftKeyPress);
var keyEnabled:Boolean = false;
function leftKeyPress(e:MouseEvent){
keyEnabled = true;
}
key.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
function keyPressed(e:KeyboardEvent):void{
if(keyPressed){
//do whatever here
}
}
You can manually dispatch a keyboard event; however, is not the best design pattern:
dispatchEvent(new KeyboardEvent(KeyboardEvent.KEY_DOWN,
true,
false,
Keyboard.LEFT,
Keyboard.LEFT));
It would be better to establish two handlers:
addEventListener(KeyboardEvent.KEY_DOWN, moveKeyHandler);
addEventListener(MouseEvent.CLICK, moveMouseHandler);
From each handler, call a function that abstracts movement function from the handlers:
protected function moveKeyHandler(event:KeyboardEvent):void
{
/* switch statement for key */
moveLeft();
}
protected function moveMouseHandler(event:MouseEvent):void
{
/* switch statement for button target */
moveLeft();
}
protected function moveLeft():void
{
}

AS3 - Button inside MovieClip triggers MC's event

On stage, I have a MovieClip named "mc" with a simple rectangle drawn inside. mc also has a Button child named "btn" which is another simple rectangle ( smaller than mc's rectangle obviously). Then I have this code on stage.
function mcDown( _e:MouseEvent):void{
trace( "mc" );
}
function btnClick( _e:MouseEvent):void{
trace( "btn" );
}
mc.addEventListener( MouseEvent.MOUSE_DOWN, mcDown );
mc.btn.addEventListener( MouseEvent.CLICK, btnClick );
The problem I am having is when click the button, mcDown event is also triggered and traces both "mc" and "btn".
How can I make it so that when I click the button, it triggers only btnClick and not mcDown along? I tried MOUSE_UP instead of CLICK, same problem. And mcDown event has to remain MOUSE_DOWN.
There is no way to prevent bubbling except setting the bubbles parameter as false in the dispatchEvent.
dispatchEvent(EVENT_TYPE, BUBBLES,....);
However, you can avoid the bubbling by doing a check. Just have the below line as first line of your listener function it avoids the events dispatched from all objects other then targets.
if(e.eventPhase != EventPhase.AT_TARGET) return;
So, for your sample code, when you click on the button both the events dispatches but in the mcDown function it won't execute after the above said line.
If you add button in a MC, and you click on the button, you also click on the MC, because the part of the MC that is under the button is still there and it runes the function for the whole of the MC, you can't remove it.
So it's good idea to make a function that will check if the button is pressed, otherwise it will run the function for the whole of the MC.
This one should do it.
//add this in you constructor
mc.addEventListener(MouseEvent.MOUSE_DOWN, myReleaseFunc);
function myReleaseFunc(e:MouseEvent):void {
if(e.currentTarget.name == Btn1) //Btn1 is instance name for a button
{
Btn_func1();
}
else if(e.currentTarget.name == Btn2) //Btn2 is another button.
{
Btn_func2();
//For every button you'll need to add another function and if statement to check if that button was clicked.
}
else
{
Mc_func();
}
}
// this outside the main class
function Mc_func():void{
//you code here
}
function Btn_func1():void{
//you code here
}
function Btn_func2():void{
//you code here
}
I think that this way is much more effiecient and it will works better and faster and you'll have a lot smaller chance of overloading the system.

Not catching on key up event

I have a flash component TextInput within a movieclip, added to stage, through as3.
The moment this mc is added to stage, it assigns keyboard event (key down and key up events), to the main stage (main app stage), and set main stage focus to main stage.
So far so good.
The problem is, when I type ENTER, I catch KEY_DOWN, but when I release key ENTER, my KEY_UP event is uncatched by my function assigned to that event.
Any body knows why?
It only works if I click the flash player.
(Code provided in comment)
private function initAdded(e:Event){
_main.stage.addEventListener(KeyboardEvent.KEY_DOWN,checkKeysDown);
_main.stage.addEventListener(KeyboardEvent.KEY_UP,checkKeysUp);
MovieClip(_main).setStageFocus()
}
private function checkKeysDown(e:KeyboardEvent):void {
if (e.keyCode == 13) {
trace('enter down')
}
}
private function checkKeysUp(e:KeyboardEvent):void {
if(e.keyCode == 13){
trace('enter up')
}
}
I'm going to guess that the KEY_UP event is being handled by the text area you are typing in, and it doesn't want to share, so it kills the event. Try:
_main.stage.addEventListener(KeyboardEvent.KEY_UP,checkKeysUp, true);
This causes your event listener to catch the event in the capture phase, before the other event listener cancels propagation of the event.

Skipping to next game level in AS3

I am in the process of building a game with around 20 levels. Now, as I was thinking of trying to add a "skip" option to the game with the space bar key. I have a little trouble, since holding down the spacebar invokes the KeyboardEvent.KEY_DOWN event multiple times.
The above scenario (Keydown and keyup) works just fine when I'm trying to move my player character on screen.
The one main concern that is lingering in my mind is "Is this going wrong because the KeyboardEvent listeners do get removed and added when changing levels?"
P.S: Adding the skip option as a button works just fine, but I'd really like to use Spacebar for the ease of use.
Listen for KeyboardEvent.KEY_UP as well, and keep a boolean that acts as a switch. If the keyboard is pressed AND the boolean is false, set the boolean to true and proceed. Then, when the key_up event callback is invoked, reset the boolean to false.
Does somethig like this work for you?
private function levelInit () : {
// ...
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
// ...
}
private function keyDownListener (e : KeyboardEvent) : void {
if (e.keyCode == Keyboard.SPACE) {
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownListener);
// add stuff to initiate the level skip
}
}
You could also call the stage.addEventListener from a setTimeout to delay adding the listener to protect things more.
Remove the key_down listener in the function and add a key_up listener. Then in the key_up function remove that listener and add back the key_down listener.