- Home
- Tutorials
- Flash
- Intermediate
- AS3 Photo Viewer Tutorial
AS3 Photo Viewer Tutorial

Transition Function
Amen KAMALELDINE
Amen is an industrial informatics engineer. he studied the first four years of his education in Faculty of Engineering in Lebanon. He went to France to Continue his studies by doing the fifth year of engineering and the master TIS (Technologie Information et Système) in the same time at UTC (Université de Technologie de Compiègne). He also work as a freelancer in flash development field. He is always interessted in the new multimedia technologies.
View all articles by Amen KAMALELDINENow we assign to the holder of all the images an ON_ENTERFRAME event and in it we perform some calculation to update the holder and the imgHolder postion and scale to have a nice transition between the images.
Code
private function dispatchOnEnterFrame(e){
//if the diffrence=0 do nothing
if(diffX==0)return;
//else update the sliding of our image holder with easing
imgHolder.x+=(finalX-imgHolder.x)/6;
//and also update the current X aside but with a greater easing coef to have some retard in time
//between the 2 transition
cX+=(finalX-cX)/12;
//generate a percentage
var per=100*(1-(finalX-cX)/diffX);
//and ease the current percentage depending on the current generated percentage
//this will assure the smoothing and the continus animation
percentage+=(per-percentage)/12;
//update the scaling to have the zoom in out effect
holder.scaleX=1-(.5)*Math.sin(Math.PI*percentage/100);
holder.scaleY=1-(.5)*Math.sin(Math.PI*percentage/100);
}
first of all if the difference between the selected image and the position of the imgHolder which is a child of the holder and contain the images IS Zero then do nothing. else we perform the easing Out equation on the imgHolder we also do the same think on a fake variable called cX the current is but with a greater coef to have the same result but in a slower time after this we calculate the percentage in an easing Out equation too, so we never have a discontinuty in our transition and at the end we update the scale wich goes between 1 if the percentage is 0 or 100 and min .5 if the precentage is 50.
all the rest in the class of our movie is some basic stuff like the drawing of the background function and the ON_CLICK event of the menuItems. If you have any questions regarding this please go to the forums.
Thanks for reading, the files are attached below. Have fun ;)
Spread The Word
Attachments
8 Responses to "AS3 Photo Viewer Tutorial" 
|
said this on 26 Mar 2008 6:44:30 PM CST
Leaving an ENTER_FRAME listening seems to eat 20-30% of the CPU while doing nothing. CPU usage seems to improve to under 3% when it is removed. Of course don't forget to addEventListener in the appropriate places, such as when the stage is resized, or the menu is clicked.
private function dispatchOnEnterFrame(e) { //if the diffrence=0 do nothing if (Math.abs(finalX-cX)<1) { trace("OnEnterFrame removed"); holder.removeEventListener(Event.ENTER_FRAME,dispatchOnEnterFrame); return; } //else update the sliding of our image holder with easing imgHolder.x+=(finalX-imgHolder.x)/6; //and also update the current X aside but with a greater easing coef to have some retard in time //between the 2 transition cX+=(finalX-cX)/12; //generate a percentage var per=100*(1-(finalX-cX)/diffX); //and ease the current percentage depending on the current generated percentage //this will assure the smoothing and the continus animation percentage+=(per-percentage)/12; //update the scaling to have the zoom in out effect holder.scaleX=1-(.5)*Math.sin(Math.PI*percentage/100); holder.scaleY=1-(.5)*Math.sin(Math.PI*percentage/100); } |
|
said this on 29 Aug 2008 2:56:21 PM CST
Hey. I kinda new to AS3, it does show how these files are suppose to be setup.. pretty confusing, maybe cuz im new to this. so question is. do i put all this code in one external file? or do i break up the code in couple files?
|
|
said this on 01 Sep 2008 9:14:29 PM CST
Andre, Hey. Download the PhotoViewer Tutorial.zip under Attachments above. The files are all together in the .zip file.
Good Luck, |
|
said this on 12 Oct 2008 9:23:24 AM CST
Excellent tutorial.
I'm new with AS3 and i have a question : i want to modify your script in order to : - remove the the 1-n buttons, - show the pictures one by one with a 2 seconds interval. remove the buttons is pretty easy ;-) But (if i don't make mistakes) i don't know how to put the setInterval to the showImageAt() function. Can you explain me to do this ? |
|
said this on 12 Nov 2008 11:17:27 AM CST
This is a great tutorial. Being that I'm new to this, I have a simple question. How is this initially called? I don't see any code that invokes the .as file, yet it works. Any help on the initial call would be great.
|
|
said this on 20 Nov 2008 5:50:07 PM CST
I have adapted parts of your excellent tutorial as the basis for an agency portfolio, and I wonder if you have given any thought to how you might incorporate video into the mixture? I'd be very interested to hear any thoughts or input you might have to offer. I sincerely appreciate the work you put into producing this tut. Thanks again.
Ken |
|
said this on 05 Dec 2008 8:42:13 PM CST
hi,
I'm also new to this and have an (hopefully) easy question.... How can I add a link to each of the images? I would like to link to external web sites from each image. Thanks!!! |
|
said this on 08 Dec 2008 11:35:09 PM CST
Great transition effects! I am trying to embed your movie in another one. I keep getting the error message "TypeError: Error #1009: Cannot access a property or method of a null object reference. at PhotoViewer()". I am using the following code to load the movie:
import flash.display.*; import flash.net.URLRequest; var rect:Shape = new Shape(); rect.graphics.beginFill(0xFFFFFF); rect.graphics.drawRect(100, 41, 741, 501); rect.graphics.endFill(); addChild(rect); var ldr:Loader = new Loader(); ldr.mask = rect; var url:String = "PhotoViewer.swf"; var urlReq:URLRequest = new URLRequest(url); ldr.load(urlReq); addChild(ldr); The error occurs at the first import statement in your PhotoViewer.as file. Any ideas on what my problem is ? Thanks. |


Author/Admin)