0
\$\begingroup\$

I am developing a game with Action Script 3.0 using Starling Framework, converting it from C++

C++ Code :

for (std::vector<MyObject*>::iterator i = m_listEnemy->begin();i!= m_listEnemy->end();)
{

    (*i)->update(dt);
    if ( (*i)->m_Hp <=0 )
    {
            (*i)->release();
            i = m_listEnemy->erase(i);
            continue;
    }
    i++;
}

MyObject is the class.

What Action Script 3.0 data type matches the C++ iterator? Also , how can I convert this C++ code to Action Script 3.0 code? Thanks in advance!

\$\endgroup\$
  • \$\begingroup\$ if we are only talking about vector here then its equvelant to an array you can do the following for(int i=0; i<size; i++) arr[i]; \$\endgroup\$ – concept3d Nov 1 '13 at 19:46
1
\$\begingroup\$

Different approaches:

for each (var value:MyObject in array)
{
   ...
}

for (var index:int in array) /*or (var prop:* in obj)*/
{
   var value:MyObject = array[index]; //or var value = obj[prop]
   ...
}

for (var i:int = 0; i < array.length; i++)
{
   var value:MyObject = array[index];
   ...
}

There are no native iterators in AS3. You can however download polygonal's game dev libraries if you want mature, advanced data access capabilities. These libraries are sort of like Boost for AS3 :)

\$\endgroup\$
  • 2
    \$\begingroup\$ As for removing objects out of an array: Arrays in as3 automatically reposition the elements when one object is removed. So it's best if the loop is done in reverse (for(var i:int = array.length; i > 0; i--)). It prevents the loop to skip an element. \$\endgroup\$ – Sidar Nov 1 '13 at 20:12
  • \$\begingroup\$ Correct. It's either that, or you break the loop immediately on finding the relevant element, store the index, and then remove it outside of the if-block. Removing in while iterating in reverse is a little more concise. \$\endgroup\$ – Engineer Nov 1 '13 at 20:24
  • \$\begingroup\$ dear nick Wiggill, can you give me some code :( when remove object from list. Thank you very much \$\endgroup\$ – user919496 Nov 2 '13 at 15:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.