A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Adding/Removing Events from a Single Enter_Frame loop?

  1. #1
    Junior Member
    Join Date
    Feb 2006
    Posts
    9

    Question Adding/Removing Events from a Single Enter_Frame loop?

    Greetings,

    I'm creating another game, this time in AS3. It is my first AS3 project. In my previous games, I developed in pretty much AS1 and just about every movieClip had it's own enterFrame function. I keep hearing about code that uses a single enterFrame function to execute all things needed, but alas I have never attempted such a routine.

    I have many ideas on how to achieve it, however I was hoping to get some official examples from people who, well, know more than me! Obviously, I don't want to be all like amateur hour and end up creating a piece of code that ends up more taxing on system resources, slowing performance, more than my old style of executing events!

    To be clear, I'm looking to run a single enterFrame event that could have events added and removed from it. So, say you had a moving cannon that shot a single cannon ball. You would have a function for the movement of the cannon, and another function for the movement of the cannon ball. Both of these would be executed on enterFrame. Instead of adding an enterFrame event listener to each sprite, add the functions so that a single enterFrame, running perhaps on the root, would know to execute these functions. Furthermore, when the cannon ball moves off screen, the function would be removed from the single enterFrame.

    Any code examples or links to tutorials would be most welcome!
    Many thanks!
    -sc2071

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    The problem with that is that the movements of the cannon and the ball would be the same.Here is the script to achieve what you want, but every object will move now.
    PHP Code:
    for (var count=0;count<numChildren;count++)
    {
        
    getChildAt(count).addEventListener(Event.ENTER_FRAME,frameHandler);
    }
    function 
    frameHandler(event:Event):void
    {
        var 
    clip:Object=event.currentTarget as Object;
        
    clip.x+=10;
        if(
    clip.x>=400)
        {
            
    clip.removeEventListener(Event.ENTER_FRAME,frameHandler);
        }

    - The right of the People to create Flash movies shall not be infringed. -

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