Get $500+ of the best After Effects files, video templates and music for only $20!

Exclusive Freebie: AS3 Image Revealer

Freebie Details
  • Platform: Flash
  • Program: Flash Professional CS3+

“Pinch, punch, first of the month”, “White Rabbits” – however you like to mark the dawn of a new month, we like to bring them in with freebies :) This month, we have a great little AS3 Image Revealer from Carlos Yanez.

Image Credits:


AS3 Image Revealer

Take a look at the usage examples:

Here is an example revealing a black and white version of an image:

This can be very useful in before and after comparisons:


Features

Using this base file you can:

  • Change the before and after (bottom-top) images
  • Use any Display Object for the effect
  • Modify the slider graphic

Usage

Open the ImageRevealer.fla and edit the MovieClip in the stage, use the Timeline to determine where to place every image and adjust the Mask size to fill the image.

Open the Main.as file and edit the highlighted lines:

package
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public final class Main extends Sprite
	{
		var frame:int = 11; //This is the middle frame

		public final function Main():void
		{
			img.gotoAndStop(11); //Change the frame to show half top, half bottom
			arrows.buttonMode = true;
			addListeners();
		}

		private final function addListeners():void
		{
			arrows.addEventListener(MouseEvent.MOUSE_DOWN, initDrag);
			arrows.addEventListener(MouseEvent.MOUSE_UP, termDrag);
			img.addEventListener(MouseEvent.MOUSE_UP, termDrag);
		}

		private final function initDrag(e:MouseEvent):void
		{
			/* Change the y value (353) to the y of your "arrows" MC
			   Change the width value (300) to the width of your image drag area */

			arrows.startDrag(true, new Rectangle(0, 353, 300));
			stage.addEventListener(MouseEvent.MOUSE_MOVE, revealImage);
		}

		private final function termDrag(e:MouseEvent):void
		{
			arrows.stopDrag();
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, revealImage);
		}

		private final function revealImage(e:MouseEvent):void
		{
			/* This is a tricky part, the default frames in the MC are 20
			   you'll need to calculate the constant according to your frames
			   and image size to reveal the image correctly */

			img.gotoAndStop(Math.floor(arrows.x * 0.07)); //0.07 is the constant here
		}
	}
}

Add Comment

Discussion 6 Comments

  1. miels says:

    When you click on on an arrow, the arrows move a little in the desired direction, but the border between the image doesn’t.

    This is caused by passing true as the first parameter to the startDrag() function.
    It aligns the mc, so its center is at the mouse position, which doesn’t update the revelation of the picture, because this only happens when the mouse moves.

    What’s the point of making everything final?

  2. facundo says:

    Are you serious?, so you have to open the .fla to embed the before and after image?. And using timeline?…and please do not tell me this is for beginners… You should contribute to spread the word about the benefits and power of the flash platform, specially these days when everyone seems to claim it is dead…this kind of articles are just pointless and worst of all, a guide of bad practices for anyone interested in jumping into AS3.

  3. drotar says:

    i just made a sample 1200 x 650 and cannot figure out how to center the arrows in the middle of the page. I tried doing it on the as file, no luck, then i tried moving them physically on the fla file, ouch…

    little help from anyone, please…. what am i missing here?

    drotar

  4. drotar says:

    i figured it out for anybody interested. please feel to drop me an email.

  5. toppet says:

    Great work! I’d like to tweak it a bit though…

    Anyway you can make it scroll back automatically after dragging?

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.