Create an iPhone-like Flip Effect in Flash Using ActionScript 3.0

Apr 6th in Effects by Carlos Yanez

In this tutorial, we'll recreate the Flip Effect used in the iPhone's UI using Flash CS4 and ActionScript 3.0.

PG

Author: Carlos Yanez

Carlos is a Web/Logo Designer, Flash Developer with passion for great designs and cool applications.

Final Result Preview

Let's take a look at the final result we will be working towards (click on the 'i' button):

Step 1 - Brief Overview

Here's basically what we're going to do. We'll need two Display Objects to flip, one in front that will be on the stage and visible at the beginning, and one that we are going to reveal when the effect is finished; this Object will be in the Library. In this example I'm using MovieClips, but you can use any of the Display Objects (Button, Component, Bitmap, Sprite, Shape, etc.).

We'll create an ActionsScript 3 Class for the effect, then some code in the .fla to customize the effect as we want.

Step 2

Create a new ActionScript 3.0 document (File > New...). It may be necessary to use the Flash CS4 version; we're going to export for Flash Player 10 due to the rotationY property being unavailable in older versions. There are a few tricks to get Flash CS3 to export Player 10 contents, you can search the web about doing so if you don't have CS4.

””

Step 3

Set the Stage size to 320 x 480px (that's the iPhone resolution but you can use any size you want), then set a background color. I'm using #111111.

””

Step 4

Rename the first layer to UI, then create another one and name it Code. Block the Code layer to avoid placing unwanted objects on it.

””

Step 5

Start creating your First View. This is the view that will be seen by default, you can use and draw anything you want.

””

Step 6

Convert it to a MovieClip (F8) and set its instance name to firstView.

””

Step 7

Create the Second view. This is the view that will be visible when the effect is finished.

””

Step 8

Convert it to a MovieClip, name it SecondView and check the Export for ActionScript checkbox (remember that this view won't be on stage, we'll call it from the Actions Panel).

””

Step 9

Now we have to create an Object which activates the effect; a Button will do the job. Create a button, set its position and name it infoButton. This button will be in the First View. In my example i've created the button inside the FirstView MovieClip. You can put it wherever you want, just remember where it is so you don't get confused in the code.

””

Step 10

In the Second View, create another button and name it doneButton. This button will flip the view back.

””

Step 11

Save your work and let's get into the code!

I'm assuming you already have a basic understanding of ActionScript 3.0. If you have a keyword specific doubt please refer to the Flash Help.

Create a new ActionScript file (File > New...).

Step 12 - Importing Required Classes

Now that you have your AS File ready, start writing:

package Classes
{
/* Import required classes for animation */

import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.Strong;

/* Import required classes */

import flash.display.*;
import flash.utils.Timer;
import flash.events.*;

The first three classes handle all the animation we will be using on the effect. In this case, I'm using a Strong easing to get a more iPhone-like flip, but you can modify this to get a different effect.

The other three classes handle the display elements, the Timer object that we'll use to check the progress of the animation and the last one handles any events that we may use.

Step 13 - Extending the Class

//We need to extend the class so we can use the addChild() method.

public class Flip extends Sprite
{

Extending the Sprite class will make our class inherit all of the methods, properties and functions the Sprite has. In this case we use it to get access to the addChild() method.

Step 14 - Declaring Variables

private var firstView:DisplayObject;
private var secondView:DisplayObject;
private var initFromSide:String;
private var duration:int;
private var flipTween:Tween;
private var addSecondView:Boolean; //This is used when the Second View IS already on stage and not in the Library, just in case.
private var added:Boolean = false;
private var timer:Timer = new Timer(1);

Declare variables to use outside the main function (flip), they get their values from the 'constructor function' and have self-explanatory names.

Step 15 - The Constructor Function

The constructor function represents the class containing the template from which new object instances are created. This is the function we need to use to call the class.

/* Flip starter function, with some parameters so you can customize your Flip like you want, remember these are the same as the variables */

public function Flip(frontObject:DisplayObject, backObject:DisplayObject, timeInSeconds:int, flipFromSide:String, secondViewIsInLibrary:Boolean):void
{
firstView = frontObject;
secondView = backObject;
duration = timeInSeconds;
initFromSide = flipFromSide;
addSecondView = secondViewIsInLibrary;

animateFirstView(); //This function animates the first part of the flip.
timer.addEventListener(TimerEvent.TIMER, checkFlip); //We check if it's time to animate the Second View
}

Step 16 - The Animate Function

 private function animateFirstView():void
{
flipTween = new Tween(firstView,"scaleX",Strong.easeIn,1,0.5,duration/2,true);//This lines are in charge of the scale animation,
flipTween = new Tween(firstView,"scaleY",Strong.easeIn,1,0.5,duration/2,true);//They reduces the view to the half of its size

 if (initFromSide == "left") //If the animation starts from left, we animate from the left!
{
flipTween = new Tween(firstView,"rotationY",Strong.easeIn,0,90,duration / 2,true);//This is a key line, it modifies the rotationY property to make the flip
timer.start();// We start the timer to check when we can change the MovieClips
}
else if (initFromSide == "right")
{
flipTween = new Tween(firstView,"rotationY",Strong.easeIn,0,-90,duration / 2,true);
timer.start();
}
}

This function is in charge of animating the First View. It reduces the size of the First View and checks the side parameter to start the animation.

Step 17 - Checking For The Right Position

This function will check the rotationY property to control the animation of the Second View.

 private function checkFlip(e:TimerEvent):void
{
if (initFromSide == "left" && firstView.rotationY >= 90)//If the animation was started from left and the First View is ready to switch
{
timer.stop(); //We stop checking when to switch views

 firstView.visible = false; //Hides the First View
 if (addSecondView && ! added)//If the Second View needs to be added from Library and has not been added
{
secondView.x = firstView.x; //Sets Second View position
secondView.y = firstView.y;

addChild(secondView);//Add Second View
added = true;//Set added to true to avoid adding a lot of Second Views
}
else
{
/* If Second View is in stage */

 secondView.x = firstView.x;
secondView.y = firstView.y;
}
 animateSecondView();//Animates Second View, the same way the first one is animated, hides the Second View and unhides the First View when done
}
else if (initFromSide == "right" && firstView.rotationY <= -90)
{
timer.stop();

 firstView.visible = false;
 
 if (addSecondView && ! added)
{
secondView.x = firstView.x;
secondView.y = firstView.y;
 addChild(secondView);
added = true;
}
else
{
/* If Second View is in stage */

 secondView.x = firstView.x;
secondView.y = firstView.y;
}

 animateSecondView();
}
}

Step 18 - Animating the Second View

This function animates the Second View. It's almost the same as the first animation function.

private function animateSecondView():void
{
flipTween = new Tween(secondView,"scaleX",Strong.easeOut,0.5,1,duration/2,true);
flipTween = new Tween(secondView,"scaleY",Strong.easeOut,0.5,1,duration/2,true);

if (initFromSide == "left")
{
flipTween = new Tween(secondView,"rotationY",Strong.easeOut,-90,0,duration / 2,true);
firstView.visible = true;
}
else if (initFromSide == "right")
{
flipTween = new Tween(secondView,"rotationY",Strong.easeOut,90,0,duration / 2,true);
firstView.visible = true;
}
}
}
}

Step 19

Save your ActionScript file as 'Flip.as'.

””

Step 20 - Using the Class

In your Flash file, open the Actions Panel and write:

import Classes.Flip; //Imports the class, you have to change 'Classes' with your own classes folder.
var flipped:Boolean = false; //We use this variable to know if the First View was already flipped.
var secondView:SecondView = new SecondView(); //Creates a SecondView Object, the one in the Library
var flip:Flip; //This is an instance of our Flip class

function callFlip(e:MouseEvent):void
{
if (! flipped) //If the FirstView isn't already flipped
{
firstView.infoButton.visible = false; //Hides the button so the user can't repeat the effect when its flipping.
flip  = new Flip(firstView, secondView, 2, "left", true); //This is the main line, it calls the class indicating wich MovieClips flip, the time the effect will take, wich side to start from, and if the second MovieClip is in the Library.
addChild(flip); //We have to add the class to get the addChild() method to work, this is a very common problem when using addChild() inside a class, now you know! ;)
flipped = true; //Set flipped to true to know that the First View was already animated
secondView.doneButton.visible = true; //Unhide the buttons so we can use them again
secondView.buttonBar.visible = true; //This is just the upper bar in the Second View
}
else if (flipped) //If flipped is true it's time to flip the secondView, you can set new parameters to get a totally diferent animation!
{
secondView.doneButton.visible = false;
flip = new Flip(secondView, firstView, 2, "right", true);
addChild(flip);
flipped = false;
firstView.infoButton.visible = true;
secondView.buttonBar.visible = false;
}
}

firstView.infoButton.addEventListener(MouseEvent.MOUSE_DOWN, callFlip); //Adds the listeners to the buttons
secondView.doneButton.addEventListener(MouseEvent.MOUSE_DOWN, callFlip);

Step 21

Save your work and check that everything is all right. Take a look at the instance names, test your code, search for errors, or maybe even alter the graphics.

Conclusion

Test your movie to see this cool effect in action!

Make sure that everything works as you expected; if not get back to the code or the .fla to take a look. If you can't find the error, you can Debug your movie by pressing Shift + Command + Return, or going to Debug > Debug Movie.

Remember: you have a lot of options to make the effect just like you want. Play with the parameters, the Display Objects and if there's something you think is missing, you can always edit the class!

I hope you liked this tutorial, thanks for reading!


Enjoy this Post?

We'd love your vote!

User Comments

( ADD YOURS )
  1. PG

    metixxd April 6th

    Good Job, thanks!

    ( Reply )
  2. PG

    Richard S Davies April 6th

    Nice, glad to see more AS3 tuts here

    ( Reply )
  3. PG

    Vincent D'Amico April 6th

    nice job!

    ( Reply )
  4. PG

    Stoyan April 6th

    Never use the built-in Tween Engine. There’re alot faster alternatives.

    ( Reply )
    1. PG

      Vincent D'Amico April 7th

      you should post your tutorials than =(.

      ( Reply )
  5. PG

    Pedro April 6th

    Great, could be smoother though!

    ( Reply )
  6. PG

    Pierre Laveklint April 6th

    Might want to use smoothing, which would result in better quality, now it looks a bit jagged…

    ( Reply )
  7. PG

    nuinosis April 6th

    Great tut man, many thanks!

    ( Reply )
  8. PG

    Clemente G April 6th

    Nice tut, for a smoother effect, I’d use Tweening Engine instead of the built in Tween Class.

    Cool effect tho

    ( Reply )
  9. PG

    Philip April 6th

    its the frame rate its kept at 12 :)

    ( Reply )
    1. PG

      nbvvb April 14th

      yeah

      ( Reply )
      1. PG

        Yes Man May 11th

        yeah

      2. PG

        tato May 13th

        yeah

      3. PG

        Randall July 13th

        Yeah?

  10. Damn, that’s nice! ;D
    But there is not layer styles in CS3. :’(

    ( Reply )
  11. PG

    MSFX April 6th

    Nice to see CS4 tuts coming out :) Should have put the framerate up and used greenSock or tweener but concepts still the same, nice effect :)

    ( Reply )
  12. PG

    Zach Dunn April 6th

    Thank you SO much for using Actionscript 3 AND putting it in an external file.

    ( Reply )
  13. PG

    iseethings April 6th

    super cool effect!

    ( Reply )
  14. PG

    lawrence77 April 6th

    Very simple! ;)
    thanks

    ( Reply )
  15. PG

    P April 6th

    I’m such a newbie on flash. i got it til the ‘firstview’ thing. couldnt continue cos dunno how to make the secondview.

    ( Reply )
  16. PG

    MorayWeb April 6th

    Nice article, it’s brought me one step closer to attacking Flash properly!

    ( Reply )
  17. PG

    RolsRice April 7th

    That looks really delicious! But, a blur effect during flip will be AWESOME!

    ( Reply )
  18. PG

    crativegroup bonn April 7th

    thank you, very much for this nice tutorial…

    greetings from germany

    ( Reply )
  19. PG

    ORiOn April 7th

    You should include demos. :/

    ( Reply )
    1. PG

      Le Marquis April 7th

      the demo is there! click on the (i) symbol in the preview

      ( Reply )
      1. PG

        ORiOn April 7th

        Ohh sorry, i didn’t see it. xD

  20. PG

    Le Marquis April 7th

    Not a bad tutorial. Although I prefered the tutorial by Lee Brimlow for thye 3D flip.
    because it’s less & easier to code. http://gotoandlearn.com/play?id=91

    ( Reply )
  21. PG

    RUGRLN April 7th

    Wicked…..Loved it!!

    ( Reply )
  22. PG

    Adrien April 7th

    Nice tut ! Thanks for sharing

    ( Reply )
  23. PG

    Philo April 7th

    Nice Tutorial!
    Higher frame rate would look much better! :)

    ( Reply )
  24. PG

    Diego SA April 7th

    Like MAC OS X Dashboard effect…

    ( Reply )
  25. PG

    Isabella April 7th

    Any chance you can save the file for cs3 ? Or does it not work?

    ( Reply )
  26. PG

    Sirwan April 7th

    I dont understand… is this supposed to work on the iPhone/iPod touch.?

    ( Reply )
  27. PG

    Danny April 7th

    like this effect.. fair play mate

    ( Reply )
  28. PG

    taha April 9th

    beautiful, looks so lovely !!

    ( Reply )
  29. PG

    Johnny April 9th

    I keep getting an error when I publish.

    1119: Access of possibly undefined property rotationY through a reference with static type flash.display:DisplayObject.

    Any suggestions?

    ( Reply )
    1. PG

      Steffen April 12th

      Ah the infamous “1119: access of possibly undefined property (blablabla)”

      This error is bull****….I had the same error yday in another project, and it took me several hours to figure out that it was a “simple” text reference error.

      In my opinion its one of the bigger flaws of Flash, but thats a whole other discussion ;)

      Without looking at ur code it seems like you adressed a “Y” property to an object(movieclip/sprite etc) but without setting the type of object, e.g.
      wrong = var myObject = myObject.y = 30;
      correct = var myObject:Sprite = new Sprite;
      myObject.y = 30;

      But take a look at this link http://curtismorley.com/2007/06/13/flash-cs3-flex-2-as3-error-1119/

      I strongly recommend you bookmark this site, cause he’s descriping almost all types of errors.

      ( Reply )
    2. PG

      Jelle Vervloessem June 11th

      Just set your publish settings to flashplayer10…

      ( Reply )
  30. PG

    Shyam Balu April 9th

    The performace is pretty terrible. Might wanna use another tweening engine.

    ( Reply )
    1. PG

      David June 6th

      That’s because he only used 12 FPS. Try with 30 FPS and you’ll see the difference.

      ( Reply )
  31. PG

    likwidmonster April 11th

    I, am assuming it is possible to use flash CS3 as well to create this as long as I use actionscript 3. Is that correct?

    ( Reply )
  32. PG

    Bill Labus April 11th

    Nice tutorial, though that’s actually more the zoom effect on the iPhone, the one you see when you launch an app or return to the home screen. The flip effect that’s used to show options/settings (such as for Weather or Stocks) is actually an animation of the screen ‘flipping’ 180 degrees to show the back side of it.

    Cool tutorial nonetheless, though.

    ( Reply )
  33. PG

    Luis Eduardo April 16th

    Nice tutorial for flash, i would try something different but the result itll be different.
    thanks

    ( Reply )
  34. PG

    coofen April 22nd

    1119: Access of possibly undefined property rotationY through a reference with static type flash.display:DisplayObject.

    1119: Access of possibly undefined property rotationY through a reference with static type flash.display:DisplayObject.

    ( Reply )
  35. PG

    kanedogg April 25th

    Dude i like the tutorial but you should mention its a method straight off the FLASH KINGS blog!! Lee brimelow’s site :)

    ( Reply )
    1. PG

      Ian Yates April 26th

      It’s probably worth mentioning that this tut bears little or no resemblance to Lee’s screencast and that Carlos’ methods and approach are entirely his own.. It’s easy to see why you thought this though ;-)

      ( Reply )
      1. PG

        Matt October 26th

        I guess better luck next time kanedogg ?

  36. PG

    CgBaran Tuts April 27th

    Excellent work thanks

    ( Reply )
  37. PG

    Aldis May 13th

    Really nice tutorial, thanks for it…

    Bet I have a one problem, when I run movie, it shows me an error: TypeError: Error #1010: A term is undefined and has no properties.at untiteled_fla::MainTimeline/frame1()

    Maybe someone could help?

    ( Reply )
  38. PG

    AS3 Newb May 14th

    Is it possible for someone to convert the source to CS3? Also would this work with Dynamic text in the views? or would those treat the flip in a different way

    ( Reply )
  39. PG

    sudaka May 15th

    Hello Master THANKS a LOT for your tutorial…….. I work for a CDROM and that’s very interesanté….

    HABLAS ESpagñol?????????

    Muchas gracias mano y me alludara bastante estoy pasando lentamente al AS3 saludos desde BELGICA.

    (YouAretheBest as MovieClip).gotoAndPlay(”NextTutorial”);

    ( Reply )
  40. PG

    Brad Sedito May 26th

    Thanks for the tutorial, great work.

    However, I saw a lot of people commenting on the lagging/jagged themes, so I went out and tried to incorporate a nicer tween engine (GS TweenMax) into the fla and as file, but had no luck. After an hour or two of tinkering around with it, I found an extremely easy solution to the tween problems.

    In the main FlashTutsFlipEffect.fla file, in flash (I use CS4 but i’d imagine it sti works the same with CS3, and change the frames per second to 40 (or even higher). It wasn’t jagged because the built in tweener is subpar, it was because the movie wasnt giving enough FPS to the tweens.

    Hope that helps anyone!

    Brad

    ( Reply )
  41. PG

    sudaka June 6th

    Hey Carlos Yanez ….. just a little question PLEASE

    Maybe you know, why the text in this effect (flip like iPhone) are not very clear…. my text axis are number exacts ….by examples x=100 y=100 ????????????

    I try to find the problem but …… i don’t find…. can you help me PLEASE???

    Obrigado

    ( Reply )
  42. PG

    sudaka June 12th

    Hey Carlos Yanez …. I find the solution for the probleme with the TEXT in your effect transtition (flip like iPhone)… Normaly the MovieClip (and sure the TEXT) become blurry … because we use the 3D objects property….

    My solution is ::::::: just ad the folow code in::::

    private function animateSecondView(){…….
    flipTween = new Tween (secondView, “rotationY”, …etc etc etc);

    //ad a function twReset when the tween is finisch //////////
    flipTween.addEventListener(TweenEvent.MOTION_FINISH, twReset);
    ……………………………………………………………..
    }

    private function twReset(e:TweenEvent):void
    {
    secondView.transform.matrix3D=null;
    secondView.x = firstView.x;
    secondView.y = firstView.y;
    }

    PS:: is the .transform.matrix3D=null;….. will reset the 3D object…..
    I hope this code will help someone

    Merci beaucoup
    muchas gracias

    ( Reply )
  43. PG

    matt June 16th

    nice tutorial,

    is this effect possible with a navigation bar and more than 2 pages?

    thx in advance, and keep up the good work

    ( Reply )
  44. PG

    MexiChriS August 2nd

    Okay, if anyone is wanting to learn to develop apps for the iPhone, please VOTE for them and get TUTs to make it happen!!! Vote for iPhone site/tutorials bellow! ….Maybe repost this somewhere yourself, to make it happen sooner, think about it!!!

    iPhone Tuts Site:
    http://psdtuts.uservoice.com/pages/2999-vectortuts-site-suggestions-and-feedback/suggestions/220438-iphone-tuts-site

    Learn To Code For iPhone:
    http://psdtuts.uservoice.com/pages/2997-nettuts-tutorial-suggestions/suggestions/155081-learn-to-code-for-iphone

    DEVTuts+:
    http://psdtuts.uservoice.com/pages/2997-nettuts-tutorial-suggestions/suggestions/245744-devtuts-

    ( Reply )
  45. PG

    Joker August 30th

    Ugh, I liked! So clear and positively.
    Thank you

    ( Reply )
  46. PG

    Zoran September 5th

    Hello,
    Super post, Need to mark it on Digg
    Zoran

    ( Reply )
  47. PG

    Joker September 8th

    Everything dynamic and very positively! :)
    Joker

    ( Reply )
  48. PG

    Nadine September 11th

    Hi, Not sure that this is true:), but thanks for a post.
    Nadine

    ( Reply )
  49. PG

    Hobosic September 15th

    Ugh, I liked! So clear and positively.
    Hobosic

    ( Reply )
  50. PG

    Dirnov September 18th

    Hi,
    Not sure that this is true:), but thanks for a post.

    ( Reply )
  51. PG

    Vikrant September 19th

    Hello,

    Thanks so much for this. I got it right when I needed it.

    Regards,
    Vikrant

    ( Reply )
  52. PG

    GlenStef September 20th

    flash.tutsplus.com to GoogleReader!
    Thank you

    ( Reply )
  53. PG

    jole September 22nd

    I got an error:
    TypeError: Error #1010: A term is undefined and has no properties.

    I made the graphics myself and copy pasted the code from the source file.
    Everything looks like it is in working order, but unfortunatly not.

    ( Reply )
  54. PG

    navas October 7th

    very good tutorilas

    ( Reply )
  55. PG

    Dani November 18th

    great tutorial, do you have actionscript 2 tutorial?

    ( Reply )
  56. PG

    DPC November 18th

    Count me in for the error 1119 and 1067 nightmare.

    Error 1067: flip = new Flip(firstView, secondView, 2, “left”, true); //This is the main line, it calls the class indicating wich MovieClips flip, the time the effect will take, wich side to start from, and if the second MovieClip is in the Library.

    The 1119 errors pertain to the infoButton commands.

    CS4, Mac OS X version

    ( Reply )
  57. PG

    Dany November 19th

    Hi,

    Awesome tutorial.

    But I’ve increased the width of the background and when the objects flip they leave a trail of their transformation visible underneath. Try to increase the width from 320 to 900 lets say, flip the objects a few times and you’ll see the trail.

    Can somebody help me with this?

    Tnx.

    ( Reply )
  1. Arrow
    Gravatar

    Your Name
    November 19th