The Flash and PHP Bible has been released! The book can be found on Amazon or wherever fine books are sold in your area.
The Flash and PHP Bible has a forum for quick support.
Create an image rollover using ActionScript 3. This effect is popular in galleries to demonstrate before/after views.
A common technique used in photo galleries is to allow a before and after preview of an image. This is achieved by preloading both images and when the user rolls over the affter, the before is displayed.
In this tutorial you will learn how to create this effect very simply using ActionScript, but the overall concept can be adapted to previous versions.
Start off by creating a new Flash file (FLA) and saving that. Then find two images to load, which in this case will be named "before.jpg" and "after.jpg" for simplicity.
Once the new file is created and the images are ready to be loaded, open the Actions panel and begin creating the code for this effect.
Create an array which will hold the image references, this is done so you can expand the example, but if you prefer you can skip the array and enter the image paths directly within the URLRequest.
var images:Array = ["before.jpg", "after.jpg"]
The next step is to create the image loaders for each image. These will simply be Loader instances
// image 1 var image1:Loader = new Loader(); image1.load(new URLRequest(images[0])); addChild(image1); // image 2 var image2:Loader = new Loader(); image2.load(new URLRequest(images[1])); image2.alpha = 0; addChild(image2);
As you can see, those blocks of code are very similar and could be converted into a function which would be easier to duplicate. Feel free to do that, but its certainly not required.
The next step is to attach the Event handlers for the mouse movements on the stage. In this case, you will assign a roll over and roll out event.
stage.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler); stage.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
The final bit of code is the creation of the event handlers. These functions are responsible for changing the alpha property on each of the image boxes, depending on whether the mouse is rolling over or off the image.
function mouseOverHandler(e:MouseEvent):void
{
image1.alpha = 0;
image2.alpha = 1.0;
}
function mouseOutHandler(e:MouseEvent):void
{
image2.alpha = 0;
image1.alpha = 1.0;
}
The last step is to save and test the file. You should see the image load in, and when you mouse over, the other image should appear. Mouse off and the original should re-appear. This example was basic, but should explain the overall concept. A good way to expand this would be to using the Tween class and create a subtle fade, rather than a sharp alpha change.
I hope you enjoyed this tutorial, here is the completed code for easy copying/pasting. Enjoy!
var images:Array = ["before.jpg", "after.jpg"]
// image 1
var image1:Loader = new Loader();
image1.load(new URLRequest(images[0]));
addChild(image1);
// image 2
var image2:Loader = new Loader();
image2.load(new URLRequest(images[1]));
image2.alpha = 0;
addChild(image2);
stage.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
stage.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
function mouseOverHandler(e:MouseEvent):void
{
image1.alpha = 0;
image2.alpha = 1.0;
}
function mouseOutHandler(e:MouseEvent):void
{
image2.alpha = 0;
image1.alpha = 1.0;
}
|
michael Mon May 19, 2008 5:34 am
plz give source file
|
|
Omar Tue May 20, 2008 12:20 pm
Thanks for tutorial :)
i learned something new :) i am still learning ActionScript 3 i hope we can see another tutorial :) |
|
mkeefe Wed May 28, 2008 9:13 pm
@michael - I will be uploading the source once I get a second.
@Omar - I will certainly be posting more tutorials :) |
|
Gerry Mon Jul 14, 2008 12:25 pm
@Michael. He gives the source right there in the code. All you have to do is put two images in the same location as your fla named before.jpg & after.jpg
That's it. No need for a source file. |
|
mkeefe Mon Jul 14, 2008 3:08 pm
@Gerry - Your indeed correct. I actually realized I don't even have source. Just coded it in the Flash editor, tested it, wrote about it and closed Flash. I tend to do this for "quick" tips.
|
|
Michael Fri Jul 25, 2008 6:33 pm
This worked great when the images are located in the same directory as the FLA. But, when I changed reference to an 'image' directory, nothing was displayed. What am I doing wrong. Referencing 'images/image1.jpg' in the Array does not work, but 'image1.jpg' does work. The same images are both local to the FLA and in the images directory.
|
|
mkeefe Sun Jul 27, 2008 9:52 pm
@Michael - Does Flash throw a loading error? Or simply not display the image?
|
|
awais Wed Apr 8, 2009 3:20 am
vaery nice tutorial!
|
|
Sarah Wed Apr 8, 2009 5:42 pm
what would you have to change in the code if you wanted the images to change without the mouse being placed over the image? just for the images to change automatically after 5 seconds for instance?
|
|
mkeefe Wed Apr 8, 2009 10:57 pm
@Sarah - In order to automate that process, simply create a timer and in the event handler, add the code that is normally run on mouse over. Let me know if you need a hand setting it up.
|
|
Sarah Thu Apr 9, 2009 6:52 am
Sorry im new to using AS3 so im not sure how to set up a timer. any help would be much appreciated! :)
|
|
M.E. Wed Jun 3, 2009 8:14 am
Thanks for the tutorial - I've been working with Flash for three years but haven't had much of a chance to really get into ActionScript (which is heresy, I know). I'm trying to rectify that.
But I do have a question - if I wanted to put MovieClips in instead of images, how would that work out in the coding? Essentially, I want an image, then when the mouse pointer moves over it, I want it to animate it (by increasing it's size slightly and maybe another effect or two). I have a basic picture in place and created a PNG cutout of the main item in the image that will grow in size when the mouse moves over it. I can create the MovieClip, I just am not sure how to integrate it. I'll work on it, maybe I can accidentally make it work. |
|
mkeefe Mon Jun 29, 2009 9:52 am
@Sarah - Here is a basic Timer introduction:
http://scriptplayground.com/tutorials/as/Basic-Timer-in-AS3/ @M.E. - You can simply load a SWF in place of the graphic, in fact you use the same loader. Then once the file is fully loaded you can use the play(); command to start the animation.
|
©2004 - 2009 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN