Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

array.forEach or for "loop"

Guest
Mar 06, 2011 Mar 06, 2011

Is there a way to execute an "array.forEach(function);" thing backwards?

The reason I need to do it backwards is because the function, on occassion, deletes the part of the array being tested.

It works fine using a for loop, (I'm currently using "for (var i:int = array.length-1; i>-1; i--) {function(i)};") but I wondered if there was a more... "proper" way to do it.

Thanks,

Brook.

TOPICS
ActionScript
973
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 06, 2011 Mar 06, 2011

A for loop is as proper a coding solution as any.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 06, 2011 Mar 06, 2011

There is no way to execute Array.forEach backwards to my knowledge.

What makes callback function delete elements? Code would be helpful.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 07, 2011 Mar 07, 2011

It's just in the function that's being called.

In this case...

for (var i:int = fallingWords_array.length - 1; i>-1; i--) {

     wordHitShip(i);

}

function wordHitShip(i):void{

     if (fallingWords_array.y >= ship.y){

          var wordHittingShip:int = i;

          wordBeingShot_txt = fallingWords_array[wordHittingShip];

          fallingWords_array.splice(wordHittingShip,1);

          wordBeingShot_txt.visible = false;

          deadWords_array.push(wordBeingShot_txt);

          wordBeingShot_txt = null;

     }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 07, 2011 Mar 07, 2011

So, are you saying you tried to use wordHitShip() (with its shown functionality) as a callback function for array.forEach()?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 07, 2011 Mar 07, 2011

In any case, you can use array.forEach() as following (there is nothing wrong with the fact that elements are removed from the array):

array.forEach(wordHitShip);

function wordHitShip(element:DisplayObject, i:int, arr:Array):void{
    if (element.y >= ship.y) {
          wordBeingShot_txt = fallingWords_array;
          arr.splice(i , 1);
          wordBeingShot_txt.visible = false;
          deadWords_array.push(wordBeingShot_txt);
          // I am not sure why you nullify this -
          // you assigned value already and store the reference
          wordBeingShot_txt = null;
    }
}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 08, 2011 Mar 08, 2011

Actually, I was doing that before but.

I get an error on the occassions that if does delete the object that

"Cannot reference null object" or something.

I assumed that meant that 'forEach' would run through each integer of the array, rather than knowing which object was which.

So if there was objects...

a

b

c

d

e

If c was to be removed, the new array would be...

a

b

d

e

And so e would be checked after c, because it is now number 4.

And then it would run one more time, because it believed there is 5 elements.

And hence, a null reference.

That's why I wanted to run it backwards.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 08, 2011 Mar 08, 2011

And, about the nulling.

I was reading somewhere about memory eticate.

It said that if you don't need to keep something stored somewhere it's worth while nulling it, as this can have an impact on larger projects.

Is that not the case in this scenario?

Thanks,

Brook.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 08, 2011 Mar 08, 2011

I take it back. It is not a good idea to use array.forEach to remove elements - it will not even remove some elements under certain conditions. Your solution to use for loop is more reliable (and faster by the way).

As for nullifying, although from memory perspective one should nullify objects, in this case reference to it is stored in deadWords_array, thus there is a reference to it and, hence, it will not be removed from the memory. Here is what proves it:

var mc:MovieClip = new MovieClip();
var array:Array = [mc];
mc = null;
trace(array[0])// traces [object MovieClip] - still in the memory

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 08, 2011 Mar 08, 2011

Ah, ok.

So using for(loop) is faster than array.forEach?

So if there is a choice between the 2, it's better to use a loop, besides the slightly less tidy look of the code?

And... so keeping a second reference of something won't keep it in memory twice?

That's good to know.

Cheers,

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 08, 2011 Mar 08, 2011

1. "So if there is a choice between the 2, it's better to use a loop, besides the slightly less tidy look of the code?"

It depends on the task. I do like elegance of array.forEach syntax and do use it when appropriate. Performance loss is not all that big.

2. "so keeping a second reference of something won't keep it in memory twice?"

Reference is not a new variable but just a pointer (database of addresses) - this is the beauty of referencing. It's like saying apple belongs to fruits and apple is in a pot, it is one variable but you know it belongs to two different entities. Or, you can have an apartment and summer home addresses. You are one but database says that you have two places.

In OOP languages software when you create object - it stays in the same place. The only thing that is changing is references. So, actually Flash doesn't move objects around but reads how object is referenced and in which context. For instance, when you write addChild(myClip) - it doesn't mean that myClip is physically moved from one place to another - it just means that another reference to myClip is created. Virtual reality sort of...

In your case when you add it to dead words - you don't add the thing itself but rather reference to it. If there is a single reference to an object in the program - instance will not be removed from memory.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Mar 08, 2011 Mar 08, 2011
LATEST

Ok, cool. I think I get it.

But I assume that the addChild function must increase memory usage, because each instance can have it's own... well... everything.

Using addChild, does that not, in essence, create a new object?

I've been using multiple arrays to store created and destroyed children, so I never actually use removeChild, rather just store them for use later... To avoid garbage collection where possible and keep a flat line of a memory usage graph... even if I can't see it. And even if this game really won't get slow, but I want to get into good habits from the start.

I've created a version of my game with debugging in the top left to be able to view how many instanc...

(It's called version 3 cos it's the 3rd full re-write of the game after realising how I kept creating code that was rather un-flexible)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines