A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: I try to understang the event: "onEnterFrame"

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    12

    I try to understang the event: "onEnterFrame"

    Hi everyone,
    Apparently I dont understand the event named: "onEnterFrame" and I wondered if someone here would care to explain that to me.
    I assigned the following code to a 1 frame clip with a 1 movie clip named: "movieClip":
    Code:
    movieClip.onEnterFrame = function() 
    {
    i++
    trace ("onEnterFrame called"+i);
    };
    var i:Number=0;
    At the Output panel appeared hundreds of numbers as if the clip was consisted of hunderds of frames.
    As I understand it, the event "onEnterFrame" is activated when a clip changes frames.
    There is only 1 frame at that clip, how could it show 500 times an event of changing frame?
    Thanks

  2. #2
    Senior Member
    Join Date
    Jun 2003
    Location
    Kent, WA
    Posts
    536
    onEnterFrame runs independent of the movieclip's timeline. It WILL occur every frame, but it does not care what the timeline is doing. You can call play(), stop(), gotoAndPlay()... whatever you want on the timeline, and onEnterFrame will keep on going.

    If you want to stop onEnterFrame from running, you can call this code:

    Code:
    delete movieClip.onEnterFrame;
    The conditions in which you call that are of course up to you. You can even do it within the onEnterFrame loop, if you wanted...

    Code:
    movieClip.onEnterFrame = function() 
    {
        i++
        trace ("onEnterFrame called"+i);
        if (i == 200)
        {
            trace("Delete onEnterFrame");
            delete this.onEnterFrame;
        }
    };
    var i:Number=0;
    (The "this" keyword refers to the instance of the movieclip on which this code is running.)

    onEnterFrame will ALWAYS run unless you delete it or remove the instance of the movieclip it's attached to.

  3. #3
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    hi-

    for myself.. it was easier to think of it like this to understand it.


    onEnterFrame executes (over and over) as long as that object is DISPLAYED/VISIBLE on the stage or deleted..

  4. #4
    Senior Member
    Join Date
    Jun 2003
    Location
    Kent, WA
    Posts
    536
    Well, that's not entirely accurate whispers.

    You can set the movieclip's visible property to false, or set it's alpha to 0, or scale it down to 0%... onEnterFrame will still run. You can set it's x position to 8000 and have it be nowhere remotely on the stage, and it will still run.

    The only things that can end it is if you explicitly delete the onEnterFrame call or if the movieclip owning it is removed from the stage; either by calling removeMovieClip, or by having the timeline advance to a point where the movieclip instance no longer exists.

  5. #5
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,756
    Maybe I wasnt clear in explaining then..

    as long as you are on a frame that holds that object..it will run.

    regardless of _alpha, _visible, x/y coords....etc


    more so in the IDE.. if the clip is on/in the frame where the code lies..it will execute.

    delete onEnterframe or move to another frame where its not on the stage.. and it doesnt run.

    not DISPLAYED/VISIBLE when running/playing the movie.

    'On entering this new frame.. if this instance is there.. run this code, over & over'..


    hopefully that is clearer

  6. #6
    Junior Member
    Join Date
    Apr 2011
    Posts
    12
    Thanks a lot Marshdabeachy. Your post is very enlightening. Yor web page very impressing

  7. #7
    Junior Member
    Join Date
    Apr 2011
    Posts
    12

    Thanks Whispers

    Quote Originally Posted by whispers View Post
    hi-

    for myself.. it was easier to think of it like this to understand it.


    onEnterFrame executes (over and over) as long as that object is DISPLAYED/VISIBLE on the stage or deleted..
    I find your explanation quite clear and helpful. Thanks again.

  8. #8
    Member
    Join Date
    Apr 2003
    Posts
    43
    what is the difference between onEnterFrame and EnterFrame? I am quite confused.

  9. #9
    Prid - Outing Nig 13's Avatar
    Join Date
    Jul 2006
    Location
    Norway
    Posts
    1,864

    Difference

    Quote Originally Posted by chocomania_coco View Post
    what is the difference between onEnterFrame and EnterFrame? I am quite confused.
    Well, by EnterFrame, if you mean enterFrame as in onClipEvent(enterFrame), then the difference is as following:

    The main difference between onClipEvent(enterFrame), which is used on movieclips, and onEnterFrame, which is used mainly on Frames, is that with onClipEvent(enterFrame), you kind of enter inside that movieclip (just like when you double-click a movieclip in Flash, and enter inside it, just like that, but with Actionscript), and if you then have 2 movieclips on the same level, using onClipEvent(enterFrame), you'll have to use tags like _root or _parent, even though they're on the same level, because onClipEvent(enterFrame) enters that specific movieclip and executes the codes below it, once very frame (even though there may be only a single frame) - while, onEnterFrame doesn't enter anything. You can refer to movieclips, buttons or any types of objects, just like you'd do on any other code!

    Get it?
    I am back, guys ... and finally 18 :P

    BRING BACK THE OLD DESIGN!! OR AT LEAST FIX THE AS TAGS

  10. #10
    Senior Member
    Join Date
    Jun 2003
    Location
    Kent, WA
    Posts
    536
    That's a decent explanation for basic functionality.

    Under the hood, there are some key differences. onClipEvent(enterFrame) is a static function, whereas onEnterFrame is dynamically generated.

    onClipEvent(enterFrame) can never be deleted. It can never be changed. As long as the instance it's on exists, the code will run. It generally goes on timeline movieclips as well - something you've placed yourself. If, for example, you're attaching a movieclip out of your library dynamically through code (via attachMovie), onClipEvent(enterFrame) won't work... unless you've placed it inside a child movieclip of the one you're attaching. onClipEvent functions were the only option in Flash 5 and earlier, which is why you still seem them around.

    onEnterFrame was introduced in Flash MX. As mentioned, this is handled dynamically, meaning you can delete it or replace it. This is also very handy when you're attaching clips through code. You can simply add any kind of onEnterFrame event you want. Say you're attaching an "enemy" movieclip, and you want his behavior to be different depending on the state of the game. You can give one of several different onEnterFrame handlers to do it. And in the middle of his behavior, if you want, you could delete it and assign him a different one. Doing that through onClipEvent(enterFrame), while possible, would require an elaborate switch statement and would run a lot of unnecessary code.

    onEnterFrame is also much more modular. If, for example, you have 20 different enemy types, and you want them to all run the same code, you can make one function to do it. Just assign the onEnterFrame handler for each movieclip to that one function. If you did that through onClipEvent(enterFrame), you'd have to copy and paste the same code onto all 20 objects, which is extremely prone to errors and a nightmare to maintain.

    Personally, I haven't used onClipEvent(enterFrame) in years. onEnterFrame is pretty much always more useful.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center