Skip to main content
February 5, 2010
Answered

How do I detect mouse direction and change frame?

  • February 5, 2010
  • 2 replies
  • 3465 views

Hi there,

I'm building a simple 360 degree image rotation effect using single frames and forward and back buttons. I'm new to AS3 and have managed to create functioning buttons... (so my code may be a bit odd!)

But now I also want to advance frames by clicking and dragging on the image itself (or on an invisible button sat on top of the image).

Although there are some threads here which have asked the same thing, I don't understand how they were answered. My code is below, and the file I am practising with is on:

https://download.yousendit.com/MVNjeFlWSWh6NE4zZUE9PQ

At the moment if you click on the image area it will advance but as there is no code detecting mouse direction, it just goes forward. Can anyone help me understand how to implement a simple direction detection and then use this to decided whether to advance or go backwards....thank you!!

stop();

// advance 1 frame on click

function forward(e:MouseEvent):void {

if (currentFrame ==15) {

gotoAndStop(1);

}else{

gotoAndStop(currentFrame + 1);

}

}

// go back 1 frame on click

function back(e:MouseEvent):void {

if (currentFrame ==1) {

gotoAndStop(15);

}else{

gotoAndStop(currentFrame - 1);

}

}

btn_fwd.addEventListener(MouseEvent.CLICK, forward);

btn_bck.addEventListener(MouseEvent.CLICK, back);

// change frame on mouse direction???

function MouseMove(event:MouseEvent):void {

{

if(event.buttonDown==true)

if (currentFrame ==15) {

gotoAndStop(1);

}else{

gotoAndStop(currentFrame + 1);

}

}

}


stage_btn.addEventListener(MouseEvent.MOUSE_MOVE, MouseMove);

This topic has been closed for replies.
Correct answer kglad

ah...

...well, I can't get it to change frame at all at the mo!


here's an almost identical thread i've answered:  http://forums.adobe.com/thread/570903?tstart=0

2 replies

February 5, 2010

There are several ways you can do this but here's the simplest one I can think of if you are using the "click and drag" method that you've mentioned. You don't actually need to use mouseMove at all with this method.

1) Record the current mouse position when you mouseDown.

2) The user drags to the left or right.

3) The user releases the mouse.

4) Record the "new" current mouse position when you mouseUP.

If(mouseDownPosition > mouseUPposition)

go right

else

go left

You can look up the API library on mouse positioning in AS3  (mouseX, mouseY)

http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/

Hope this helps~

February 5, 2010

Thank you both... I think I'm nearly there and have cleaned up my original code. I'm trying the click and drag method.

Here's some of my updated code... but I don't understand how to refer back to the recorded positions in order to direct the frame progression. I'm guessing that the bit in bold is where the code is wrong:

stage_btn.addEventListener(MouseEvent.CLICK, traceCoordinates);

function traceCoordinates(event:MouseEvent):void {

    trace(stage_btn.mouseX, stage_btn.mouseY);

{

if(event. mouseX > ???) {

nextFrame();

}else{

prevFrame();

}

}

}

kglad
Community Expert
February 5, 2010

with ihelpu's suggestion you're only going to be resizing when the mouse is released which probably not going to give a satisfactory result.  you can use dragging to determine resizing but, you'll still need a loop to repeatedly resize based on mouse position.

kglad
Community Expert
February 5, 2010

1.  usually, you would detect which 1/2 of the image the mouse is over and advance or move back a frame.

2.  you can use nextFrame() instead of gotoAndStop(currentFrame + 1);

3.  you can use prevFrame() to move back one frame.