Getting Mouse Position on Event - actionscript-3

I have two events; mouse up and down. I get the initial position of the mouse when left button down. Then I plan to get the last position of the mouse when button released. So if there is a horizontal movement I can easily recognise. However it is problematic. Since I add listener to a movie clip, it obtain mouse x in bounds of that movie clip. What I mean is if you release button outside of the movie clip it does not work because event attached to it. Are there any turnarounds here?
m_c.addEventListener(MouseEvent.MOUSE_DOWN, StartPoint);
m_c.addEventListener(MouseEvent.MOUSE_UP, EndPoint);
function StartPoint(event:MouseEvent):void
{
initX = stage.mouseX;
}
function EndPoint(event:MouseEvent):void
{
lastX = stage.mouseX;
trace("drop ", lastX);
if(lastX < initX)
{
trace("goes left");
.
.
.
}
}

you need to addListener to stage, preferable in listener of MOUSE_DOWN
function StartPoint(event: MouseEvent) : void {
stage.addEventListener(MouseEvent.MOUSE_UP, EndPoint);
// another code
}

Related

mouseclick sometimes fails to work

I used an eventlistener for MouseEvent.CLICK. If I click once, it sometimes wont shoot, but when I click again it shoots. Is this a problem within flash or my mouse? It's strange. Here is the code for my fire event.
function fire(m: Event)
{
//reset the cooldown
//spawn a bullet
//set the position and the rotation of the bullet
b.rotation = turret.rotation;
b.x = turret.x;
b.y = turret.y;
//add the bullet to the parent object
parent.addChild(b);
}

Flash AS3 rollover and button with ClickTag

I have a banner with a ClickTag and a hover function.
My problem is that that the user can't click on the button because of the hover function.
My code is for the ClickTag:
knap1.addEventListener(MouseEvent.CLICK, ADFclicked);
function ADFclicked(event:MouseEvent) { AdfURLNavigator.navigateToUrl( AdfFlashVarsUtil.getParameter("clickTAG"), AdfFlashVarsUtil.getParameter("landingPageTarget")); }
And for the hover function:
var holder:MovieClip = new MovieClip();
btn.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
btn.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
btn.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
function mouseOverHandler(e:MouseEvent):void{
//creating a new tooltip instance
var tooltip:Tooltip = new Tooltip();
//we tell the holder to hold our tooltip
holder = tooltip;
//positioning the tooltip on the stage
holder.x = 190;
holder.y = 280;
//adding the tooltip to the stage
addChild(tooltip);
}
function mouseOutHandler(e:MouseEvent):void{
//we remove the holder when the cursor is outside our button
removeChild(holder);
}
function mouseMoveHandler(e:MouseEvent):void{
holder.x = 190;
holder.y = 280;
}
Can anybody help?
I would assume, not seeing the entire code, that btn object is covering knap1 object so you cannot click on anything that is beneath btn.
If you want to have hover function over whole banner try using MOUSE_LEAVE event to detect mouse leaving and them MOUSE_MOVE to track if mouse is back on flash object after leaving it. As for MOUSE_MOVE event you can add listener to stage to detect mouse movement without any additional containers.
Hey you are using mousevent for hover & click at one time. So better remove addeventlistener & write the mouseover in button as inline.

Need to move this Object in one direction

I am trying to move this seatBelt object just on the horizontal plane. I have posted what I did so far. it's not really working out because it seems like once I press the button the seat belt is only able to move towards the right side and MOUSE_UP doesn't work. can somebody guide me on how to go about doing this?
seatBelt.addEventListener (MouseEvent.MOUSE_DOWN, prsing);
seatBelt.addEventListener (MouseEvent.MOUSE_UP, lfting);
seatBelt.addEventListener (MouseEvent.MOUSE_MOVE, mving);
function prsing (e:MouseEvent){
posX= seatBelt.x ;
posY = seatBelt.y ;
mouseclick = 1;
}
function mving (e:MouseEvent) {
if (mouseclick == 1) {
seatBelt.x = mouseX ;
}
}
function lfting (e:MouseEvent){
seatBelt.x = posX;
seatBelt.y = posY;
mouseclick =0;
}
So your intended functionality is to be able to drag the seatBelt along the x-axis and on release for it to go back to its original position?
You need to change the MOUSE_UP and MOUSE_MOVE to listen to the stage rather than the seatBelt. This is because it is possible for you to release the button when it is no longer over the seatBelt and so the function never gets called. The stage however will receive the event.
stage.addEventListener(MouseEvent.MOUSE_UP, lifting);
stage.addEventListener(MouseEvent.MOUSE_MOVE, moving);
I am not sure where you are declaring the mouseX variable but if you are after dragging functionality alter the listener to:
function moving (e:MouseEvent) {
if (mouseclick == 1) {
seatBelt.x = e.stageX;
}
}
What you have mostly looks okay code-wise, except what I think is happening is that you are "mousing up" on the stage/somewhere off the seatBelt object. I'm guessing this as to make the object move you have it reacting to mouse moving...to which I am guessing you are moving the mouse all over the stage and off the seatbelt object at which point you are then releasing the mouse.
To check if this is the case, try releasing the mouse over your seatBelt object to see if your MOUSE_UP event does get triggered.
I'm guessing the behaviour you want is that you want to have the object stop moving at any point regardless of where the mouse is released. To do this, try adding the MOUSE_UP event listener to the stage:
this.stage.addEventListener (MouseEvent.MOUSE_UP, lfting);
Though, since you may not always want this listener to be active, perhaps only add it when the mouse is pressed down over the object by adding and removing the listener only as needed.
I've edited your code a bit to show what I mean and taken out the "mouseclick" boolean as it wouldn't really be needed to keep track of the mouse down/up event anymore:
seatBelt.addEventListener(MouseEvent.MOUSE_DOWN, prsing);
function prsing(e:MouseEvent){
posX= seatBelt.x ;
posY = seatBelt.y ;
this.stage.addEventListener(MouseEvent.MOUSE_MOVE, mving);
this.stage.addEventListener(MouseEvent.MOUSE_UP, lfting);
}
function mving(e:MouseEvent) {
seatBelt.x = mouseX;
}
function lfting(e:MouseEvent){
seatBelt.x = posX;
seatBelt.y = posY;
this.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mving);
this.stage.removeEventListener(MouseEvent.MOUSE_UP, lfting);
}
You can use the Sprite.startDrag method to do what you need. No MOUSE_MOVE listener is needed. Lookup the params if you need to: Sprite. I like to only have only one of the MOUSE_DOWN or MOUSE_UP listeners active at a time.
seatBelt.addEventListener (MouseEvent.MOUSE_DOWN, prsing);
function prsing (e:MouseEvent){
seatBelt.startDrag(false, new Rectangle(0,seatBelt.y,stage.width,seatBelt.y));
seatBelt.removeEventListener (MouseEvent.MOUSE_DOWN, prsing);
seatBelt.addEventListener (MouseEvent.MOUSE_UP, lfting);
}
function lfting (e:MouseEvent){
seatBelt.stopDrag();
seatBelt.addEventListener (MouseEvent.MOUSE_DOWN, prsing);
seatBelt.removeEventListener (MouseEvent.MOUSE_UP, lfting);
}

How do I stop a function of a parent movieclip when in a child of that parents?

Help!!
I have a piece of code on a mc that when the mouse is dragged it plays through that movie clip, giving a 360 spin of a product.
Inside this movieclip on different increments of the 360 spin i have child movieclips with various other animations to relate to each angle of the product.
exp..
Scene 1 > spinY_mc > AWComplete_mc
My code for the spin is written within the actions in scene1 and controls spinY_mc but once im in AWComplete_mc i do not want you to be able to drag the mouse and spin?
Im sure this is simple but im a noob at all this and am taking on a mammoth project!
Here is the code used on the movieclip (spinY_mc) I dont want this code to work when inside of its child mc (AWComplete_mc).
// Rotation of Control Body Y
spin_Y.stop();
spin_Y.buttonMode = true;
var spin_Y:MovieClip;
var offsetFrame:int = spin_Y.currentFrame;
var offsetY:Number = 0;
var percent:Number = 0;
spin_Y.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
spin_Y.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
function startDragging(e:MouseEvent):void
{
// start listening for mouse movement
spin_Y.addEventListener(MouseEvent.MOUSE_MOVE,drag);
offsetY = stage.mouseY;
}
function stopDragging(e:MouseEvent):void
{
// STOP listening for mouse movement
spin_Y.removeEventListener(MouseEvent.MOUSE_MOVE,drag);
// save the current frame number;
offsetFrame = spin_Y.currentFrame;
}
// this function is called continuously while the mouse is being dragged
function drag(e:MouseEvent):void
{
// work out how far the mouse has been dragged, relative to the width of the spin_Y
// value between -1 and +1
percent = (mouseY - offsetY) / spin_Y.height;
// trace(percent);
// work out which frame to go to. offsetFrame is the frame we started from
var frame:int = Math.round(percent * spin_Y.totalFrames) + offsetFrame;
// reset when hitting the END of the spin_Y timeline
while (frame > spin_Y.totalFrames)
{
frame -= spin_Y.totalFrames;
}
// reset when hitting the START of the spin_Y timeline
while (frame <= 0)
{
frame += spin_Y.totalFrames;
}
// go to the correct frame
spin_Y.gotoAndStop(frame);
}
I'm pretty sure you just want to stop the propagation of an event from the child so it doesn't make it out to the parent. If you add a listener for the event you want to block (lets say it's mouseDown you want to block).
child_mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownBlocker);
private function mouseDownBlocker(event:MouseEvent):void
{
event.stopImmediatePropagation();
}
The way events work is they start at the "deepeest" child which the mouse has a hit event with, then they "bubble" up through all the parents. By stopping the propagation you block the bubbling from occurring, so the parents never get the event.

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();
}