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.
Update: More advanced class based version now available.
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.
|
|
Nancy Mon Jul 13, 2009 10:18 am
This is a great tutorial. Thanks so much!!
I am also new to AS3. I am trying to use the rollover effect, but with multiple image rollovers. Right now, when I rollover any of the individual images, they are all changing. Any help would be greatly appreciated! |
|
mkeefe Mon Jul 13, 2009 11:57 am
@Nancy - You would need to create "mouseOverHandler" and "mouseOutHandler" functions for each image as the image reference is located within those functions. Alternatively you could create a custom class that holds the reference to the image and applies the proper image swapping. I could write a tutorial on this if needed.
|
|
Nancy Mon Jul 13, 2009 12:06 pm
That would be great if you could and would help so much! I'd really appreciate it. Seeing how I under the concepts of what you're saying, but not as sure how to actually write the functions as this early of a stage.
|
|
Nancy Mon Jul 13, 2009 12:16 pm
Here is the code so far as I've tried to solve the problem:
var images:Array = ["dot_door.png", "door.png", "dot_sink.png","sink.png","dot_inside.png","inside.png","dot_outside.png","outside.png"] // door tip var image1:Loader = new Loader(); image1.load(new URLRequest(images[0])); image1.x = 390.4; image1.y = 175.5; addChild(image1); // door callout var image2:Loader = new Loader(); image2.load(new URLRequest(images[1])); image2.alpha = 0; image2.x = 390.6; image2.y = 135.5; addChild(image2); // sink tip var image3:Loader = new Loader(); image3.load(new URLRequest(images[2])); image3.x = 601; image3.y = 152.8; addChild(image3); // sink callout var image4:Loader = new Loader(); image4.load(new URLRequest(images[3])); image4.alpha = 0; image4.x = 411; image4.y = 78; addChild(image4); // inside tip var image5:Loader = new Loader(); image5.load(new URLRequest(images[4])); image5.x = 332.9; image5.y = 340.9; addChild(image5); // inside callout var image6:Loader = new Loader(); image6.load(new URLRequest(images[5])); image6.alpha = 0; image6.x = 333; image6.y = 208; addChild(image6); // outside tip var image7:Loader = new Loader(); image7.load(new URLRequest(images[6])); image7.x = 379.9; image7.y = 296.9; addChild(image7); // outside callout var image8:Loader = new Loader(); image8.load(new URLRequest(images[7])); image8.alpha = 0; image8.x = 380; image8.y = 164; addChild(image8); stage.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler); stage.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler); stage.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler2); stage.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler2); stage.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler3); stage.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler3); stage.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler4); stage.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler4); //door function mouseOverHandler(e:MouseEvent):void { image1.alpha = 0; image2.alpha = 1.0; } function mouseOutHandler(e:MouseEvent):void { image2.alpha = 0; image1.alpha = 1.0; } //sink function mouseOverHandler2(e:MouseEvent):void { image3.alpha = 0; image4.alpha = 1.0; } function mouseOutHandler2(e:MouseEvent):void { image4.alpha = 0; image3.alpha = 1.0; } //inside function mouseOverHandler3(e:MouseEvent):void { image5.alpha = 0; image6.alpha = 1.0; } function mouseOutHandler3(e:MouseEvent):void { image6.alpha = 0; image5.alpha = 1.0; } //outside function mouseOverHandler4(e:MouseEvent):void { image7.alpha = 0; image8.alpha = 1.0; } function mouseOutHandler4(e:MouseEvent):void { image8.alpha = 0; image7.alpha = 1.0; } stop(); |
|
mkeefe Mon Jul 13, 2009 5:27 pm
@Nancy - I have written a classed based version of this concept, which can be viewed here. I hope it helps.
http://scriptplayground.com/tutorials/as/Dynamic-Image-RollOver-using-Classes/ |
|
MarkM Tue Jul 14, 2009 2:38 pm
This is nice tutorial, but I'm looking for something a little different and have searched high and low... maybe you can help.
I want to place a group of thumbnails at the bottom of my stage and when I roll over them (not click but just roll over), I want the full size image to appear above the thumbnails. I've seen lots of examples that allow to do this if you click on a thumbnail, but nothing that lets you just roll over. To me, the difference isn't that great, but my client is adamant that they don't want to click. Thanks. |
|
Breck Mon Jul 27, 2009 6:38 pm
This is exactly what I have been looking for!
Is there a way to automatically resize an image to fit within the stage dimensions that are set in the flash file, or vice versa? (resize the stage to fit the image). Thank you! |
|
mkeefe Mon Aug 3, 2009 10:09 am
@Breck - That would only be possible if you used Javascript to watch the browser window and fire an event to resize the stage.
|
©2004 - 2010 scriptplayground | Privacy Policy | Legal
Validate Site: XHTML CSS | Designed by: Matthew Keefe of mkeefeDESIGN