Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6802507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:13:54+00:00 2026-05-26T19:13:54+00:00

I would like to know what’s the difference when I write a temporary variable

  • 0

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

version1

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

or this:

version2

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:13:55+00:00Added an answer on May 26, 2026 at 7:13 pm

    In version 1 the declaration:

    var tempX:int
    

    defines a variable that lasts only as long as that iteration of the for (or for-each) loop it’s in. Each iteration tempX is defined, given a value from an Enemy object, and at the end it is left for garbage collection.

    In version 2, you reference two variables attached to an Enemy object referenced by the temporary variable named tempEnemy.

    In both versions the reference to the Enemy object, tempEnemy, is reassigned the next iteration’s Enemy object.

    Each method has its advantages. From a memory standpoint, version 2 is probably better, since it changes an existing variable over and over rather than creating a new variable that’s discarded at the end of each iteration. On the other hand, version 1 doesn’t require you to have oldX defined in its class variables, which can often get mucky enough without these sorts of variables.

    Best practices with code are based off of (a) working with other programmers, who need to be able to read and understand the code, and (b) leaving a project and coming back to it later, where you’ll need to be able to read and understand your own code. For short projects you don’t plan on sharing, version 2 is okay (and probably more memory-efficient), but any large project should use something more like version 1.

    Another consideration is, are you going to use that variable anywhere other than the function where it is defined(set)? If not, you don’t need to store it in the object, which points again to version 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is possibly a candidate for a one-line answer. I would like know it
I would like to know if any of you have experienced this and workarounds
would like to know how can this be implemented in Joomla. I have a
I would like know what is the difference between the Quartz Scheduler and JMS,
Would like to know when to use, This applies for what browser ? if
I would like to know how this query would be written if no joins
I would like to know whether this is the correct way of hiding visible
Would like to know which direction player hits terrain tile from (just a simple
i would like know some reference. I know i can googling it. but prefer
Would like to know what a programmer should know to become a good at

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.