How can add element to array in AS3? - arrays

How can add element to array in ActionScript3
If i have an array:
var myArray:Array;
How can add element to this array "myArray", something like this:
myArray[] = value;
My second question is: How can compare if variable value exist in array element value?
Something like in_array function in php

1. All of these are different ways of adding item to array.
someArray.push(someValue); : add last item
someArray.unshift(someValue); : add first item
someArray[index] = someValue; : set item somewhere
someArray.splice(index, 0, someValue); : insert item somewhere
2. Checking if a value is present in array.
if (someArray.indexOf(someValue) == -1) { /*value is not present*/ }
Refer to ActionScript language reference on Adobe livedocs.

To answer both your questions here, you can add to an array by direct access or by the push() method, like so:
myArray[7] = something;
or
myArray.push(something);
Also as Nox noted, you can use the splice method as well to add in elements. This method is used to delete N amount of elements at a specific index, but you can also simultaneously inject one or more elements at the same index.
For your second question about how to check values or compare them in an array, here is one method:
var i:int = 0;
for(i; i < myArray.length; ++i){
if(myArray[i] == 10){
trace('found');
}
}

Related

Is it safe to iterate an array while modifying it?

I know you shouldn't, I kind of know why. But I mean I don't understand my own code once I am trying really to think what's going on.
So I have an array with bunch of objects. I am iterating over it and once I find an object with specific type, I remove it from the array, and add another object into the array. So something like this:
var arr = parent.allchildren() //getting all the children in array
for ele in arr{
if(ele==somethingHere){
parent.remove(ele)
parent.add(new ele) //add new child into child array
}
}
If I have an array of 1,2,3,4,5, and I remove 3 and add a 6 while iterating, the actual array would be 1,2,4,5,6 but the array I am iterating would still be 1,2,3,4,5.
Which I think it would be fine, because at the end I still get what I want, which removed the element and added the element I need. However modifying the list while iterating it is bad and you shouldn't do that, but for my case I think it does what I need. What could be the potential issue in my case that I can't see?
One thing you may want to think about doing is making all of the changes at the end of the iteration. Instead of making the changes one by one, record the changes you want to make while iterating, and then actually make those changes once your loop is finished.
For example, you could make an array of elements to remove, and an array of elements to add.
//Our array where we record what we want to add
var elementsToAdd = [Any]()
//Our array of what elements we want to remove. We record the index at
//which we want to remove the element from the array
var indexesToRemoveAt = [Int]()
//Getting all the children in array
var arr = parent.allchildren()
//Enumerating an array allows us to access the index at which that
//element occurs. For example, the first element's index would be 0,
//the second element's index would be 1, the third would be 2, and so
//on
for (index,ele) in arr.enumerated() {
if(ele == somethingHere) {
indexesToRemoveAt.append(index)
elementsToAdd.append(newEle)
}
}
//Now that we have recorded the changes we want to make, we could make
//all of the changes at once
arr.remove(at: indexesToRemoveAt)
arr.append(contentsOf: elementsToAdd)
Note that removing array elements at multiple indexes would require the following extension to Array. If you wanted to avoid creating this extension, you could always just loop through the array of indexes and tell the array to remove at each individual index. All this extension function is really doing is looping through the indexes, and removing the array element at said index.
Array extension to remove elements at multiple indexes:
extension Array {
//Allows us to remove at multiple indexes instead of just one
mutating func remove(at indexes: [Int]) {
for index in indexes.sorted(by: >) {
if index <= count-1 {
remove(at: index)
}
}
}
}
I just tested in a playground with the following code:
var arr = ["hi", "bye", "guy", "fry", "sky"]
for a in arr {
if arr.count >= 3 {
arr.remove(at: 2)
}
print(a)
}
print(arr)
This prints:
hi
bye
guy
fry
sky
["hi", "bye"]
So it looks like when you use a for-in loop in Swift, the array is copied and changes you make to it will not affect the array you are iterating over. To answer your question, as long as you understand that this is the behavior, there's nothing wrong with doing this.

Actionscript 3: Identifying an array element to delete when the elements are objects/class instances

I have an array of objects, some of which are movieclips and some instances of classes. I need to remove an item from the array but are struggling with how best to identify which array index to delete.
I am currently using this code, which does work
var i:int;
for (i = 0; i < list.length; i++) {
if (list[i].toString() == '[object myClass]') {
trace('found', i);
list.removeAt(i);
}
}
I am thinking a better way must exist to get the object name and without looping through the array.
I could use a little clarification on how you want to identify the object(s) that should be removed. If you are looking to simply remove any object that is an instance of the myClass class, I would recommend the is keyword.
Also, a warning, removing items from a list as you are iterating over it is just asking for trouble. If you remove object at index [0] then the object that used to be at index [1] is now index [0], but your for loop is going to increment i by one at the end of each iteration, so you will never check index [0] again. Thus you may skip one or more of the objects that you wanted to remove.
Instead try iterating over it backwards-- that should resolve that problem.
Here is what both of those recommendations together looks like:
for (var i:int = (list.length - 1); i >= 0; i--)
{
if (list[i] is myClass)
{
trace("found", i);
list.removeAt(i);
}
}

Last cell in array that modified

Is there any way to find out which cell in an array is the last cell that modified? (e.g. change its value)
In any compile-based languages.
For example assume we defined array1 with n cells. Now value of array1[2] changed to 1, after that value of array1[7] will change to 1, I want a solution to find out the 7th cell as last cell that modified.
Just don't access the array directly, write to it in a function/method and keep track as it changes. In C# there are properties/indexers which wrap the call X = 2 in a method for you.
Neither of these seem like particularly great solutions, but they will both accomplish what you want in C++:
int indexToChange, lastIndexChanged;
int array1 [10];
indexToChange = 2;
array1[indexToChange] = 1;
lastIndexChanged = indexToChange;
indexToChange = 7;
array1[indexToChange] = 1;
lastIndexChanged = indexToChange;
Then at any point array1[lastIndexChanged] would be the most recently updated. But this is annoying since it requires 3 commands for every array update. Instead, you could wrap it into a function:
void changeArray(int array[], int length, int indexToChange, int newVal){
array[indexToChange] = newVal;
lastIndexChanged=indexToChange;
}
Which would then require all your array updates to look like this (assuming lastIndexChanged was declared globally and array1 has size 10):
changeArray(array1, 10, 2, 1);
changeArray(array1, 10, 7, 1);
Then, to update the most recently changed element to 0:
changeArray(array1, 10, lastIndexChanged, 0);
However, both of these examples will only work for a single array in a program, which does not seem particularly useful to me.
Only other idea I have involves creating an array of tuples (or something similar) and using the 2nd element of the tuple as one of the following:
A bool flag indicating if the element was the last one changed
An int flag indicating the "age" of the element
However, both of those methods require accessing every element of the array for every single array update.
For (1), with each update you would have to make sure to find the previously last updated element and set it's flag to false as you set the flag of the element you were updating to true.
For (2), with each array update you would increment the "age" flag of every element, but set the "age" of the element you updated to 0. This has the advantage of enabling you to also find the nth last updated element.
In either case, if your original array is an array of ints, you may be able to implement this with a 2-dimensional array with two rows. So, if array1 is size n, you would have int array1[2][n], giving you an array like this (for n=4, assuming all cells initialized to 0):
[0][0][0][0]
[0][0][0][0]
where you would use the top row for your values and your bottom row for flags. Thus, an array update would look something like this:
For "bool" flags (just using 0 and 1 to simulate a bool value):
array1[0][2] = 1; //set value of element 2 to 1
array1[1][getLastUpdated()]=false; //resetting the previous "last updated" flag
array1[1][2] = true;
For "age" flags:
array1[0][2] = 1; //set value of element 2 to 1
for (int i=0; i<array1Length; i++){
array1[1][i]++; //increment ages of every element
}
array1[1][2] = 0; //reset age of the element you just updated
Then, to find the most recently updated element, you would search for array[1][n] to be true or 0, respectively.

How can i splice current index in a foreach?

I have this foreach loop to check for collision and i want platform(movieclip) to be removed in case of collision. So far i've come up with this:
if (mcContent.mcPlayer.y + mcContent.mcPlayer.height > platformCloud.y)
{
mcContent.mcPlayer.y = platformCloud.y - mcContent.mcPlayer.height - 1;
jump();
mcContent.removeChild(platformCloud);
//platformsCloud.splice(platformCloud);
}
What this is doing is, removing the movieclip (ok so far so good) but without the splice, when the loop runs again through the array it is still there. So with the splice that is commented out there's 1 little problem, it removes all the movieclips from the array, apprently.
How can i splice only the current index that is being checked?
.splice() accepts a start index and an amount of items to remove, not the object you want to remove from the array.
Parameters
startIndex:int — An integer that specifies the index of the element in the array where the insertion or deletion begins. You can use a negative integer to specify a position relative to the end of the array (for example, -1 is the last element of the array).
deleteCount:uint — An integer that specifies the number of elements to be deleted. This number includes the element specified in the startIndex parameter. If you do not specify a value for the deleteCount parameter, the method deletes all of the values from the startIndex element to the last element in the array. If the value is 0, no elements are deleted.
You want to do this:
var index:int = platformsCloud.indexOf(platformCloud);
platformsCloud.splice(index, 1);
Why not just create a new array of the items to keep? Use Array.push to add new items. This may actually be more efficient than modifying the existing array. It also doesn't require keeping track of indices (which are required to use Array.splice).
Example code:
var keptPlatforms = [];
// do stuff
if (mcContent.mcPlayer.y + mcContent.mcPlayer.height > platformCloud.y)
{
mcContent.mcPlayer.y = platformCloud.y - mcContent.mcPlayer.height - 1;
jump();
mcContent.removeChild(platformCloud);
} else {
keptPlatforms.push(platformCloud);
}
// later, after this cycle, use the new Array
platformClouds = keptPlatforms;
Now, the reason platformsCloud.splice(platformCloud) removes all items is because the first argument is coerced to an integer so it is equivalent to platformsCloud.splice(0) which says "remove the 0th-indexed item to the end of the array". And, this does indeed clear the array.
To use Array.splice, you'd have to do something like:
// inside a loop this approach may lead to O(n^2) performance
var i = platformClouds.indexOf(platformCloud);
if (i >= 0) {
platformClouds.splice(i, 1); // remove 1 item at the i'th index
}

AS3 Array Object Filtering Question

I have an array of objects. What I would like to do is get the last index of an object whose property equals a certain value.
Ex:
// this occurs on a mouse click
var stockObj:Object = new Object();
stockObj.ID = "an_id";
stockObj.category = "a_category";
array.push(stockObj);
//psuedo
trace(array.lastIndexOf(stockObj.category=="a_category"));
I would like this to trace the index of the object whose category property equals "a_category"
function searchCategory(arr:Array, cat:String):int {
for (var i:int = arr.length - 1; i >= 0; i--) {
if (arr[i].category == cat) { // assuming array objects contains category
return i;
}
}
return -1; // no match
}
last index of searches on a string not an array:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/String.html#lastIndexOf%28%29
what you will need to do is run through the Array which will take O(n) time and compare to see which at which index has the object with category name "a_category"
for(int i = 0;i<array.length;i++){
if(array[i].category = "a_category")
maxIndex = i;
}
}
There is actually a better way to loop through all obj in an array of obj, but I can't remember it atm hopefully someone can comment that in but I think its something like
for (x in array){
...
}
anyways using that logic, it would be a lot faster if you reverse it, so you start at the end of the array and return the index with the first occurrence of the obj with category "a_category"

Resources