A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: temporary variables

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    85

    temporary variables

    hi folks,

    I would like to know what's the difference when I write a temporary variable like this (these are just examples):

    version1
    Code:
    for each tempEnemy in enemyManager.enemies {
    	var tempX:int = tempEnemy.x;
    }
    or this:

    version2
    Code:
    for each tempEnemy in enemyManager.enemies {
    	tempEnemy.oldX = tempEnemy.x;
    }
    What's wrong and right? Currently I write it like version 2 and I'm not sure if I should change it to version 1. Can someone help me out with this please? I know most developers write like version 1 but I'm a little bit confused because I am totally unaware about version 1. If I use version 1 does that mean that my value is stored explicitly in a temporary variable that is cleared in every cycle?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    In both, you have failed to give tempEnemy a type. You should do that. I presume tempEnemy is something dynamic like MovieClip and that's what you're actually asking about.

    The first version actually creates a locally scoped variable tempX. tempX will be defined in the scope in which it was declared (like a function), but not defined outside that scope. It is not a property of tempEnemy.

    The second version does not create a variable. It sets a property oldX on tempEnemy. If tempEnemy is dynamic, then it does not need to declare an oldX property beforehand, and setting it will add it to the object. In the dynamic case, the new property is untyped, and you could potentially set it to something that is not the same type. If it is not dynamic, then that class will have to have declared an oldX property for that to work. Either way, you are setting a property on tempEnemy and tempEnemy will have a property oldX until that property is deleted (in the dynamic case), or forever (in the normal case). But just because it has a property doesn't mean that you can't set that property value to null or undefined or something.

    Usually, using an actual variable (version 1) is preferable because it does not cause any permanent changes to any objects. The variable automatically falls out of scope, and the memory used for it is eligible for garbage collection.

    If oldX should actually be a property of the enemy, then declaring a property in the class and using that is preferable. That may look like version 2, but using a dynamic property (without declaring it beforehand) is almost never a good idea.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    85
    Wow! Thank you very much! That helps a lot. In general all my variables are declared (somewhere in the code). This was just a short example to demonstrate my concernings. But they're gone now!

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