A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Declare variables referenced in enterframe at function or class level?

  1. #1
    Member
    Join Date
    Feb 2003
    Location
    Calgary
    Posts
    49

    Declare variables referenced in enterframe at function or class level?

    Hi,

    If i have a series of variables that are referenced on an enter frame function, is it better to declare the frequently used vars at the class level? Is there a performance benefit? What is the standard?

    Example

    PHP Code:

    public class Animation extends Sprite{

      private var 
    num:int;

      public function 
    Animation(){
      }

      private function 
    happensOnEnterFrame():void{
        
    num transformation
        
    // do other things with num...
      
    }


    ...as opposed to...

    PHP Code:

    public class Animation extends Sprite{

      public function 
    Animation(){
      }

      private function 
    happensOnEnterFrame():void{
        var 
    num:int
        num 
    transformation
        
    // do other things with num...
      
    }



  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Personally, unless num needs to be used outside of that function, I would declare it inside. Why clutter up the class with properties that are used in only a single place?

    I haven't tested, but I would also expect the local variable to be slightly faster as it's in a narrower (and thus resolved earlier) scope.

  3. #3
    Member
    Join Date
    Feb 2003
    Location
    Calgary
    Posts
    49
    Thanks flax, I thought that perhaps if the variable was only declared only once at the class level instead of being redeclared every time the enterFrame function was called, there might be a performance benefit although likely infinitesimal. The points about cluttering the class and possible speed benefit due to local scope make sense.

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