Home Tutorials Forums Articles Blogs Movies Library Employment Press

<< Prev 5 | Next 5

Array.distinctShuffle()

//Simple array randomiser...

Array.prototype.distinctShuffle = function() {
        this.sort(function(a,b) { i = Math.round((Math.random() * 100) - 50); return i;});
        return this;
};

var n = new Array(1,2,3,4,5,6,7,8,9,10);


trace(n);
n.distinctShuffle();
trace(n);
n.distinctShuffle();
trace(n);
n.distinctShuffle();
trace(n);
stop();

Posted by: John Rafferty | website http://www.hotmail.com
Array.distinctShuffle()
/*
I needed a script that would randomise an array and return a completely unique array.
Each element had to have a new position.

I came across a script entitled "Randomize an array with no similarities to previous layout"
at: http://www.actionscript.org/actionscripts_library/Array_Object/
but after testing it found that an error occurs when unshift() is used.
The resulting array is only completely unique in cases where unshift() isn't applied.

Here is my solution, tested and working in Flash 5 and above:
*/

/*-------------------------------------------------------------------*/

Array.prototype.distinctShuffle = function() {
        result = [];
        for (posArray=[], i=0; i<this.length; posArray[i]=i++);
        for (last=this.length-1; last>=0; last--){
                selected = this[last];
                rand = random(posArray.length-1);
                lastPos = posArray.getPos(last);
                if (lastPos == null){
                        result[posArray[rand]] = selected;
                        posArray.splice(rand, 1);
                }else{
                        posArray.splice(lastPos, 1);
                        result[posArray[rand]] = selected;
                        posArray.splice(rand, 1);
                        posArray.push(last);
                }
        }
        return result;
};

Array.prototype.getPos = function(item){
        for(i=0; i<this.length; ++i) {
                if (this[i] == item) { return i; }
        }
        return null;
};

/*-------------------------------------------------------------------*/


// Usage

Array.prototype.checkError = function(nArray) {
        for (i=0; i<this.length; i++) {
                if (this[i] == nArray[i]) { return "found"; }
        }
        return "none"
};

a = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
b = a.distinctShuffle();

trace("original array : " + a);
trace("shuffled array : " + b);
trace("error: " + a.checkError(b));
trace("------------------------------------");

/*-------------------------------------------------------------------*/

Posted by: Roland Levy | website http://www.rolplay.com
Array.fromFile
//File data format the same as for LoadVars :)

Array.prototype.fromFile = function(file) {
        this.__proto__.__proto__=LoadVars.prototype
        this.load(file);
};

myArray=[]
myArray.fromFile("data.txt")


// enjoy!

Posted by: aceed | website http://prototype.prv.pl
Array.inArray()
Array.prototype.inArray = function (value)
// Creates a new method for the array object.
// Returns true if the passed value is found in the
// array -- by matching for identity, not similarity.
// Returns false if the item is not found.
{
        var i;
        for (i=0; i < this.length; i++) {
                // Matches identical (===), not just similar (==).
                if (this[i] === value) {
                        return true;
                }
        }
        return false;
};

Posted by: Mike Brittain | website http://www.embimedia.com/
Array.moveElement(index,num)
//Array.moveElement(index,num)
//	index - element in array to move
//	num - move Array elements up(postive num, towards index 0) or down(negative num, towards end of array)
Array.prototype.moveElement = function(index,num){
        var num=int(num);
        if(num>0){ //move upward num times
                var i=index;
                while(i>index-num){
                        if(i==0) break;
                        var e = this[i];//this.slice(i,i+1);
                        this.splice(i,1);
                        this.splice(i-1,0,e/*.slice()*/;
                        i--;
                }
        }else if(num<0){ //move downward num times
                var i=index;
                while(i<index-num){
                        if(i==this.length-1) break;
                        var e = this[i];//this.slice(i,i+1);
                        this.splice(i,1);
                        this.splice(i+1,0,e/*.slice()*/;
                        i++;
                }
        }
}
ASSetPropFlags(Array.prototype,"moveElement",1); //hide from for... in

//example
myArray = ["hello","goodebye",["one","two","three"],"stay awhile"];
trace(myArray);
myArray.moveElement(1,-5);
trace(myArray);

Posted by: Aaron Beall | website http://abeall.com

<< Prev 5 | Next 5

Copyright 2000-2010 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.