Okay, very simple: there is an array containing 3 objects. Each object has a unique property called "ID" with values of either 1, 2, or 3.
One of the objects gets deleted.
The objective now is to update the ID property of each object corresponding to the new array.length value.
So for example, the object with ID of 2 got deleted. The remaining objects in the array would each have ID values of 1 and 3 respectively.
So the objective is to loop through the array and update the ID properties to 1, and 2 (instead of 1 and 3).
So I guess the question is how to write a loop to update a common property of each element in an array. Thanks.
You can use a for-loop to go through the array, as in walkietokyo's answer, or you can use a method closure:
myArray.forEach ( function ( item:*, i:int, arr:Array) : void { item.ID = i; } );
or a while-loop:
var i:int = -1;
while (++i < myArray.length) myArray[i].ID = i;
for (var i:uint = 1; i <= myArray.length; i++) {
myArray[i].ID = i;
}
General info on loops:
http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7fcf.html
var i:uint; // for speed keep out of the loop
var arrayLength = myArray.length // for speed keep out of the loop
for (i = 0; i < arrayLength; i++) {
myArray[i].ID = i;
}
Related
var arr: Array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
I can add these numbers to an array separately like this but how do I add 1 to 50 at once instead of typing it all the way through?
for (var i:Number=1; i<=50;i++){
var arr:Array(i) = [i];
}
function randomize(a: * , b: * ): int {
return (Math.random() > .5) ? 1 : -1;
}
trace(arr.sort(randomize));
I am trying to implement something like this.
Thank you.
Pretty simple. You can address the Array's elements via square bracket notation. Works both ways:
// Assign 1 to 10-th element of Array A.
A[10] = 1;
// Output the 10-th element of A.
trace(A[10]);
Furthermore, you don't even need to allocate elements in advance, Flash Player will automatically adjust the length of the Array:
// Declare the Array variable.
var A:Array;
// Initialize the Array. You cannot work with Array before you initialize it.
A = new Array;
// Assign some random elements.
A[0] = 1;
A[3] = 2;
// This will create the following A = [1, null, null, 2]
So, your script is about right:
// Initialize the Array.
var arr:Array = new Array;
// Iterate from 1 to 50.
for (var i:int = 1; i <= 50; i++)
{
// Assign i as a value to the i-th element.
arr[i] = i;
}
Just keep in mind that Arrays are 0-based, so if you forget about index 0 it will remain unset (it will be null).
Suppose I have array A and array B which contains object. How do I "clean" elements from array A that are also exist in B. That is, I need to remove all element in A that are also in B.
From my understanding, you're trying to remove all similar items from array A that are present in B?
for (var i:uint = 0; i < arrayB.length; i++) {
arrayA.splice(arrayA.indexOf(arrayB[i]), 1);
}
Do note this only removes ONE of each item in arrayA. If you have multiple of the same instances in arrayA that are present in arrayB, only one will be removed. For multiples of the same instances, use this instead:
for (var i:uint = 0; i < arrayB.length; i++) {
while(arrayA.indexOf(arrayB[i]) >= 0) {
arrayA.splice(arrayA.indexOf(arrayB[i]), 1);
}
}
This effectively removes every element of Array B that happen to occur in Array A;
for each (var anItem:Object in B)
{
var anIndex:int;
do
{
anIndex = A.indexOf(anItem);
A.splice(anIndex, 1);
}
while (anIndex > -1);
}
I have a random number variable defined as below
var rannum:Number = Math.floor(Math.random()*50+1);
Then I have a trigger that calls for a new random number everytime a button is clicked
ranbtn.addEventListener(MouseEvent.CLICK, reran);
function reran (event:MouseEvent):void
{
rannum = Math.floor(Math.random()*50+1);
}
I would like to prevent the same random number from being selected until all the numbers have been selected and then possibly start over?
I found a few threads like this one but none of them were specifically what I needed
You need to create an array of the possible values and each time you retrieve a random index from the array to use one of the values, you remove it from the array.Here you have an easy example with javascript.
var uniqueRandoms = [];
var numRandoms = 50;
function makeUniqueRandom() {
// refill the array if needed
if (!uniqueRandoms.length) {
for (var i = 0; i < numRandoms; i++) {
uniqueRandoms.push(i);
}
}
var index = Math.floor(Math.random() * uniqueRandoms.length);
var val = uniqueRandoms[index];
// now remove that value from the array
uniqueRandoms.splice(index, 1);
return val;
}
I've found another option, You can declare an array of Integers:[1,2,3,4...50] and sort them randomly.
var sorted:Array = [];
for(var i:int = 0; i < 50; i++){
sorted.push(i);
}
//I'm making a copy of sorted in unsorted
var unsorted:Array = sorted.slice();
//Randomly sort
while(sorted.join() == unsorted.join()){
unsorted.sort(function (a:int, b:int):int { return Math.random() > .5 ? -1 : 1; });
}
If you get a selected num, you can add one until it is not selected.
Create a list of integers from 1 to 50.
Pick a random integer from the list and remove it.
When there are no more integers left (after 50 picks), repeat step 1.
Code:
function createRangeOfIntegers(from:int, to:int):Vector.<int> {
if (from >= to) throw new ArgumentError("Invalid arguments");
var integers:Vector.<int> = new <int>[];
for (var i:int = from; i <= to; i++) {
integers.push(i);
}
return integers;
}
function getRandomInteger(integers:Vector.<int>):int {
var index:int = Math.random() * integers.length;
var integer:int = integers.splice(index, 1)[0];
return integer;
}
Example:
// create all the possible integers
var integers:Vector.<int> = createRangeOfIntegers(1, 50);
// select a random integer
var random:int = getRandomInteger(integers);
// When you've selected all integers you can start over
if (integers.length == 0)
integers = createRangeOfIntegers(1, 50);
I created a level editor for web game, I can build, save, load and play levels.
Now I want to edit some levels but I have a weird situation.
I export a level as a single array, it looks like this 3,4,5,5,7,89,4,2,1...and those numbers represent frames. (tile-based).
Now if I want to edit this level and save it again, I need a level to be described as multidimensional array.
Actually, when I save the level I have a string that describes my map, then I convert string to array.
So can you tell me (if possible), how to convert this array1 (or string) to array2?
Lets say I have only 25 tiles (map from level editor is array1)
array1 =
1,1,1,1,1,
2,2,2,2,2,
3,3,3,3,3,
4,4,4,4,4,
5,5,5,5,5
I need this:
array2 =
[
[1,1,1,1,1],
[2,2,2,2,2],
[3,3,3,3,3],
[4,4,4,4,4],
[5,5,5,5,5]
];
UPDATE:
So I need 2d array to build level container.
I do not have experience with tile based games, but here you can see what I do.
Let's say I have 2d array and this is how I create a new level container:
for (i = 0; i < array2.length; i++)
{
for (var j = 0; j < array2[i].length; j++)
{
tile = new Tile();
tile.name = "" + i + j;
tile.x = j * tile.width;
tile.y = i * tile.height;
levelContainer.addChild(tile);
tile.gotoAndStop(array2[i][j]+1);
tile.addEventListener(MouseEvent.MOUSE_DOWN,
buildingLeve);
}
}
addChild(levelContainer);
I have tried to get 2d array from single array as Rudolfwm and Marcela suggested, but when I want to edit a level container using new array2, my tiles go on wrong frames.
For example, if correct frame is 1, tile goes to frame 11,
This code above (building level) works if I create my own 2d array, but not if I convert string to 2d array as suggested.
Try array1[x+y*row] which gives the same result as copying all your data to array2[x][y].
Or if you insist on 2d arrays:
var array2 = new Array(row);
for (var y = 0; y < row; y++) {
array2 [y] = new Array(column);
for(var x=0; x < column; x++) {
array2 [y][x]=array1[x+y*row];
}
}
You start with a String and convert that into an Array using String.split().
Once you have a temporary array, you use a nested loop to populate the final array (arr21).
var row:int = 5;
var column:int = 5;
var arr1:String = "1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5";
var tempArr:Array = arr1.split(',');
var arr2:Array = [];
for(var i:int = 0; i < row; i++)
{
arr2[i] = []; // create a new row array
for(var j:int = 0; j < column; j++)
{
// grab the first item from the temp array and push it onto the row array
arr2[i].push(tempArr.shift());
}
}
NOTE: This is not optimized, and could become quite laggy with larger level maps. This is just to give you an idea of where you can start.
I am trying to make a video quiz and when you click it adds a value of 1 to an array. The array size goes up to [9] and I am trying to read from the array so that if there is a value of 1 in the array between [0] and [9] it will add 1 to the counter.
I have got it working for just the first value in the array so I know it works, but I am not sure about how to make it read from all of the array to check for values of 1.
Heres my code so far:
if (clickTimes[0] == 1)
{
counter2++;
}
Better yet, use a for each...in loop. It's really no different from a regular for loop, but it does provide some synactic sugar.
for each (var i:int in clickTimes)
{
if (i == 1)
{
counter2++;
}
}
For the beauty of it...
counter2 = clickTimes.join('').replace(/0/g, '').length;
It puts all your values in one string, remove the zeros and counts the characters left.
var clickTimes:Array = [1,1,1,0,0,0,1,1,0] // 5 1's and 4 0's
var counter2:int = 0
clickTimes.forEach(function(obj:*){
if (obj == 1){
counter2++;
}
})
trace(counter2) // traces 5
What about using a loop?
for (var i:int = 0; i < clickTimes.length; ++i)
{
if (clickTimes[i] == 1)
counter2++;
}
This would increment counter2, for each element that has a value of 1, in the clickTimes array.
You do not need if. Just the following:
var clickTimes:Array = [1,1,1,0,0,0,1,1,0] // 5 1's and 4 0's
var counter2:int = 0;
for each (var i:int in clickTimes)
{
counter2 += i;
}
trace(counter2); //5