Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

Go Back   ActionScript.org Forums > Community Boards > Just for Kicks Challenges

Reply
 
Thread Tools Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
Old 05-20-2003, 12:15 AM   #1
mpol777
Registered User
 
mpol777's Avatar
 
Join Date: Jun 2001
Posts: 247
Default Challenge - Array Class

Description
Any custom method added to the Array Class.

Rules
Standard Rules

Format
ActionScript Code:
/* Method:     Method Name Description:     A short description of what your method does. Arguments:     argument - (type) description of the argument Return:     What the method returns */ Array.prototype.MyMethod = function() { } // EXAMPLE

If you don't have a .swf or .fla, paste an example of it being used. The example doesn't count towards the line limit.

The person with the best method gets a cookie. *








*To recieve prize winner must be between the ages of 93 to 106 years old. Please allow 12 years for delivery.
mpol777 is offline   Reply With Quote
Old 05-20-2003, 12:52 AM   #2
Billy T
Oops I did it again
 
Billy T's Avatar
 
Join Date: Oct 2001
Location: Melbourne
Posts: 8,578
Default

OK I don't know how useful this is or if its been done before (or even if you can do it with a built in method) but here goes

ActionScript Code:
/* Method:     remove Description: Extends the splice method a bit Arguments: 'item' can be either a number (which will be the index of the element you want to remove) or a string (which will be the value of the element you want to remove) Return: The array with 'item' removed. */ Array.prototype.remove = function(item) {     if (typeof item == "number") {         this.splice(item,1);     } else {         for(i=0;i<this.length;i++){             if(this[i]==item){                 this.splice(i,1);             }     }     }     return this; }; // EXAMPLE theArray = new Array("Bob", "Fred", "Jeff", "Monica"); trace(theArray); theArray.remove("Fred"); trace(theArray); theArray.remove(1); trace(theArray);

just extends the splice method a bit. Seems to me that removing 1 item from an array is pretty common and that's what this prototype does

cheers
__________________
Billy
Online Galleries
Free Flash Video Tutorials
Photo Website Template

Don't email or PM me questions...
Billy T is offline   Reply With Quote
Old 05-20-2003, 07:18 AM   #3
farafiro
Addicted To FLASH
 
farafiro's Avatar
 
Join Date: Dec 2001
Location: EGYPT
Posts: 12,439
Send a message via MSN to farafiro Send a message via Yahoo to farafiro
Default

ok, here is mine
basicly this to return a random value of an array so u can use it to pick the Array elements randomly
ActionScript Code:
posArray = new Array("0", "275", "550", "825", "1100", "1375"); function random_num(which_array) {     n = random(posArray.length);     return which_array[n]; } function go() {     newPos = random_num(posArray);     trace(newPos); } setInterval(go, 3000);
Example, sorry, this was meant to be off line, so u just wait for it about 10-20 sec
ActionScript Code:
//Example's script stop(); posArray = new Array("0", "275", "550", "825", "1100", "1375"); function random_num(which_array) {     n = random(posArray.length);     return which_array[n]; } function go() {     newPos = random_num(posArray);     _root.nextFrame();     if (_currentframe>5) {         gotoAndStop(2);     }     trace(newPos); } _root.onEnterFrame = function() {     currPos = nav._x;     if (newPos<currPos) {         friction = currPos-newPos;         nav._x = currPos-(friction*.2);         nav2._x = currPos-(friction*.7);     } else if (newPos>currPos) {         friction = newPos-currPos;         nav._x = currPos+(friction*.2);         nav2._x = currPos+(friction*.7);     } }; setInterval(go, 3000);
__________________
â€* GOD Is Near â€*
Questions Don't PM for Questions . Thanks
An eye for an eye, make the whole world blind
_____________________________________________GHANDI

Last edited by farafiro; 05-20-2003 at 07:24 AM..
farafiro is offline   Reply With Quote
Old 05-20-2003, 12:39 PM   #4
buggedcom
CONTEMPLATING LIFE
 
Join Date: Sep 2001
Location: i'm feeling slightly teddy-ish
Posts: 733
Send a message via AIM to buggedcom
Default mine

findPos

done a million times b4 but here you go

ActionScript Code:
/* METHOD : find DESCRIPTION : finds the positions in an array where the idex is the same as the value ARGUMENTS : value = a value RETURNS : an array containing the index nums of the found values */ Array.prototype.findPos = function(value) {     var tAr = [];     for (var i = 0; i<this.length; i++) {         if (this[i] == value) {             tAr[tAr.length] = i;         }     }     return (tAr.length>0) ? tAr : false; }; // EXAMPLE listRA = ["first", "second", "third", "first"]; var ar = listRA.findPos("first"); if (!ar) {     trace("not found"); } else {     trace("value 'first' found in positions : "+ar); }
__________________
~bugged
I am looking for freelance work

imageWeaver gallery
microsite direct download

play here microgame (unfinshed, also give it a while to load)
game
buggedcom is offline   Reply With Quote
Old 05-20-2003, 02:35 PM   #5
Billy T
Oops I did it again
 
Billy T's Avatar
 
Join Date: Oct 2001
Location: Melbourne
Posts: 8,578
Default

I think we need one from the challenge starter...
__________________
Billy
Online Galleries
Free Flash Video Tutorials
Photo Website Template

Don't email or PM me questions...
Billy T is offline   Reply With Quote
Old 05-20-2003, 02:53 PM   #6
farafiro
Addicted To FLASH
 
farafiro's Avatar
 
Join Date: Dec 2001
Location: EGYPT
Posts: 12,439
Send a message via MSN to farafiro Send a message via Yahoo to farafiro
Default

Quote:
Originally posted by Billy T
I think we need one from the challenge starter...
heheheheeeeeeee
yep, I think we do
__________________
â€* GOD Is Near â€*
Questions Don't PM for Questions . Thanks
An eye for an eye, make the whole world blind
_____________________________________________GHANDI
farafiro is offline   Reply With Quote
Old 05-20-2003, 05:50 PM   #7
mpol777
Registered User
 
mpol777's Avatar
 
Join Date: Jun 2001
Posts: 247
Default

OK here ya go.

I'm not exactly happy with it. It seems like there would be a good way without duplicating the array.

ActionScript Code:
/* Method:     insertAt Description: Inserts data into a particualr position into an array Arguments: data - can be pretty much anything.  string, number, boolean, object, etc. index - (number) the index to insert the data at Return: What the method returns */ Array.prototype.insertAt = function(data, index) {     oldArray = new Array();     oldArray = oldArray.concat(this);     count = 0;     for (i=0; i<oldArray.length; i++) {     if (i == index) {         this[count] = data;         count++;     }     this[count] = oldArray[i];     count++;     }     return this; } // EXAMPLE array1 = new Array("remington","winchester","glock","colt"); trace(array1); // returns "remington,winchester,glock,colt" array1.insertAt("browning",2); trace(array1); // returns "remington,winchester,browning,glock,colt"
mpol777 is offline   Reply With Quote
Old 05-20-2003, 06:35 PM   #8
littleRichard
Registered User
 
littleRichard's Avatar
 
Join Date: Feb 2003
Location: Florida
Posts: 289
Default

hate to say this, but you can do that much easier with the built in splice() method.

array1 = new Array("remington","winchester","glock","colt");
trace(array1);

array1.splice(2, 0, "browning");
trace(array1);
littleRichard is offline   Reply With Quote
Old 05-20-2003, 07:37 PM   #9
mpol777
Registered User
 
mpol777's Avatar
 
Join Date: Jun 2001
Posts: 247
Default

Quote:
Originally posted by littleRichard
hate to say this,
Don't hate the playa, hate the game.

That is much better. thanks.
mpol777 is offline   Reply With Quote
Old 05-20-2003, 08:06 PM   #10
Esquared
Registered User
 
Join Date: May 2002
Location: Pittsburgh, PA, USA
Posts: 342
Send a message via AIM to Esquared
Default I'll try my hand at a challenge...

ActionScript Code:
/* Method:     insertInOrder Description: inserts an object into a SORTED array in log(n) time (fastest possible: for instance, need < 10 comparisons for array of length 1024) Arguments: object - (anything) the object to be inserted order - (boolean) increasing if true, decreasing if false Return: the array containing the inserted object */ Array.prototype.insertInOrder = function(object, order) {     L = 0;     R = this.length;     M;     while(L < R)     {         M = Math.floor(L+(R-L)/2);                 if(object < this[M])         {             if(order) R = M;             else L = M+1;         }         else if(object > this[M])         {             if(order) L = M+1;             else R = M;         }         else             break;     }     this.splice(L, 0, object);     return this; } // EXAMPLE trace("BEFORE INSERTION:"); // increasing = new Array(1,2,3,4,5,6,8,9); trace("     -increasing: " + increasing)// returns "1,2,3,4,5,6,8,9"  // decreasing = new Array(9,8,6,5,4,3,2,1); trace("     -decreasing: " + decreasing)// returns "9,8,6,5,4,3,2,1"  // trace("AFTER INSERTION:"); // increasing.insertInOrder(7, true); trace("     -increasing: " + increasing); // returns "1,2,3,4,5,6,7,8,9" // decreasing.insertInOrder(7, false); trace("     -decreasing: " + decreasing); // returns "9,8,7,6,5,4,3,2,1"
Esquared is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
loading an object from an array NateVeronica ActionScript 2.0 1 03-30-2004 11:53 PM


All times are GMT. The time now is 04:30 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.
You Rated this Thread: