Useful Code Snippets

I figured it might be nice to have a thread where people can post some useful AS code snippets for all to see. If you have some useful code, by all means post it in this thread and I will index it in this post ASAP. If you do post some code, please give a short description on how to use it. A sample would be nice too.

:mountie:

[SIZE=3]String[/SIZE]
strReplace - A function used for replacing portions of a string with a new one.
wordCount - Returns the amount of words in the argument.
is_str - Returns [FONT=Courier New][FONT=Verdana]true[/FONT] [/FONT]if the argument is a string, or can be used as a string.
autoFormat - All sentences are started with a capital, all "I"s are capitalised and sentences can be spaced.

[SIZE=3]Array[/SIZE]
arrIndexOf - A function used for finding the index of the first occurance of an object in an array.
arrLastIndexOf - A function used for finding the index of the last occurance of an object in an array.
remove - Removes the specified index element from an array.
shuffle - Randomises the contents of any array between the startIndex and endIndex indices.

[SIZE=3]MovieClip[/SIZE]
createClassMovieClip - A function used for creating empty movie clips with subclass association.
_brightness - Property for setting brightness of a movieclip (like in the IDE). Value range from -100% to 100%.
_recordframerate - Records the approximate frame rate.
fadeOut - Fades a movie clip out of the stage, based on the fade speed.

[SIZE=3]Number/Math[/SIZE]
numParity - A function that returns the parity of a number. In mathematics, parity refers to whether a number is odd or even.
factorial - A function that returns n! (factorial) from a positive integer or the amount of combinations possible from a set.
numDerive - A function for approximating the derivative of a function at a point.
numRoundTo - A function for rounding a decimal number to a precision.
addCommas - Converts the parameter to a string and formats the number value correctly with decmals where necessary.
ordinalise - Reads the numberparameter and converts it into an ordinalised String.
is_numeric - Returns [FONT=Courier New][FONT=Verdana]true[/FONT] [/FONT]if the argument is a number.
distanceBetween - Calculates the distance between 2 points.

[SIZE=3]Sound[/SIZE]
pause - Stops a playing sound at it’s current position and allows access to a variable [FONT=Courier New]newPosition[/FONT][FONT=Verdana] which enables the sound to resume at it’s last position.[/FONT]

[SIZE=3]Color[/SIZE]
setBrightness - Method for setting brightness of a Color object. Value range from -100% to 100%.

[SIZE=3]Object[/SIZE]
**[SIZE=3]_[/SIZE]**recordDownloadSpeed - Monitors how fast an Object is downloading

[SIZE=3]TextField[/SIZE]
getAction - Returns an the type of modification (paste or cut) done to a text field.

[SIZE=3]Drawing Methods[/SIZE]
drawStar - A function used to draw stars in a movie clip.

[SIZE=3]Miscellaneous[/SIZE]
verifyEmail - Reads the address parameter to a string and checks that it is compliant to the typical “valid” email standards of today.
alertPopup - This creates a nice looking draggable popup.

[SIZE=3]Classes[/SIZE]
com.Javascript.Request - URL query vars class.
com.centerObjects.centerEasing - Tells flash to keep content within a flash movie centered.
com.dragging.Dragger - This class is designed to move objects in a grid.

[SIZE=3]Code Snippets[/SIZE]
Custom ContextMenu - A script that hides the original right-click menu’s playback & zoom functions and allows you to add your own.
Accordian Component with Child List Components - This code creates an accordion component with 3 list components as its children.
Display Current Frame - A movieclip that you can place anywhere in your flash movie that will automatically display the current frame.

[size=2]Huh.Do you hunt one place can buy small order,custom made dance dress?[/size][size=2]maybe right now you dont need those info,but in future those info is good for you.[/size][size=2]Please keep this in your favour.Also you can recommend to your friend.[/size][size=2]we are china garment factory to make dance dress**,**[url=http://www.smartsgarment.com/catalogue/]latin dance dress.[/size][size=2][/size] [size=2]Small order is acceptable,Custom Made is Ok.[/size]

Well that was AS2.

Again, AS2.

RegEx was not part of AS2. Keep your pants on, newcomer. :wink:

As far as a random color, what do you have against:


Math.random()*0xFFFFFF

[QUOTE=LordMoyne;2337799]Why are they posted and stickied in the AS3 forums? Not that it even matters, because even in AS2 they are broken. Both the factorial and the email validator post unexpected behavior for certain inputs, or in the case of the email validator don’t work on a slew of cases. They are also painfully slow and are really bad examples of basic code that gets read by thousands of people. People should NOT be programming like the above examples.[/QUOTE]

Because this thread was created before AS3 was released and it got moved here for whatever reason.

I have nothing against you posting code and making corrections; there’s just no need to be a **** about it.

^

also 0! == 1
and they are permutations without repetitions, not combinations

Ugliest thread EVAR!

[quote=LordMoyne;2337794]Man, these things just get worse and worse. I’m not even going to ask what the deal is with that (255 - 0 + 1).


function getRand():uint {
    return ((Math.floor(Math.random() * 256)<<16) + (Math.floor(Math.random() * 256)<<8) + Math.floor(Math.random() * 256));
}

For comparisons sake, 1,000,000 iterations of my code ran in 750ms. 1 million of yours took nearly 10 seconds.[/quote]

  public static function generateColor():int {
   return Math.ceil(Math.random() * 0xFFFFFF);
  }

[size=2]Some [color=#0000ff]Runescape[/color] Players come to our site and ask us if we have to get [color=#0000ff]runescape Autotyper[/color] or [color=#0000ff]runescape hacks[/color]. We alwsys told them that there is no [url=http://www.runescape2gold.net/][color=#0000ff]runescape gold[/color] hacks or [color=#0000ff]runescape account[/color] hacks or [url=http://www.runescape2gold.net/][color=#0000ff]Runescape Money[/color] here. we’re a professional [color=#0000ff]gold sellin[/color]g site, we have no time and interests to scam u, we only sell [color=#0000ff]Runscape Gold[/color].Ur acc is no use for us. We do this for clients hundres of times every day.We can guarantee we’re a site u can trust and u’ll come back again after this first successful business.In forward to know more abount what is is [color=#0000ff]runescape Autotyper[/color]. So I read some articles. There are some information about [color=#800080]RuneScape[/color] Autoty per. Shared!Just passing on the love.This is the best AutoTyper I swear to god. Undetecdable.To download do the following.Click on the linkhttp://www.megaupload.com/?d=BW7X98OPOn the top right corner type the numbers/letters shown into the box to the right of it.Then click enter. The download will start.NEW!:AutoWcer:Do the same as the AutoTyper.Auto WcerI found a auto wcer just for you! I just didnt think anyone was intrested I tested it, and it works properly, like the auto typer.http://www.megaupload.com/?d=2I5NINP4Post comments after you have tried out this auto typer and/or wcer!Most macro programs have key loggers though and virus, should always scan your computer after trying one to double check.How come im banned?!?I’m trying to spread the love of auto ing. Autoers WILL take over [color=#0000ff]Runescape[/color] and you cant stop it.Do u have to download the toolbar first?[color=#800080]Runescape News[/color] More from www.runescape2gold.net… [color=#800080]More>>[/color] [/size]