Displaying Pictures According to the Cursor Position

August 17th, 2010 in Flash | 4 Comments

In this easy tut, learn how to browse pictures contained in a movie clip according to the cursor position. The user will watch the human evolution by simply moving his mouse cursor horizontally.

View DemoDownload Source

1. Create a new flash file (Actionscript 3.0) and save it as src.fla.

2. Create a movie clip and put each one of your picture in a new frame. Set its registration point to the top left and give it an instance name of “pic_mc”.

3. Create an “actions” layer and with its first frame selected open the actions panel.
First add a stop() to the “pic_mc” movie clip to prevent if from looping through its frames and add ROLL_OVER and ROLL_OUT event listeners.

pic_mc.stop();
pic_mc.addEventListener(MouseEvent.ROLL_OVER, overHandler);
pic_mc.addEventListener(MouseEvent.ROLL_OUT, outHandler);

4. In the overHandler() function, we add a MOUSE_MOVE event listener that will call the changeFrame() function when the event will be triggered. In the changeFrame() function, we set the current frame of “pic_mc” according to the mouse position on the movie clip.

function overHandler(e:Event):void {
	pic_mc.addEventListener(MouseEvent.MOUSE_MOVE,changeFrame);
}

function changeFrame(e:Event):void {
	var frame : uint = Math.ceil(pic_mc.totalFrames * pic_mc.mouseX / pic_mc.width);
	pic_mc.gotoAndStop(frame);
}

5. In the outHandler() function, we simply remove the MOUSE_MOVE listener.

function outHandler(e:Event):void {
	pic_mc.removeEventListener(MouseEvent.MOUSE_MOVE, changeFrame);
}

6. Here’s the final code, test your movie to see it in action.

pic_mc.stop();
pic_mc.addEventListener(MouseEvent.ROLL_OVER, overHandler);
pic_mc.addEventListener(MouseEvent.ROLL_OUT, outHandler);

function overHandler(e:Event):void {
	pic_mc.addEventListener(MouseEvent.MOUSE_MOVE,changeFrame);
}

function changeFrame(e:Event):void {
	var frame : uint = Math.ceil(pic_mc.totalFrames * pic_mc.mouseX / pic_mc.width);
	pic_mc.gotoAndStop(frame);
}

function outHandler(e:Event):void {
	pic_mc.removeEventListener(MouseEvent.MOUSE_MOVE, changeFrame);
}
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Twitter


What you think ...

(4 Comments)

+ Add a Comment
  1. Majstor
    Aug 22nd, 2010

    Very clever action, but many thanks…

  2. saki
    Sep 8th, 2010

    nice useful Technique !!!!!

  3. saurav
    Sep 29th, 2010

    Its a nice flash workout but strange to look the computer age naked

  4. Alarmed» Blog Archive » cursor pictures
    Jun 8th, 2011

    [...] Displaying Pictures According to the Cursor Position | RiaCodes Aug 17, 2010 … In this easy tut, learn how to browse pictures contained in a movie clip according to the cursor … [...]

Leave a Comment

Name (required)

E-mail (required, will not be published)

Website