How do I stop mouse_out firing when mouse click fires - actionscript-3

I have buttons that have mouse_over, mouse_out and CLICK events. But when I click the button it takes me to another frame and the mouse_out event tried to fire. How do I stop that happening?
act1_btn.addEventListener(MouseEvent.CLICK, act1Pressed);
act1_btn.addEventListener(MouseEvent.MOUSE_OVER, act1Over);
act1_btn.addEventListener(MouseEvent.MOUSE_OUT, act1Out);
act1_btn.addEventListener(Event.ENTER_FRAME, act1EnterFrame);
function act1Over(e:MouseEvent):void
{
trace("over");
act1Animating = true;
logo_1.visible = true;
bubble.visible = true;
txt1.visible = true;
}
function act1Out(e:MouseEvent):void
{
act1Animating = false;
logo_1.visible = false;
bubble.visible = false;
txt1.visible = false;
}
function act1EnterFrame(e:Event):void
{
if (act1Animating && e.target.scaleY < 1.1)
{
e.target.scaleY += 0.02;
e.target.scaleX += 0.02;
}
if (!act1Animating && e.target.scaleY > 1)
{
e.target.scaleY -= 0.02;
e.target.scaleX -= 0.02;
}
}
function act1Pressed(e:MouseEvent):void
{
trace("clicked");
act1Animating = false;
logo_1.visible = false;
bubble.visible = false;
txt1.visible = false;
gotoAndStop(2);
}

Here are two ways to handle this:
1) Only assign the MOUSE_OUT listener in the MOUSE_OVER handler, then remove it after the MOUSE_OUT handler is done. I.e.,
function act1Over(e:MouseEvent):void {
/* your code */
act1_btn.addEventListener(MouseEvent.MOUSE_OUT, act1Out);
}
function act1Out(e:MouseEvent):void {
/* your code */
act1_btn.removeEventListener(MouseEvent.MOUSE_OUT, act1Out);
}
2) Use stopPropagation() in your CLICK handler:
function act1Pressed(e:MouseEvent):void {
/* your code */
e.stopPropagation();
}
Also, in the future, please use code tags to mark up your code!

It might not be a bad idea to give ROLL_OVER and ROLL_OUT MouseEvent a shot instead. These just fire once when someone rolls over the object, or rolls out, instead of firing continuously.

When you click a button , you will trigger a MouseOver & a MouseOut event , if you don't wish to trigger the MouseOut event after the Click event , then you should remove the MouseOut event listener in the Click event listener.
This means that in order to be sure that you have a MouseOut listener when you MouseOver , you should add your MouseOut listener in the MouseOver listener.
Finally, you should remove the MouseOut event listener within the MouseOut listener.

Related

adding an EventListener to an Enter_Frame event function

I was just curious if I add an a EventListener such as addEventListener(MouseEvent.CLICK, onClick);
Inside an ENTER_FRAME event function will it add an instance of that MouseEvent Listener every frame?
Here is my code now just wondering if this is bad practice:
addEventListener(Event.ENTER_FRAME, engineLogic);
inside my engineLogic function:
//Max objects for rock throwers on left
if (aXPositionArray.length == 0)
{
//Remove listener to add more and make button turn grey etc
rockThrowerSpawnScreen.left.removeEventListener(MouseEvent.CLICK, chooseSpawnSideRockThrowers);
rockThrowerSpawnScreen.left.visible = false;
}else
if (aXPositionArray.length != 0)
{
rockThrowerSpawnScreen.left.addEventListener(MouseEvent.CLICK, chooseSpawnSideRockThrowers);
rockThrowerSpawnScreen.left.visible = true;
trace("LISTENER");
}
or does it only add it once and checks the function every frame?

change keyboard event to mouse event as3

I am a new new person who learn action script 3.
i have problem when i convert keyboard event to mouse event when i moving a walking character.
when use the keyboard event i have no problem. this is my code
import flash.ui.Keyboard;
var speed:Number=2;
stage.addEventListener(KeyboardEvent.KEY_DOWN, stikman);
function stikman(e:KeyboardEvent)
{
if (e.keyCode==Keyboard.LEFT)
{
stik.x-=speed;
stik.scaleX=-1;
stik.stik2.play();
}
else if (e.keyCode==Keyboard.RIGHT)
{
stik.x+=speed;
stik.scaleX=1;
stik.stik2.play();
}
}
and then i try to change keyboard event to mouse event when moving character with button it should press click, click and click. i want to hold the click when moving the character and when mouse up the character stop. but i still don't know how. this my code when i try to change to mouse event
var speed:Number=2;
mundur.addEventListener(MouseEvent.MOUSE_DOWN, stikman);
function stikman(e:MouseEvent)
{
stik.x-=speed;
stik.scaleX=-1;
stik.stik2.play();
}
maju.addEventListener(MouseEvent.CLICK, stikman2);
function stikman2(e:MouseEvent)
{
stik.x+=speed;
stik.scaleX=1;
stik.stik2.play();
}
Because keyboard produces KeyboardEvent.KEY_DOWN event repeatedly as long as key is pressed, while MouseEvent.CLICK as well as MouseEvent.MOUSE_DOWN are dispatched only once per user action.
With mouse you need to change the logic.
// Subscribe both buttons.
ButtonRight.addEventListener(MouseEvent.MOUSE_DOWN, onButton);
ButtonLeft.addEventListener(MouseEvent.MOUSE_DOWN, onButton);
var currentSpeed:Number = 0;
var isPlaying:Boolean = false;
function onButton(e:MouseEvent):void
{
// Set up the directions and start the animation.
switch (e.currentTarget)
{
case ButtonLeft:
currentSpeed = -speed;
stik.stik2.play();
stik.scaleX = -1;
break;
case ButtonRight:
currentSpeed = speed;
stik.stik2.play();
stik.scaleX = 1;
break;
}
isPlaying = true;
// Call repeatedly to move character.
addEventListener(Event.ENTER_FRAME, onFrame);
// Hook the MOUSE_UP even even if it is outside the button or even stage.
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
}
function onFrame(e:Even):void
{
// Move character by the designated offset each frame.
stik.x += currentSpeed;
if (!isPlaying)
{
// Stop at last frame.
// if (stik.stik2.currentFrame == stik.stik2.totalFrames)
// Stop at frame 1.
if (stik.stik2.currentFrame == 1)
{
// Stop the animation.
stik.stik2.stop();
// Stop moving.
removeEventListener(Event.ENTER_FRAME, onFrame);
}
}
}
function onUp(e:MouseEvent):void
{
// Indicate to stop when the animation ends.
isPlaying = false;
// Unhook the MOUSE_UP event.
stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
}

Action Script. How to disable keyboard?

how to disable keyboard keys in Action Script?
I'm creating Flash "memory" game, Idea to discover 2 equal cards. When 2nd card is discovered it is shown for 750 milliseconds, in that time player can't do any actions.
When I use this mouseChildren = false; player can't click with mouse for this time, but he can use keyboard arrows/enter/space/tab buttons... I need to disable It for this time.
Here is part of my code:
{
trace("Wrong");
_message = "Wrong";
message_txt.text = _message;
_secondCard = event.currentTarget;
var timer:Timer = new Timer(750, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, flipBack);
timer.start();
stage.addEventListener(KeyboardEvent.KEY_DOWN, blindKeyboard);//added here
stage.addEventListener(KeyboardEvent.KEY_UP, blindKeyboard);//added here
mouseChildren = false;
}
}
function blindKeyboard(e:KeyboardEvent):void{ //added here function
e.preventDefault();
e.stopPropagation();
}
protected function flipBack(event:TimerEvent):void
{
_firstCard.gotoAndPlay("flipBack");
_secondCard.gotoAndPlay("flipBack");
_firstCard.addEventListener(MouseEvent.CLICK, checkCards);
_secondCard.addEventListener(MouseEvent.CLICK, checkCards);
_firstCard = _secondCard = undefined;
mouseChildren = true;
}
You could just have functions for adding/removing listeners :
function addListeners():void
{
// loop through and add the listeners for the cards
// add keyboard listeners
}
function removeListeners():void
{
// loop through and remove listeners from the cards
// remove keyboard listeners
}
Before you set the timer, you remove your listeners :
removeListeners();
Then in your flipback timer handler you just call the addListeners :
addListeners();
Try
stage.addEventListener(KeyboardEvent.KEY_DOWN, blindKeyboard);
stage.addEventListener(KeyboardEvent.KEY_UP, blindKeyboard);
function blindKeyboard(e:KeyboardEvent):void{
e.preventDefault();
e.stopPropagation();
}

Disabling repeating keyboard down event in as3

now I'm trying to make the keyboard events to stop repeating.
My idea was to have a true and false condition for when the key is pressed so that it wont repeat if the key is down already.
//Mouse Event Over
keyCButton.addEventListener(MouseEvent.MOUSE_OVER, function(){gotoAndStop(2)});
//Variable
var Qkey:uint = 81;
//Key Down Event
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
var soundplayed = false;
function keydown(event:KeyboardEvent){
if (event.keyCode==Qkey) {
this.soundplayed=true;
}
}
if (this.soundplayed==false){
gotoAndPlay(3);
}
//Key Up Event
stage.addEventListener(KeyboardEvent.KEY_UP, keyup);
function keyup(event:KeyboardEvent){
this.soundplayed=false;
gotoAndStop(1);
}
doing this makes the key loop over and over without a keyboard event
I think i need to add a "&& keyDown..." to "if (this.soundplayed==true)" but i dont know how to do it without getting errors
here is the keyboard player i'm trying to fix http://soulseekrecords.org/psysci/animation/piano.html
Just another (perhaps a bit more general) way to write what Kishi already suggested:
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUp);
var downKeys:Dictionary = new Dictionary();
function keyDown(e:KeyboardEvent):void {
if(!downKeys[e.keyCode]) {
downKeys[e.keyCode] = true;
processKeyDown(e);
}
}
function keyUp(e:KeyboardEvent):void {
delete downKeys[e.keyCode];
}
function processKeyDown(e:KeyboardEvent):void {
trace(e.keyCode);
}
The processKeyDown function will be called as if keydown repeating was disabled. If you need to do something when the key is up, put that code in the keyUp function, or maybe call a processKeyUp function defined like processKeyDown.
I'm not sure what you are doing on these frames..
Is that the complete code?
Anyway, you should try something like this:
// Mouse Events
this.keyCButton.addEventListener(MouseEvent.MOUSE_OVER, function():void{ gotoAndStop(2) });
// Variables
var Qkey:uint = 81;
var soundplayed = false;
// Keyboard events
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
this.stage.addEventListener(KeyboardEvent.KEY_UP, keyup);
// Event listeners
function keydown(event:KeyboardEvent){
if (event.keyCode == Qkey && !this.soundplayed) {
this.soundplayed = true;
this.gotoAndPlay(3);
}
}
function keyup(event:KeyboardEvent){
this.soundplayed = false;
this.gotoAndStop(1);
}
Notice that the keydown event listener will now execute once -- I mean.. at least the if branch -- as the soundplayed variable is used as a locking mechanism.
It'll only execute again after keyup's been executed (this.soundplayed = false).

actionscript 3, removeEventListener not working properly

I've made this game with event listener (coordinates) on mouse position when a click occurs (to move the character).
I've another event listener for drag and drop (to combine items) that works pretty well.
function stageDown (event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.CLICK, coordinates);
MovieClip(getChildByName(event.target.name).toString()).startDrag();
MovieClip(getChildByName(event.target.name).toString()).addEventListener(MouseEvent.MOUSE_UP,stageUp);
...stuff..
}
function stageUp(event:MouseEvent):void
{
stopDrag();
...stuff...
stage.addEventListener(MouseEvent.CLICK, coordinates);
}
In the function stageDown I remove the event listener for the movement(coordinates), than I add it again at the end of the function stageUp (when you release the mouse button and the drag is complete)
But is not working, when I release the drag the character start moving, can't understand why
I don't fully understand the why (something to do with how Click events are tracked I suppose) but it is the 'normal' behavior.
Here is how I've handled this in the past. Basically you can add a higher priority click listener to the object you are dragging and cancel the event there: (see code comments)
//Assuming you have something like this in your code/////////
stage.addEventListener(MouseEvent.CLICK, coordinates);
function coordinates(event:MouseEvent):void {
trace("STAGE CLICK"); //whatever you do here
} ///////////////////////////////////////////////////////////
//add your mouse down listener to your object
someObject.addEventListener(MouseEvent.MOUSE_DOWN, stageDown);
//ALSO add a click listener to your object, and add it with higher priority than your stage mouse click listener
someObject.addEventListener(MouseEvent.CLICK, itemClick, false, 999);
function itemClick(event:MouseEvent):void {
//stop the event from reaching the lower priority stage mouse click handler
event.stopImmediatePropagation();
trace("Item CLICK");
}
function stageDown (event:MouseEvent):void
{
Sprite(event.currentTarget).startDrag();
//listen for the mouse up on the stage as sometimes when dragging very fast there is slight delay and the object may not be under the mouse
stage.addEventListener(MouseEvent.MOUSE_UP,stageUp, true);
//if you don't care about mouse up on stage, then you can just forget the mouse up listener and handler altogether and just stop drag on the itemClick function.
}
function stageUp(event:MouseEvent):void
{
//remove the stage mouse up listener
stage.removeEventListener(MouseEvent.MOUSE_UP,stageUp, true);
trace("UP");
stopDrag();
}