Actionscript 3 ContextMenu closed event? - actionscript-3

There is an event which is dispatched when the context menu (right click menu) in actionscript for flash has been opened:
ContextMenuEvent.MENU_SELECT
Now, is there an event which is dispatched when the menu has been closed?

Good question.
That would make a nice feature request, an ContextMenuEvent.MENU_CLOSED event :)
I think I have half you're answer. Here's my idea:
var myContextMenu:ContextMenu = new ContextMenu();
var menuLabel:String = "Custom Item";
var rightClicking:Boolean;
addCustomMenuItems();
myContextMenu.addEventListener(ContextMenuEvent.MENU_SELECT, menuSelectHandler);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseUpHandler);
var redRectangle = makeRedRectangle();
redRectangle.contextMenu = myContextMenu;
function makeRedRectangle():Sprite{
redRectangle = new Sprite();
redRectangle.graphics.beginFill(0x990000,.2);
redRectangle.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
redRectangle.mouseChildren = false;
addChild(redRectangle);
return redRectangle;
}
function addCustomMenuItems():void {
myContextMenu.hideBuiltInItems();
var item:ContextMenuItem = new ContextMenuItem(menuLabel);
myContextMenu.customItems.push(item);
item.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler);
}
function menuSelectHandler(event:ContextMenuEvent):void {
trace("menuSelectHandler: " + event);
rightClicking = true;
}
function menuItemSelectHandler(event:ContextMenuEvent):void {
trace("menuItemSelectHandler: " + event);
}
function mouseUpHandler(event:MouseEvent):void{
if(rightClicking){
trace('ContextMenu Closed\nThank You! Come Again!');
rightClicking = false;
}
}
Basically I create a sprite that sits on top of everything, but has mouseChildren set to false, so clips bellow it can get the clicks. You might want to have this one transparent. I used this so you get an event fired when you right click over it. When that happens I set rightClicking to true, meaning, I know the right click was pressed, I'm just waiting for something else to happen. There are two options:
The user selects an item from the menu.
The user click away to make the menu go away.
For option 1, if the user selects any of your custom items, that's cool, you can handle that, if not, at least you know what might happen.
For option 2 I've setup the listener for the MOUSE_DOWN event, so if the rightClicking was turned on and you got to the mouse down, that's your menu closing.
Hope this helps!
I know, it looks like hacky old school as2, and the code is modified from the documentation example, but it's a thought :)

Related

Actionscript: BlitMask stopping mouse event listeners from working, how to fix?

I have a button class which contains event listeners to trigger an animation of the button on click.
I used many instances of this class to form a list which the user can scroll through. I have implemented BlitMask, this works, however the mouse event listeners in the button class no longer work. This code
_blit = new BlitMask(_mc, _obounds.x, _bounds.y, _bounds.width, _bounds.height, false);
Is what stops the button class.
How can I get the behaviour pre blitmark?
My code which creates the bottom is
var tf:TextField = new TextField(text);
tf.x = 70;
tf.y = 20;
_btn.addChild(tf);
_btn.addEventListener(MouseEvent.CLICK, click);
click is never called.
The blitmask causes the movieclip to be nothing more than an image of clip.
bitmapMode must be set to false to correct this.
_blitMask.bitmapMode = false;
EDIT: To expand on a further issue that implementing this solution causes, turning bitmapMode on only when needed causes the bitmap to not reflect changes in the classes that changed while bitmapMode was off. So you must set
_blitMask.update(null, true);
To force a full update when you use
_blitMask.bitmapMode = true;

AS3 - how to get the mouse cursor to click on a button?

In my application i have a mouse cursor which is attached to the mouse. However it will not let me click on buttons within my application which is a big problem for me as buttons are essential in what i need to do.
I am new to AS so any help would be much appreciated!
stage.addEventListener(MouseEvent.MOUSE_MOVE, draw_cursor);
stage.addEventListener(Event.MOUSE_LEAVE, hide_cursor);
Mouse.hide();
function draw_cursor(event:MouseEvent):void
{
my_cursor_mc.visible = true;
my_cursor_mc.x = event.stageX;
my_cursor_mc.y = event.stageY;
}
function hide_cursor(event:Event):void
{
my_cursor_mc.visible=false;
}
i tried using this (below) but was very glichy and had to press button for cursor to go away THEN i was able to click on the button (not really ideal):
stage.addEventListener(MouseEvent.CLICK, hide_cursor);
Sounds like your cursor might be stealing the mouse events for your buttons. In your top level code (or constructor) try adding:
// Disable mouse events for cursor
my_cursor_mc.mouseEnabled = false;
If you mouse event has any child objects also add:
// Disable mouse events for any children of the cursor
my_cursor_mc.mouseChildren = false;

Remove eventListener not working on buttons AS3 - Flash

I am trying to remove an eventlisnter on a button so when the button is pressed the animation completes before you can press the button again. But based on my code below you can push the button as many times as you like:
var LeftButt:MovieClip = new left_button();
var RightButt:MovieClip = new right_button();
var topClip:Sprite = new Sprite();
addChild(topClip);
LeftButt.addEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "left");});
RightButt.addEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "right");});
function clothingApp(event:MouseEvent):void{
topClip.addChild(RightButt);
topClip.addChild(LeftButt);
}
function moveItems(event:MouseEvent, SlideDirection:String):void{
LeftButt.removeEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "left");});
RightButt.removeEventListener(MouseEvent.MOUSE_UP, function(e){moveItems(e, "right");});
trace(SlideDirection);
}
So technically this code should only run once because I never set up the eventListener again. But you can press the buttons as many times as you like.
If you want to remove event listeners, you can't add them using anonymous functions.
Create a wrapper function with the same functions as your anonymous function, and you'll be fine.
function moveLeft(event:MouseEvent):void
{
moveItems(event, "left");
}
function moveRight(event:MouseEvent):void
{
moveItems(event, "right");
}
LeftButt.addEventListener(MouseEvent.MOUSE_UP, moveLeft);
RightButt.addEventListener(MouseEvent.MOUSE_UP, moveRight);
LeftButt.removeEventListener(MouseEvent.MOUSE_UP, moveLeft);
RightButt.removeEventListener(MouseEvent.MOUSE_UP, moveRight);

Is it possible to remove an eventListener temporarily (i.e. until a function has been executed)

I am doing actionscript 3 at the moment and was wondering if it was possible to remove and eventListner temporarily. I know of removeEventListener, however, that removes the eventListener completely and i cannot click the button again.
If you want some more details, here the exact problem. I have function that when a button is pressed, an object appears. In the function which makes this object there is an eventListener which leads to a function which allows the user to press that object. When you press that object it disappear and the button will animate. However, since the original eventListener still exists, you can press the object while in motion and create a new object. So to the point: What i want to do is disable the eventListener while the button is moving, and reactivate it when it stops.
The best way is to simply use a flag which tells the function if the animation is complete or not. Here's an example of what I'm talking about using TweenLite as tween library:
public class CreateButton extends Sprite{
private var animating:Boolean = false;
public function CreateButton(){
this.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
}
private function onClick(event:MouseEvent):void{
if(this.animating == false){
// Trigger creation functionality
TweenLite.to(this, 0.5, {/* Parameters for the actual animation */ onComplete:animationComplete});
this.animating = true;
}
}
private function animationComplete():void{
this.animating = false;
}
}
It is best practice to remove the listener if it's functionality is to be disabled. You could however set the .mouseEnabled to false if you want to disable it's click functionality without removing the listener.

Fake mouseclick on hover AS3

This morning I stumbled to this question:
When hovering a button, is it possible to click it automatically after X seconds? So the user doesn't need to click it with his mouse?
How can I let Flash believe my mouse really clicked some button on the stage or brought up with as3?
I have a lot of buttons in my movie. So I prefer to use some code which will cover this function for all existing or coming up buttons in my movie.
I would normally use a code like this but is there some workaround to accomplish this in a different way? I do no want to add code to every button.
this.addEventListener(MouseEvent.OVER, onMouseClickEvent);
public function onMouseClickEvent(event:Event)
{
trace(event);
if(event.buttonDown) // if button goes down normally
trace("MOUSE CLICKED NORMALLY");
else
trace("left button was not down");
}
The easiest way i think, is to subclass Button.
Then you should add mouse over/out listeners, add click listener that looks like that
:public function clickListener(event:MouseEvent = null){...}
When the mouse is hovering, raise a flag that the mouse is on the object, start a timer and when the timer callback function is called, you check the if the flag (you turn the flag down, when the mouse is out) is true and just call clickListener()
Listen for MouseEvent.MOUSE_OVER and start a timer, at the end of which the button will send the MouseEvent.CLICK event. In the mouseover handler, use the SystemManager to add a listener for MouseEvent.MOUSE_OUT which cancels the timer. The timer removes the listener using the SystemManager as well. So does clicking the button.
Finally! Solved!
This did the trick:
public function runOnce(event:TimerEvent):void {
btnSignal.dispatch("KEYBOARD", btnCode);
}
Robusto & Radoslav Georgiev: Thank you for pointing the right direction!
(I'm answering this a little late but would like to give input for future people).
One way to 'skin this cat' is to simply let your hover event trigger a timer (i.e. 3 seconds). In an EnterFrame or other function let a number or Boolean change when 3 seconds is reached.
//Pseudo code
if(timer == 3)
{ numberVar = 1;
//or
BooleanVar = True;
}
else
{
numberVar = 0;
//or
BooleanVar = false;
}
//end
Then just as you connected your methods to a mouseEvent, connect those same methods to fire when numberVar == 1 or BooleanVar == True. That's it.
For super simplicity and readability let your MouseClickEvent just be numberVar = 1 or BooleanVar = True.
These become super simple to implement over time and in my experience are 'very' error proof. Easy to fix also in the case of a typo or something else. No super elusive imports either. Hope that helped.
Great question by the way (+ 1)
:D