Click after mouse up outside stage - actionscript-3

I'am drag game scene in Flash project (mouse down - start drag, mouse up - stop drag). If I mouse up outside stage, click on any object (buttons) don't work once. After one click other click works fine. What's wrong?
update: Trace logs shown that there event as mouseOver, mouseDown, mouseUp, mouseOut are dispatches, but not CLICK.
update: There is silencer of first click after drag in the project. It's necessary to eliminate situation of end drag on some game object (dispath click). Sorry. Thank you all for answers.

You might be losing focus when leaving the stage. Try using (Event.MOUSE_LEAVE) to 'force' a mouse_up.

something like this:
private var _draggedItem:Sprite;
myDisplayObject.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
private function mouseDownHandler(event:MouseEvent):void {
_draggedItem = event.currentTarget as Sprite;
_draggedItem.startDrag();
_draggedItem.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
stage.addEventListener(Event.MOUSE_LEAVE, stageMouseOutHandler);
}
private function stopDragCurrentItem():void {
if (_draggedItem) {
_draggedItem.stopDrag();
_draggedItem.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
if (stage) {
stage.removeEventListener(Event.MOUSE_LEAVE, stageMouseOutHandler);
}
_draggedItem = null;
}
}
private function mouseUpHandler(event:MouseEvent):void {
stopDragCurrentItem();
}
private function stageMouseOutHandler(event:Event):void {
trace("stage out!")
stopDragCurrentItem();
}
update:
And concerning the lost focus, you cold do the following in html where you embed your flash:
<object classid="..." codebase="...." width=550 height=400
name="myMovie" onmouseover="window.document.myMovie.focus();">
though i haven't tested it.

Related

MOUSE_DOWN not firing when expected - AS3, Actionscript

MOUSE_DOWN not firing when expected - AS3, Actionscript
I am not seeing the below code trace in MOUSE_DOWN fired, until I lift the mouse button back up.
This happens even on a new FLA, with only MOUSE_DOWN, MOUSE_UP and CLICK handler code -- nothing else.
Regardless of what I do, I only see MOUSE_DOWN, MOUSE_UP and CLICK traced out AFTER I lift (release) the mouse button..
See code below.
It happens if I attach the handlers to the stage;
or to a sprite.
It happens with/without buttonMode being set.
It happens with/without preventDefault() being called in CLICK..
It happens using event:MouseEvewnt or event:Event as some have suggested.
When I click, I see nothing.
When I release, I see this:
Mouse down
Mouse up
Mouse CLICK
Why don't I see "Mouse down" when clicking (before releasing)?
What the heck am I missing here?
Thank you!
stage.addEventListener(MouseEvent.MOUSE_DOWN, function(event:MouseEvent)
{
// event.preventDefault();
trace("Mouse down");
} );
stage.addEventListener(MouseEvent.MOUSE_UP, function(event:MouseEvent)
{
// event.preventDefault();
trace("Mouse up");
} );
stage.addEventListener(MouseEvent.CLICK, function(event:Event)
{
// event.preventDefault();
trace("Mouse CLICK");
} );

Problems disabling movieclip button flash/as3

I've got a screen which involves a movie-clip where the object has a outline to symbolize that it can be clicked. Upon clicking the object, I'm requesting it to do numerous functions, disable itself and then go to another frame which removes the outline symbolizing it cannot be clicked anymore. But once you disable an object it goes to the original frame.
The object itself consists of these 3 frames.
Frame 1: Original State (Glow)
Frame 2: Hover over giving stats
Frame 3: No glow
To summerise i'd like to click the object and for it to go to the no glow frame and disable the movieclip.
The movieclip enabled = 1 is for when the user returns to the this frame, so the scene is aware of the button press.
Movieclip.addEventListener(MouseEvent.CLICK, Fun_Movieclip);
Movieclip.addEventListener(MouseEvent.MOUSE_OVER, Fun_MovieclipMouseOver);
Movieclip.addEventListener(MouseEvent.MOUSE_OUT, Fun_MovieclipMouseOut);
function Movieclip(event:MouseEvent):void
{
MovieclipEnabled = 1;
Movieclip.gotoAndStop(1);
Movieclip.mouseEnabled = false;
}
function Fun_MovieclipMouseOver(event:MouseEvent):void
{
Movieclip.gotoAndStop(2);
}
function Fun_MovieclipMouseOut(event:MouseEvent):void
{
Movieclip.gotoAndStop(3);
}
For some reason when ever the movieclip is disabled, it always reverts back to the glow state. Does anyone have a solution for this? Cheers
Edit: Inside the movieclip, the first frame has Stop();. Don't know if this would interfere with it.
mc.addEventListener(MouseEvent.CLICK, clickHandler);
mc.addEventListener(MouseEvent.MOUSE_OVER, mouseoverHandler);
mc.addEventListener(MouseEvent.MOUSE_OUT, mouseoutHandler);
function clickHandler(event:MouseEvent):void
{
mc.gotoAndStop(3);
mc.removeEventListener(MouseEvent.CLICK, clickHandler);
mc.removeEventListener(MouseEvent.MOUSE_OVER, mouseoverHandler);
mc.removeEventListener(MouseEvent.MOUSE_OUT, mouseoutHandler);
}
function mouseoverHandler(event:MouseEvent):void
{
mc.gotoAndStop(2);
}
function mouseoutHandler(event:MouseEvent):void
{
mc.gotoAndStop(1);
}
Not entirely sure what you meant by:
The movieclip enabled = 1 is for when the user returns to the this frame, so the scene is aware of the button press.
My suggestion for getting the scene to recognize the button click is to have the scene also listen to the mouse click handler

out and over events dispatch together with down event after changing movie size

I have a MovieClip instance (movie) brought by code to the stage. I want to add some effects when mouse over or mouse down for this movie. So, first i added event listeners to this MovieClip:
movie.addEventListener(MouseEvent.MOUSE_DOWN, movieDownHandler);
movie.addEventListener(MouseEvent.MOUSE_UP, movieUpHandler);
movie.addEventListener(MouseEvent.MOUSE_OVER, movieOverHandler);
movie.addEventListener(MouseEvent.MOUSE_OUT, movieOutHandler);
Then i added event handlers:
private function movieDownHandler(e:MouseEvent):void {
trace("down");
}
private function movieUpHandler(e:MouseEvent):void {
trace("up");
}
private function movieOverHandler(e:MouseEvent):void {
trace("over");
}
private function movieOutHandler(e:MouseEvent):void {
trace("out");
}
And when i test it, everything goes ok: mouse over this movie, traces over, mouse down traces down, mouse up traces up and so on... But, when i add size change to the movie, for example, to mouse down handler like this:
private function movieDownHandler(e:MouseEvent):void {
trace("down");
movie.scaleX = 0.9;
movie.scaleY = 0.9;
}
and some filter effect to over handler, for example blurFilter:
private function movieOverHandler(e:MouseEvent):void {
trace("over");
e.currentTarget.filters = [new BlurFilter(1,1,1)];
}
then i receive unexpected behavior for event handlers: mouse over traces over (it is ok) and then i press (mouse down without releasing mouse button) at movie, then three events happen one after one: 'down', 'out', 'over' (mouse cursor don't leave MovieClip shape). What is the problem? Furthermore, setting scaleX and scaleY to 1.1 doesn't break handlers behavior
When you click a button,it go through three stage,first 'over',then 'down',then 'up',so it trace like that.
scareX has a range:0~1,the sacre is 0% ~ 100%

prevent next scene frame on movieclip mouseclick

I have a scene with a few objects as movieclips which can be clicked one at a time.
What happens is that I'm able to click every object and on click the scene switches to the next frame.
How do I change that?
Basically I have a key and a door, both movieclips.
You can collect the key, it disappears and after that you are able to click the door to open it.
What actually happens is you are both able to click the key and the door.
When you click the key, it's working as intended, but when you click the door, the key still disappears. This is much more annoying with more than 2 objects.
code for the key:
addEventListener(MouseEvent.CLICK, CollectKey);
function CollectKey(event: MouseEvent): void
{
this.visible = false;
// door
MovieClip(root).door.addEventListener(MouseEvent.CLICK, MovieClip(root).FinishGame);
}
code for the door:
stop();
function FinishGame(event: MouseEvent): void
{
if(MovieClip(root).currentFrame == 4)
{
nextFrame();
}
}
http://www.wuala.com/sollniss/stuff/Untitled-2.swf/
http://www.wuala.com/sollniss/stuff/Untitled-2.fla/
EDIT
After looking at your .fla, I can see your issue:
On your first frame, you have the following script:
stop();
addEventListener(MouseEvent.CLICK, StartGame);
function StartGame(event: MouseEvent): void
{
nextFrame();
}
You likely aren't aware that the mouse click listener you add there, doesn't go away until you tell it to (even if the frame changes). That's why every click calls next frame.
To remedy this, simply remove the listening before you move on to the next frame:
function StartGame(event: MouseEvent): void
{
removeEventListener(MouseEvent.CLICK, StartGame);
nextFrame();
}
And, may be that only visible false is not enough, you also need to set enabled = false and mouseEnabled = false for the key element, because without it, it will keep hearing the click event.

Is every MOUSE_DOWN guaranteed a MOUSE_UP?

Are there any circumstances under which a Flash application could receive two MouseEvent.MOUSE_DOWN without a MouseEvent.MOUSE_UP in between?
In my ActionScript 3 application, I want to track the user dragging a sprite. My plan is to listen for a MouseEvent.MOUSE_DOWN, start an indefinitely repeating timer that records the mouse position, and stop the timer on MouseEvent.MOUSE_UP. Is that a good plan?
Click but do not release the button.
While keeping the button down move the cursor outside.
Release the button outside.
Come back inside and click again.
You will get two down event without an up event.
A better solution to drag-and-drop might be to listen MOUSE_MOVE event and in the event handler check buttonDown property. If the button is down while moving, this will be true, otherwise false.
EDIT: For stage even if you release the button outside the flash window then up event is fired. So for stage this can work, but still instead of timer using MOUSE_MOVE with buttonDown looks better to me. You can check yourself with the following code:
public class StageEventTest extends Sprite
{
public function StageEventTest()
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
}
private function onDown(evt:MouseEvent):void {
trace("down");
}
private function onUp(evt:MouseEvent):void {
trace("up");
}
private function onMove(evt:MouseEvent):void {
trace("move", evt.buttonDown);
}
}
it will miss the mouse up event if the mouse is outside of the stage and continue it's mouse tracking when mousing over the stage since the mouse up event was never called. this likely problem can be mitigated by using Event.MOUSE_LEAVE - forward the mouse leave event to the mouse up event handler so that if the user mouses away from the stage the mouse up event will be called.
Yes, but unless you are listening to the stage, I think you can miss the mouseup event if the mouse has gone outside the Flash movie. Nothing beats an experiment though.
Below you can find the code I always use for custom dragging. Normally, it should work fine. I listen to the MOUSE_MOVE-event for repositioning and tracking the sprite, but you can just as well use the Event.ENTER_FRAME-event or use a timer instead.
ObjectToDrag.addEventListener(MouseEvent.MOUSE_DOWN, startCustomDrag);
public function startCustomDrag(e:MouseEvent):void
{
_prevX= e.stageX;
_prevY= e.stageY;
stage.addEventListener(MouseEvent.MOUSE_MOVE, dragObject); //REPLACE BY TIMER OR Event.ENTER_FRAME
stage.addEventListener(MouseEvent.MOUSE_UP, stopCustomDrag);
}
private function dragObject(e:MouseEvent):void
{
ObjectToDrag.x += _prevX - e.stageX; //OR ANY OTHER CALCULATION
ObjectToDrag.y += _prevY - e.stageY; //OR ANY OTHER CALCULATION
_prevX= e.stageX;
_prevY= e.stageY;
}
public function stopCustomDrag(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, dragObject); //REPLACE BY TIMER OR Event.ENTER_FRAME
stage.removeEventListener(MouseEvent.MOUSE_UP, stopCustomDrag);
}