0

please inspect me coding:

function createRandomList():void
{
    var newlist:Array = [0,1,2];
    var curlist:Array = item[selectedlevel - 1] //selectedlevel = 1;
    var normal:int = curlist[0];
    var tempboo1:Boolean = false;
    var tempboo2:Boolean = false;
    var tempboo3:Boolean = false;
    while (curlist[0] + curlist[1] + curlist[2] > 0)
    {
        if (Number(curlist[0]) == 0 && tempboo1 == false)
        {
            newlist.splice(newlist.indexOf(0), 1);
            tempboo1 = true;
        }
        if (Number(curlist[1]) == 0 && tempboo2 == false)
        {
            newlist.splice(newlist.indexOf(1), 1);
            tempboo2 = true;
        }
        if (Number(curlist[2]) == 0 && tempboo3 == false)
        {
            newlist.splice(newlist.indexOf(2), 1);
            tempboo3 = true;
        }
    var temp:int = Math.floor(Math.random()*(newlist.length));
    curlist[temp] -=  1;
    generatedlist.push(Number(newlist[temp]));
        trace(item);
    }

    while (normal > 0)
    {
        var temp2:int = Math.floor(Math.random() * 3) + 1;
        generatednormal.push(Number(temp2));
        normal--;
    }
}

My item was [[5,0,0],[10,0,0]]; But after became [[0,0,0],[0,0,0]];

I just want to duplicate Array item to be a new variable curlist.

Every time it traces, returning item[0][0] decreasing 1, I only want to use curlist as a temp Array to calculate a new random Array based on item[0].

Ouput:

4,0,0,10,0,0
3,0,0,10,0,0
2,0,0,10,0,0
1,0,0,10,0,0
0,0,0,10,0,0

Is there any links between them, or is it my problem? Please help! If you need any more infoemation, please comment me!

3 Answers 3

1

Arrays are passed by reference, not value. That means when you modify an array through any property that points to it, the original array will be modified.

To make a duplicate, you can use .slice()

Returns a new array that consists of a range of elements from the original array, without modifying the original array. The returned array includes the startIndex element and all elements up to, but not including, the endIndex element.

If you don't pass any parameters, the new array is a duplicate (shallow clone) of the original array.

Sign up to request clarification or add additional context in comments.

2 Comments

your answer works, i'm sorry, but i preferred RafH 's answer.
@DanielCheung That's fine, just be cautious of overhead that isn't necessary. .slice() is specifically designed for duplicating an array.
0

You can clone your arrays if you want to create a new reference.

function clone( source:Object ):* 
{ 
    var myBA:ByteArray = new ByteArray(); 
    myBA.writeObject( source ); 
    myBA.position = 0; 
    return( myBA.readObject() ); 
} 

var a:Array = [[0,0],[1,1]];
var b:Array = clone(a);

b[0] = [2,2];

trace(a)
trace(b)

Output

0,0,1,1
2,2,1,1

It works for any object, not only arrays. More infos here : AS3 - Clone an object

2 Comments

For anyone stumbling upon this answer - be aware that this method is around 10-15x slower than using .slice().
Your deep cloning methods falls very short when having no primitives in your array. But for numbers & such, this is a simple solution, indeed, albeit slow, as Marty mentioned.
0
var array : Array = [ 1, 2, 3];
var array2 : Array = array.concnt();
array[ 0 ] = 4;
trace( array );// 1, 2, 3
trace( array 2);// 4, 2 ,3

So use .concat() to duplicate an array with primitives. If you have an arrays with arrays. Duplicate the children arrays, and put them into an empty one. If you have children of children arrays and so forth, make something recursive.

2 Comments

your answer works, i'm sorry, but i preferred RafH 's answer.
Sure, take care if you are adding complex objects like Sprite. RafH's method will either freeze your application and/or not work properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.