Removing a specific element from an array by name in AS3 - arrays

I would like to remove a specific element from an array, not by index because the index value of that item is not static.
myarray.splice(myclip, 1);
When I use this code instead flash removes the first element in the array.
Is there something I am missing here?

Documentation of splice()
Both parameters need to be integers, the first one is the position of the element you want to delete, and the second one is the amount of elements you want to delete. Try myarray.splice(myarray.indexOf(myclip),1);
Don't know why it would only remove the first element in your snippet, maybe internally it casts myclip to 0? Doesn't matter, use indexOf. If that doesn't work, for loop through the array to get the position first.

Related

Dynamic Array of flash AS3

I have an array to store true answers and false answer of random frame multiple choices questions:
var arraytruefalseanswer=[];
I use push method to insert every true and false answer in the array:
arraytruefalseanswer.push(trueanswer)
arraytruefalseanswer.push(falseanswer)
The problem is:
I can not remove the last element of arraytruefalseanswer .
Because
If I use pop method arraytruefalseanswer.pop(),
it will remove all elements in the array arraytruefalseanswer or bring back to
arraytruefalseanswer=[]
If i use delete, it is still leaving null.
Please help... how can I remove the last element of arraytruefalseanswer using flash AS3?
Thank you.
The documentation says: When you do a pop(), it returns items that were popped, and the array gets modified as a side effect. Therefore, in order to just remove the last element, you call arraytruefalseanswer.pop(); as is. You can use trace(arraytruefalseanswer) to verify if anything popped. Also check your code flow, it's possible that when you're popping your last element, you think the code reaches the call once but it's not so, and it say pops your entire array so you've left with an empty array. I can't say more without ever seeing your entire block of code where you work eith your truefalseanswer.

How can I remove an element from an array at an index position without permanently changing the array?

How do I remove an item from an array at a specific index without permanently removing the item from the array? I have
my_data_col.tap{|i| i.delete_at(index)}.select{|item| ... }
I notice that, after the call, my_data contains one element fewer than it did when I started. How do I remove the element at the index without permanently altering the array?
Given your example in #Ursus's post (now deleted) I would execute as follows:
my_data_col.select.with_index do |str,idx|
str.respond_to?(:downcase) && idx != index
end.map(&:downcase).uniq
Next time please post your full example so people can assist more easily

Removing Elements From Arrays in SpriteKit/Swift

When you remove a SpriteNode from Parent in SpriteKit, does it automatically remove it from any arrays that it is in? Also, when you remove an element from an array, does every other element shift or is there a gap where the element used to be?
Yes when a node is removed from a parent it is removed from the parent in every way. There is no array automatically created for a parent and its children. If you create an array manually you'd have to remove the element manually as well. When an element is removed it doesn't leave a gap in between. All the elements after it just shift one spot down.
var array = [0,1,2,3] //A new array
array.removeAtIndex(1) //The 1 would remove the element in the second place which is a 1 in this case
//Now the array should look like [0,2,3] instead of [0,,2,3]//The one is not nil. No gap in between.

Remove a term from an array during a for loop

I've been creating a system for storing objects that persist for a specified duration, then remove themselves. However, I'm having some trouble figuring out how to remove expired items from the array while the for loop is running (to minimize extra iterations through the array)
Here is what I've made so far. Terms in the list are flagged with a Boolean value indicating that they're done.
For i = 0 To VisualEffects.Count - 1 Step 1
VisualEffects(i).Update(gt)
VisualEffects(i).Draw(sb, Pos, Cam)
If VisualEffects(i).CD.isExpired() Then
VisualEffects.RemoveAt(i)
i = -1
End If
Next
Why does this produce an error? How can I remove a term from an array and continue iterating through the remainder of the loop?
Regards
Ares
The line
i = -1
sets i to -1, causing the For loop to terminate.
If you can, reverse the order of the loop so that you start with the last array element and count down. That makes the remove logic much more straightforward.
.NET arrays are fixed-size so you can't actually remove an element from an array at all. You can change the value of an element, so you can set an element to Nothing to remove the object that it referred to, but the element is still there. If you read the MSDN documentation for the Array class, you'll see that the RemoveAt method throws a NotSupportedException for this reason.
If you're actually using a collection rather than an array then you can call RemoveAt but you need to loop from the last index to the first rather than first to last. That way, removing an item will not affect the indexes of those items that you are yet to visit.

Do arrays in AS2 have to be reset to the beginning when re-used?

I am working with an older and undocumented set of ActionScript (AS2) and I have found that an array, when looping through it the second time, does not give the proper results. It has been a while since I used ActionScript - does the array need to be reset before the second time through another for loop?
For instance PHP has reset() which returns the array's pointer back to the first item in the array.
There's is no such thing as pointers in ActionScript.
You can target each and every item in an Array by simply targeting it with myArray[index], and no pointer needs to be reset to be able to re-read it.
If your two loops produce different results, I would suggest looking into code that could change anything in it between the two loops or in the first one.
Maybe you could post it here ?

Resources