ActionScript 3 No Mouse Click When Mouse Moves - actionscript-3

I have different elements in a scroller:
protected var theScroll:Scroller = new Scroller();
Every Element got an EvenetListener:
mc1.addEventListener(MouseEvent.MOUSE_DOWN, showMC, false, 0, true);
If a user wants to scroll the elements the EventListener is called and the user can't scroll.
How can I check whether a user wants to scroll or click?
Any ideas?
Best Janine

Set a mouse down flag in your element at the mouse down handler, if that boolean flag is true and you are having a mouse move event (check it at mouse move handler) then that means user is trying to drag. Do not forget to reset the flag when mouse goes up. FYI: Flex has a drag event.

Related

AS3 addChild() drawing on top of my movieClip on CLICK

So basically my issue is when I click on my movieClip I want it to spawn this animation I did over the protonCore. Essentially to show that when you click the protonCore you generate 1 proton. However the problem with this when spamming your CLICK, is that when it adds this child on every click, it draws on top of the movieClip and prevents hit detection while the addedChild "fuseSpark" is added to the stage. Is there a way to make it so when I add this child it doesn't affect the hitBox of the clickable movieClip?
function protonGenerator(e:MouseEvent):void
{
var fuseSpark:MovieClip = new MC_FX_fuse;
stage.addChild(fuseSpark);
fuseSpark.x = stage_protonCore.x;
fuseSpark.y = stage_protonCore.y;
}
An easy solution would be to disable the mouse for those children when you create them:
fuseSpark.mouseEnabled = false;
That is, ofcourse, only if you don't care if the user can click those elements.

How to click through a Rectangle (like mouseChildren = true)

I have this problem, I use this image pan class: http://www.lextalkington.com/blog/2009/08/auto-pan-class-for-panning-an-image-on-mouse-movement/
but the problem is that the objects/sprites/movieclips that are in it have to be clickable, only problem is that the mouseChildren adn mouseEnabled properties can't be applied to a Rectangle object.
Anyone has an idea on how to be able to click through this so I can acces my objects in the panned item? (if that makes any sense...)
This class is using a Rectangle as the scrollRect for the image. The scrollRect only specifies the visible area of the image. It is not the thing you want to detect mouse clicks on.
Instead, you can listen for a mouse click on the image itself.
From the code you linked to, the image is a DisplayObject variable named _clip.
In the constructor for that image panning class, you can add your mouse listener:
_clip.addEventListener(MouseEvent.CLICK, onImageClick);
Then define the event handler:
private function onImageClick(event:Event):void
{
// do something
}
By the way, since _clip is a DisplayObject, it doesn't have mouseChildren or mouseEnabled properties (those are defined in subclasses of DisplayObject).
_clip.mouseEnable = false;
this should work, considering tha _clip will be clicked through

Disable mouse event (click) whilst actions are performed

I'm currently design an application that when a button is pressed it expands to display further information. A major issue I am faced with is that if the button is pressed whilst contracting so that it expands again, the co-ordinates are saved from when it was clicked, meaning it will never return to its original state.
I either need a way of disabling the mouse click on the button whilst the TweenMax is doing its job in contracting the button, or by extracting the coordinates from an array.
I've managed to get the array of coordinates from my menu class into the main class, but can't work out the best way in order to stop the problem from occurring.
--
expand = false;
(run menu function)
item.addEventListener(MouseEvent.CLICK, boxExCo);
private function boxExCo(e:MouseEvent):void
{
if (!expand)
{
selectedBox = e.target as Box;
boxX = selectedBox.x;
boxY = selectedBox.y;
expand = true;
TweenMax.to.... (expand)
}
else
{
expand = false;
TweenMax.to... (contract to coordinates)
}
}
You need to use item.removeEventListener(MouseEvent.CLICK, boxExCo); when you no longer want the event to be able to fire, and then just add it back on using
item.addEventListener(MouseEvent.CLICK, boxExCo); when you want the event to be able to fire again.
Once you start your tween max, remove the event.
Once it is finished, add the event back on.

AS3: Drag and Drop button

I have a button with a MouseEvent.CLICK listener. The CLICK event is being triggered when the button is pushed, mouse is down while rolled out, then released when rolled in on the button again. I do not want this to happen, the click event should not occur when the button is being dragged.
My flash file contains a large amount of buttons and click listeners, I would like to solve this problem with as little code as possible. What is the simplest solution to this problem?
you need to add event listeners and handlers when they are required and remove them when they are no longer needed. you will use your own logic for your needs, but here's an example:
button.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownEventHandler);
function mouseDownEventHandler(evt:MouseEvent):void
{
evt.currentTarget.addEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
evt.currentTarget.addEventListener(MouseEvent.ROLL_OUT, rollOutEventHandler);
trace("Mouse Down");
}
function mouseUpEventHandler(evt:MouseEvent):void
{
evt.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
evt.currentTarget.removeEventListener(MouseEvent.ROLL_OUT, rollOutEventHandler);
trace("Mouse Click (Mouse Up)");
}
function rollOutEventHandler(evt:MouseEvent):void
{
evt.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, mouseUpEventHandler);
evt.currentTarget.removeEventListener(MouseEvent.ROLL_OUT, rollOutEventHandler);
trace("Roll Out");
}
if you have a lot of buttons which behave the same way, you should create a custom button class of which all of your buttons would be instances.
On mouse down, record the mouse coordinates, do the same on mouse up and compare the two coordinates. If the distance is more than 10px (or whatever you want) then cancel the click (or set some boolean to false that allows the code in the click listener to run).
or
On mouse down, start recording the mouse coordinates, so you know the clip has been moved, then on mouse up, you know if the clip has been moved even if the user places the clip back on exactly the same spot.

Simulate a swipe momevent in As3, Flash or Flex

Here's my dilemma:
If I add buttons to a Sprite, I have to listen for the MOUSE_DOWN or CLICK event to make the button work as it should.
However, I want the sprite that contains the button to function like a touchscreen device, and I want the contents of the sprite to scroll up-and-down when you swipe it...
I am afraid the mouse events of the buttons will prevent the events from being captured by the container. This would mean that when I swipe over the button, the button get's clicked instead of the container.
I know Flex passes events down the chain of elements, but I don't believe this is the case for Flash.. ?
You should use MOUSE_UP event instead of MOUSE_DOWN, it's better practice, even if not to consider problems with scrolling. After there's nothing on MOUSE_UP you can hang
function onMouseDown(e: Event) {
flag = true
}
on MOUSE_DOWN. After that
function onMouseDown(e: Event) {
if (flag) {
//scrolling here
}
}
and dont forget to set flag=fasle on MOUSE_UP.