ActionScript 3.0 onEnterFrame

In ActionScript 3.0, the frame event model has been changed from the ActionScript 2.0 version quite a bit. In ActionScript 2.0, if you wanted to perform a task every time you entered a frame, you would type

onEnterFrame = function(){
   trace("Do Something");
}

In ActionScript 3.0, you must use an event listener format to perform a frame event. For example…

addEventListener(Event.ENTER_FRAME,myFunction);
function myFunction(event:Event) {
	trace("Do Something");
}

It’s a little more complicated than the old way, but ActionScript 3.0 opens up a whole new world of programming possibilities.

37 thoughts on “ActionScript 3.0 onEnterFrame

  1. DameonFlashDemon

    I agree, it opens up way more doors. It will just take me a bit of time to get used too it. On the bright side I’m feeling more and more like a real programmer with 3.0. lol.

    Reply
  2. Damon Sanchez

    I think it will just be like the days of the dot notation switch, there is a learning curve but eventually the designer and the programmer will meet in the middle. I think what Adobe has to do now is stop moving forward for awhile and let us all catch up. If anyone remembers Director, this is what killed Director. Macromedia kept coming out with the “New and Improved methods” before you knew it the books were 12 inches thick and eventually people said screw this!!! and when to something SIMPLE and FAST which at the time was Flash. So I agree AS3 is better, but if AS4 comes out and it again is completely different – I will drop Flash like a bad habit and never look back. and start using SIMPLE and FAST which right now looks like it’s going to be Microsoft’s Blend, Visual Studio and Silverlight (WPF) Trinity.

    Reply
  3. Danny Staten

    I have developed out in silverlight and with version 1.0, it is not ready to compete at the level with flash. It doesn’t have anything equivalent to the flash movie clip for example. Reusing a single storyboard on multiple buttons etc requires custom javascript to modify the storybaord’s targets to match whatever you rolled over. It is all doable, but it version 1.0 isn’t quite there yet. I look forward to when it is there though because silverlight has promise. I for one am really curious to see how the battle between Flex and Silverlight is going to shape up.

    Reply
  4. Mux

    can you help me with a code? iam trying to make a button wich expands when mouse roll on it and collapses when mouse is rolled out : thx for help

    this.stop();

    import flash.events.*;

    var rewind = 2

    this.addEventListener(Event.ENTER_FRAME, enterf);
    function enterf(e:Event){
    if(rewind == 1){
    prevFrame();
    }
    }

    this.addEventListener(MouseEvent.MOUSE_OVER , over)
    function over(event:MouseEvent){
    var rewind = 2;
    play();
    }
    this.addEventListener(MouseEvent.MOUSE_OUT , out)
    function out(event:MouseEvent){
    var rewind = 1;
    }

    Reply
  5. Ryan

    Doing AS3 in Flex is pretty neat.. at first it’s weird with no UI based IDE, like someone pulled the floor out from under you.

    Then once you get the hang of it, the freedom and intellisense is awesome. The libraries of code out there from the communities are great.. And these blogs and boards of course are the grease in the cogs. Nice work

    Reply
  6. Ryan

    Is onEnterFrame still the best way to have something executing constantly ?

    i.e : I have a sprite I want it kinda floating around randomly on the screen, I have my OnEnterFrame event listener running my MoveStuff(), it’s works.. but is this the best way to acheive this ?

    Reply
  7. Julio Villa

    I’m totally new to actionscript 3.0…i’m trying to load an external swf file with a frame event, into a movieclip on the mainstage. How do i go about doing this…I could do it in actionscript 2.0 with loadmovie(); but this is all rocket science to me now. PLEASE HELP!

    Reply
  8. maroon

    AS3 is a bad idea.
    The AS2 is better than AS3 for designers, but who using flash? – Designers. Adobe should be changing AS for designer.

    Reply
  9. Simon

    I completely agree with Damon Sanchez. Flash is going the same (wrong) way as Director did with ‘lingo’. Every time Adobe make actionscript ‘better’, it takes me months to catch up. I’ve completely ignored 3.0 until now because it’s so completely different to 2.0. Flash has always been for designers, why change it? There are much better apps and scripting languages out there for developers. Flash has (had) it’s niche – it was a great lightweight middle-ground between design and development and it worked a treat. Now, if Flash was able to do incredible things thanks to the new scripting language I’d understand.. but it’s just making simple tasks harder for people like me (designers) and more familiar for developers (not me). At the same time, though, Adobe are automating simple operations, like tweening (both via scripting and now, with CS4, during authoring). Ridiculous. Yes I could stick to 2.0 but then I’d fall behind – which you can’t afford to do in this industry.
    They’re trying to stay one step ahead of their new competition BUT by making people learn a new scripting language, aren’t Adobe risking their users switching to a new app since they’re learning a new language anyway? And what about in education? How are students ever going to learn when their teachers are just as new to it as they are?
    Not happy.

    Reply
  10. PapaJ

    @maroon

    AS3 is not bad, and Flash is not used exclusively by designers (I’m a designer myself BTW). The Flash platform is now a viable and efficient solution for developing and deploying desktop and internet apps.

    Actionscript is now an object orientated language, and anyone worth their salt will tell you that OOP development (despite a steeper learning code) is the *only* way to develop feature rich media.

    It really annoys me when people knock AS3 saying it sucks – just because you don’t understand it and have to change some of your (bad) design/development habits to industry recognized standards is no reason to dismiss such a much needed update to the language.

    Luckily for you ludites, Adobe is creating another Flash IDE called ‘Flash Catalyst’, aimed at allowing designers to quickly create functionality without having to touch code (http://labs.adobe.com/technologies/flashcatalyst/). It looks like a good app, and fits nicely into the design > develop > deploy process – hopefully it’ll stop whining little peons like you from banging your pretty little heads against brick walls.

    Reply
  11. Damon Sanchez

    Action Script 3 is powerful, but not close in power and measure to other OOP languages like ASP.Net C#. So I guess where my complaint is, if Adobe is going to drop a bomb on us, then do it… and get it over with… make Action Script just-as-if-not more powerful than C#. But the steady incremental changes with each incarnation being holistically different than the next is driving me nuts. Again I’m complaining because it took me a year to relearn AS3, I’m happy now going all Kung Fu on Problems but dude, if AS5 is holistically different than AS3 —I’M DONE WITH ADOBE and moving on to Silverlight WPF and Blend. Which by the time AS5 comes rolling about will have as much ubiquity as SWF.

    Reply
  12. mike

    Large clients want Flex based RIAs these days… “pretty” photo showcases and “sticky” banners are no longer in demand. You can’t impress folks or make a dime anymore with some whiz-bang AS2… I’ve been working with flash for 9 years so AS3 was a MASSIVE hit to my way of thinking, pride, my whole mantra really and wow has it opened doors that I NEVER thought would be available to me. I was forced to adapt, because eating is important to me, and it turned out to be a blessing to any existing AS2 developer who commits to AS3. Everyone else is falls in the much needed washout category in my mind. The industry needed a purge though ;) .

    -m

    Reply
  13. john

    Hi chaps,
    I have a big problem.
    I try to move on and start using AS 3.0
    Here is what i need.
    On enter frame load an external swf file.
    When the time line hits frame 20, “ delete onEnterFrame (remove the swf file)”.
    On Frame 21 load different swf file, when it hits frame 50, gotoAndStop(1);
    Can somebody make my day?
    Thanks in advance.
    John

    Reply
  14. john

    I have managed to create the swf file which will load other swf files after 5 secs.
    What i need now is: after 5secs the first swf file to be removed and on the next frame to upload a new swf file bu just changing the
    var my_next_swf:URLRequest = new URLRequest(“swf1.swf”);

    to

    var my_next_swf:URLRequest = new URLRequest(“swf2.swf”);
    and
    var swf_secs:Timer = new Timer(5000, 1);
    to
    var swf_secs:Timer = new Timer(10000, 1);

    here is the full code.

    var my_next_swf:URLRequest = new URLRequest(“swf1.swf”);
    var next_swf_loader:Loader = new Loader();
    next_swf_loader.load(my_next_swf);
    addChild(next_swf_loader);

    var swf_secs:Timer = new Timer(5000, 1);

    swf_secs.addEventListener(TimerEvent.TIMER, doit_next_swf);

    function doit_next_swf(event:TimerEvent):void
    {

    nextFrame();

    //removeEventListener(Event.ENTER_FRAME, ?);
    }

    swf_secs.start();

    Reply
  15. Honza

    Could someone help me convert 2.0 to 3.0
    Im screwed…

    _root.onEnterFrame = function(){
    if(_root._ymouse200){
    myVar=true;
    }
    if(_root._xmouse100 and myVar==true){
    imgBar.nextFrame();
    imgBar.nextFrame();
    } else{
    imgBar.play();
    }
    if(_root._xmouse>50 &&_root._xmouse<350 && myVar==true){
    imgBar.stop();
    }
    }

    Reply
  16. Honza

    or better

    _root.onEnterFrame = function(){
    if(_root._ymouse200){
    myVar=true;
    }
    if(_root._xmouse100 and myVar==true){
    imgBar.nextFrame();
    imgBar.nextFrame();
    } else{
    imgBar.play();
    }
    if(_root._xmouse>50 &&_root._xmouse<350 && myVar==true){
    imgBar.stop();
    }
    }

    Reply
  17. Diana

    please i need to convert as2 flashplayer 7.0 to as3 flash player 10.
    the code is:

    // Ordenadas de Origen
    orig_x = this._x;
    orig_y = this._y;

    // Profundidad Asignada
    profundidad = 20; // No olviden ajustar el valor

    // Ajustamos su posición Z en la escena
    this.swapDepths( profundidad );

    onEnterFrame = function(){
    // Obtenemos la ordenada X de destino
    dest_x = ( ( ( 210 – _root._xmouse ) / 210 ) * profundidad ) + orig_x;
    // Obtenemos el incremento de movimiento en X
    incr_x = ( dest_x – this._x ) / 10;
    // Ajustamos la posición en X
    this._x += incr_x;

    // Obtenemos la ordenada Y de destino
    dest_y = ( ( ( 130 – _root._ymouse ) / 130 ) * profundidad ) + orig_y;
    // Obtenemos el incremento de movimiento en Y
    incr_y = (dest_y – this._y ) / 10;
    // Ajustamos la posición en X
    this._y += incr_y;
    }

    Reply
  18. M.Paaske

    I’m trying to make a flash move on to a php site when it reatchen it’s end. So I tryed this but it just spams my browser with new tabs.

    addEventListener(Event.ENTER_FRAME, e);

    function e(event:Event):void
    {
    var link:URLRequest = new URLRequest(“http://www.google.com”);
    }

    stop();

    Reply
  19. Brauner

    I am as frustrated as the rest of you with learning AS3. I have a graphics degree, and picked up flash design (AS2)because it suited me better.
    Now, as I am becoming pretty fast and knowledgeable at an upper intermediate level, everything I have learned is no longer applicable.

    Fortunately, I have access to a lot of training videos, etc. I’ve been taking pages and pages of notes, doing side test swf projects, and making myself write out code by hand instead of copying and pasting… All to make a working navigation that loads/unloads external swf files into a container swf. Something I can do in my sleep in AS2 now seems to take hundreds of lines of redundant code because I am not used to creating generic functions/variables that can be “recycled”.

    I’m still trying to figure a slick way to have an empty loader waiting for content (an external page swf) so that my button press will populate it BUT then have the next button press remove the current swf AND load the next one. Right now they are stacking on top of themselves.

    Anyway, just wanted to vent because I’m the only flash guy in my group here, and no one really gets what’s happening here! :?

    Reply
  20. Brauner

    Here’s my button code by the way (at the moment) It’s got a lot of commented out stuff as I turn off and on different pieces of code. Oh- and please excuse my goofy naming conventions!! When it’s really late, and I’m tired, and trying to remember which part of a function/var is in my words and not as3 I try to keep a sense of humor about it!

    ————————————-
    btn1_mc.buttonMode=true;
    btn2_mc.buttonMode=true;

    btn1_mc.useHandCursor=true;
    btn2_mc.useHandCursor=true;

    var titsLoader:Loader;
    //var boobsLoader:Loader;

    var urlReq:URLRequest;

    var ballsUrl:String=”balls.swf”;
    var boobsUrl:String=”boobs.swf”;

    addEventListener(Event.ENTER_FRAME,myFunction);
    function myFunction(event:Event) {
    trace(“Do Something”);
    }

    function Badass(ba:MouseEvent):void {
    if (ba.target==btn1_mc) {
    //titsLoader = new Loader();
    urlReq=new URLRequest(ballsUrl);
    titsLoader.load(urlReq);
    mt_mc.addChild(titsLoader);
    }
    if (ba.target==btn2_mc) {
    titsLoader = new Loader();
    urlReq=new URLRequest(boobsUrl);
    titsLoader.load(urlReq);
    mt_mc.addChild(titsLoader);
    }
    }

    //function unloadMovies(e:Event):void {
    // removeChild(titsLoader);
    // removeChild(titsLoader);
    //}

    btn1_mc.addEventListener(MouseEvent.CLICK,Badass);
    btn2_mc.addEventListener(MouseEvent.CLICK,Badass);
    ————————————

    This is just a side test file to see how the hell I can put in/ pull out my page swfs..
    I havent gotten it to work totally yet.

    Reply
  21. SCOTT

    SIMON IS A MORON. Flash has full capabilities of any other scripting language AS3 is way more logical and easy to use. they are making the program better you moron. it takes no time to learn AS3 if you know AS2. Use your brain and learn something technology is about advancement not sitting still in the same place forever.

    Reply
    1. JohnB

      “AS3 is way more logical and easy to use. they are making the program better you moron. it takes no time to learn AS3 if you know AS2.”

      Logical to you perhaps. AS2/AS1 was, to me, a gateway as a graphic designer into the programing world. It was easy to use because it was very lenient and did a lot of the heavy lifting for you. It didn’t restrict you to any particular way of solving a problem.

      I’m a f’ing ninja in AS2. I can make anything I needed relatively quickly. To say that if you know AS2 then AS3 is easy is simply wrong. It uses some of the same syntax and ideas, but the general way of organizing the project is completely different.

      To an artist, having that kind of flexibility that AS2 provided is logical. AS3 is trying to be more like real programming languages which doesn’t really make sense cause it’s still less powerful than other languages. So if is logical to other programmers who love doing things in a “logical” way. The problem I see is, why would you want to use AS3 when there are way more powerful languages out there?

      Reply
  22. loobrasil

    please i need to convert as2 to as3 flash player 10.
    the code is:
    _root.onEnterFrame = function(){

    if(_root._ymouse400){
    myvar=true;
    }
    if(_root._xmouse700 and myvar==true){
    imgBar. nextFrame();
    } else{
    imgBar.play();
    if(_root._xmouse>100 && _root._xmouse<800 && myvar==true)
    {imgBar.stop();}
    }
    }

    Reply
  23. Sam

    Just to be part of this constructive discussion, I’ll present myself as a designer, with good skills in programming. I do my best with php and AS to create dynamic websites that my clients usually enjoy.
    I’ve spent hours, days and weeks learning AS 3, and after all this time, I can do almost as many things with AS 3 than I coud with AS2. The only difference is that it takes twice as long programming (like the on(press) stuff becoming much more longer).
    So, to conclude this interesting participation, I would just definitely say that AS3 REALLY sucks.

    Reply
  24. SueJ

    Welp, I have AS3 and don’t know from AS2… I appreciate the frustration — I can’t even figure out how to do it at all. I just want to have a program to have some animation and then input from the user. I have a book that showed me how to do a number guessing game, but there’s no animation before it. So when I did my version with the animation, it just animates right through. I have no idea whether I’ve got a coding mistake or whether I just need to figure out how to make it stop.

    Reply
  25. cbrown

    Arrgh! I’m completely new to ActionScript 3.0 and only dabbled a little in 2.0 for some very simple actions. Can someone please help me convert the original example (pasted below) to get the movie to stop.

    Original Example:
    addEventListener(Event.ENTER_FRAME,myFunction);
    function myFunction(event:Event) {
    trace(“Do Something”);
    }

    Reply
  26. Tim

    I need help please. I have this in as2 for a button that zooms forward on roll over and stops then drops back on roll out.

    my as2 is
    this.onEnterFrame = function(){
    if(zoom == true){
    prevFrame();
    }
    }

    this.onRollOver = function(){
    zoom = false;
    play();
    }

    this.onRollOut = function(){
    zoom = true;
    }

    Could someone show me how to do this in as3 please?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>