Page 5 of 9 FirstFirst ... 34567 ... LastLast
Results 61 to 75 of 129

Thread: Useful Code Snippets

  1. #61
    @wesball

    you would have to use a fadeIn function that i have not posted yet, if you get creative with the fade function, you can convert it into a working fadeIn function

  2. #62
    HEllo ALL, I am new to forum but have used for almost 7 months.
    I have an issue I think may be easy for some but for me it has given a bit of a head ache.

    I would like too: have a user select multiple radio buttons and depending on what they select have them gotoAndStop at a certain frame.
    I used a case switch to do this which works, but I am not able allow the user to select more or less than a given number
    CODE:

    var arr:Array = [];



    ////////////radiobuttons///////////////////

    var rbL:Object = {};

    rbL.click = function(obj:Object){

    var sel = obj.target;

    var dat = sel.data;

    trace(sel+" - data - "+dat);

    arr.push(dat);

    };



    for(var n:Number=1; n!=6; n++){

    var init:Object = {_x:50, _y:n*40+280, label:n, data:n, groupName:"rb"+n};

    var ref:MovieClip = this.attachMovie("RadioButton", "RB_"+n, n+1000, init);

    ref.addEventListener("click", rbL);

    }

    ////////////submit button///////////////////

    this.attachMovie("Button","btnSubmit",2000,{_x:75, _y:675, label:"Design Studio"});



    var Submit:Object = {};

    Submit.click = function(){

    var str:String = "";

    for(var m:Number=0; m!=5; m++){ // take first 3 entries only

    str += arr[m];

    }

    switch(str){

    case "12345":

    _root.gotoAndStop("vocal1");

    break;

    case "23451" :

    _root.gotoAndStop("vocal2");

    break;

    case "231" :

    _root.gotoAndPlay("studio1");

    break;

    case "213" :

    _root.gotoAndPlay("studio1");

    break;

    case "312" :

    _root.gotoAndPlay("studio1");

    break;

    case "321" :

    _root.gotoAndPlay("studio1");

    break;

    case "4321" :

    _root.gotoAndPlay("studio2");

    break;

    // and so on to cover all variations

    case "678" :

    trace("you selected 678");

    break;

    }

    };



    btnSubmit.addEventListener("click", Submit);



    ////////////reset button///////////////////

    this.attachMovie("Button","btnReset",2100,{_x:75, _y:730, label:"Reset"});



    var Reset:Object = {};

    Reset.click = function(){

    for(var n:Number=1; n!=5; n++){

    _root["RB_"+n].selected = false;

    }

    arr = [];

    };

    btnReset.addEventListener("click", Reset);

  3. #63
    217
    posts
    Registered User
    Quote Originally Posted by REEFˇ View Post
    ActionScript Code:
    function numParity(num:Number):String {
        num % 2 ? parity = "odd" : parity = "even";
        return parity;
    }


    Description: A function that returns the parity of a number. In mathematics, parity refers to whether a number is odd or even.

    Parameters:
    num:Number - The number to analyze.

    Returns:
    String - If the number is found odd, "odd" will be returned, otherwise, if the number is found even, "even" will be returned.

    Example Usage:
    ActionScript Code:
    function numParity(num:Number):String {
        num % 2 ? parity = "odd" : parity = "even";
        return parity;
    }
    trace(numParity(89)); // outputs odd
    trace(numParity(9000)); // outputs even


    How to do it in AS3 ?

  4. #64
    Quote Originally Posted by jbailey View Post
    How to do it in AS3 ?
    this is already in AS3
    Code:
    function numParity(num:Number):String {
    return num % 2 ? "odd" : "even";
    }

  5. #65
    217
    posts
    Registered User
    Quote Originally Posted by Felixz View Post
    this is already in AS3
    ActionScript Code:
    function numParity(num:Number):String {
    return num % 2 ? "odd" : "even";
    }

    WOW ... weird all last night I couldn't get it to work . Now it does in AS3 my bad and embarrassment - should have done more testing and with more sleep.

  6. #66

    Afrostyle MovieClip instance using getDefinitionByName

    Code:
        
    public static function getMovieInstance( skinName:String ):MovieClip {
                var classDef:Object = null;
                var instance:MovieClip=null;
                var skin:Object = skinName;
                if (skin is Class) { 
                    return (new skin()) as MovieClip; 
                }
                try {
                    classDef = getDefinitionByName(skin.toString());//try to find name in system
                    instance = new classDef();
                } catch(e:Error) { } //do nothing
                if (instance == null) { 
                    instance = new MovieClip();
                }
                return instance as MovieClip; //return it as a MovieClip
        }

    Description:
    A function used for returning a movieClip from the fla buy finding it name "getDefinitionByName" .

    Parameters:
    skinName:String - The name to find.

    Returns:
    MovieClip - new MovieClip instance of a found reference to the class object or blank movieclip if not found.

    Example Usage:
    Code:
        if( mainBgSkin)    { 
             mainHolder.removeChild(mainBgSkin); 
             mainBgSkin=null; 
        } 
        mainBgSkin =   getMovieInstance(  "bgSkinSymbol" );
        addChild(mainBgSkin );

  7. #67
    Hey everyone - a stupid little snippet, and one I'm sure everyone's come across, but I was looking a while back for a fast way to get the sign of a value, and eventually scratched this down:


    Code:
    function sign(val:Number):int
    {
         return (val < 0) ? -1:1;
    }
    How simple does that get, eh? lol, but it's actually helped me out a lot.. Hope I'm passing along something as useful to someone else

  8. #68

    not working properly

    Quote Originally Posted by nathan99 View Post
    verifyEmail function

    verifyEmail(address:String) : Object
    Reads the address parameter to a string and checks that it is compliant to the typical "valid" email standards of today.

    Parameters

    address:String - The email that you want to be validated. The function does not directly modify any variables.

    Returns

    Object- An Object value, whether or not the email is valid. If the email is invalid it will return the error, otherwise it will return true, or valid.

    Example

    The following demonstrates the usage of the validator over every condition, displaying every possible result. ActionScript Code:
    function verifyEmail(address:String):Object {
    if (address.indexOf("@") == -1) {
    return "Error: Invalid Address - No domain name entered.";
    } else if (address.indexOf(".") == -1) {
    return "Error: Invalid Address - No top-level domain entered.";
    } else if (address.indexOf("@")<5) {
    return "Error: Invalid Address - Address too short.";
    } else if (address.lastIndexOf(".")-5<=address.lastIndexOf("@")) {
    return "Error: Invalid Address - Domain name too short.";
    } else if (address.charAt(0) == "." || address.charAt(0) == "_" || address.charAt(0) == "-") {
    return "Error: Invalid Address - Address invalid.";
    } else if (address.split("@").length != 2) {
    return "Error: Invalid Address - Too many @s.";
    } else if (address.indexOf(" ") != -1 || address.indexOf("<") != -1 || address.indexOf(">") != -1 || address.indexOf("(") != -1 || address.indexOf(")") != -1 || address.indexOf("[") != -1 || address.indexOf("]") != -1 || address.indexOf("\\") != -1 || address.indexOf(",") != -1 || address.indexOf(";") != -1 || address.indexOf(":") != -1) {
    return "Error: Invalid Address - Invalid characters.";
    }
    return true;
    }
    trace(verifyEmail("test"));
    // Outputs Error: Invalid Address - No domain name entered. (There needs to be at least one @)
    trace(verifyEmail("tester@test"));
    // Outputs Error: Invalid Address - No top-level domain entered. (There needs to be at least one .)
    trace(verifyEmail("<a href="mailto:test@test.com">test@test.com</a>"));
    // Outputs Error: Invalid Address - Address too short. ("test" is not long enough to be a valid address)
    trace(verifyEmail("<a href="mailto:tester@test.com">tester@test.com</a>"));
    // Outputs Error: Invalid Address - Domain name too short. ("test" is not long enough to be a valid domain name)
    trace(verifyEmail("<a href="mailto:_tester@tester.com">_tester@tester.com</a>"));
    // Outputs Error: Invalid Address - Address invalid. (Addresses cannot start with .)
    trace(verifyEmail("<a href="mailto:-tester@tester.com">-tester@tester.com</a>"));
    // Outputs Error: Invalid Address - Address invalid. (Addresses cannot start with -)
    trace(verifyEmail("<a href="mailto:.tester@tester.com">.tester@tester.com</a>"));
    // Outputs Error: Invalid Address - Address invalid. (Addresses cannot start with _)
    trace(verifyEmail("<a href="mailto:tester@@tester.com">tester@@tester.com</a>"));
    // Outputs Error: Invalid Address - Too many @s. (Addresses can contain only one @ symbol)
    trace(verifyEmail("test <a href="mailto:er@tester.com">er@tester.com</a>"));
    // Outputs Error: Invalid Address - Invalid characters.(Addresses cannot contain a space)
    trace(verifyEmail("test:er@tester.com"));
    // Outputs Error: Invalid Address - Invalid characters.(Addresses cannot contain a
    trace(verifyEmail("test;er@tester.com"));
    // Outputs Error: Invalid Address - Invalid characters.(Addresses cannot contain a
    trace(verifyEmail("test>er@tester.com"));
    // Outputs Error: Invalid Address - Invalid characters.(Addresses cannot contain a >)
    trace(verifyEmail("test<<a href="mailto:er@tester.com">er@tester.com</a>"));
    // Outputs Error: Invalid Address - Invalid characters.(Addresses cannot contain a <)
    trace(verifyEmail("test\er@tester.com"));
    // Outputs Error: Invalid Address - Invalid characters.(Addresses cannot contain a \)
    trace(verifyEmail("test,er@tester.com"));
    // Outputs Error: Invalid Address - Invalid characters.(Addresses cannot contain a ,)
    trace(verifyEmail("test[er@tester.com"));
    // Outputs Error: Invalid Address - Invalid characters.(Addresses cannot contain a [)
    trace(verifyEmail("test]er@tester.com"));
    // Outputs Error: Invalid Address - Invalid characters.(Addresses cannot contain a ])
    trace(verifyEmail("test(<a href="mailto:er@tester.com">er@tester.com</a>"));
    // Outputs Error: Invalid Address - Invalid characters.(Addresses cannot contain a ()
    trace(verifyEmail("test)er@tester.com"));
    // Outputs Error: Invalid Address - Invalid characters.(Addresses cannot contain a ))
    trace(verifyEmail("<a href="mailto:tester@tester.com">tester@tester.com</a>"));
    // Outputs true, the email is valid


    I tested this code exactly and it was working as described. i had to take alot of the double quotes to single quotes so it would begin to work correctly but then it wouldn't respond correctly tot he right email address. anyway just an FYI....

  9. #69
    Cum shotsCumCum sexCum *** shotCum into *****
    How about regular expressions?
    Code:
    var email_pattern: RegExp =/^\w+([\-\w]+)*\w@\w+(([\.\-]\w)*\w+)*\.\w{2,4}$/i
    Last edited by ddffee1; June 19th, 2008 at 04:22 AM.

  10. #70
    Quote Originally Posted by REEFˇ View Post
    ActionScript Code:
    function factorial(num:Number):Number {
    var nShriek:Number = num;
    if (num > 0) {
    while (num > 1) {
    num--;
    nShriek *= num;
    }
    return nShriek;
    } else if (!num) {
    return 1;
    }
    }


    Description: A function that returns n! (factorial) from a positive integer or the amount of combinations possible from a set. nShriek is an informal way of saying nFactorial.

    Parameters:
    num:Number - The number to analyze.

    Returns:
    Number - n! or the amount of combinations possible.

    Example Usage:
    ActionScript Code:
    var setA:String = "cat";
    var setB:String = "apple";
    trace(factorial(setA.length + "combinations")); // outputs 6 combinations
    trace(factorial(setB.length + "combinations")); // outputs 120 combinations

    trace(factorial(0)); // outputs 1
    trace(factorial(1000)); // outputs Infinity
    trace(factorial(4); // outputs 24


    Wow, is this a joke? Besides the clumsy nature in which its written, it will break if you pass it a negative number, and will also exhibit odd and incorrect behavior if given a non-whole number (you did type it as a Number after all for a function that only accepts natural numbers). Try this:

    Code:
    function factorial(num:int):int {
        if (num < 1 || !num) return 0;
        var fact:int = 1;
        for (var i:int = 2; i <= num; i++) fact *= i;
        return fact;
    }

  11. #71
    TheCanadian's Avatar
    10,248
    posts
    Noo doot aboot it, eh?
    Well that was AS2.
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

    Maybe getTimer() or TweenMax is the answer to your problem . . .

  12. #72
    Code:
    function verifyEmail(address:String):Object {
     if (address.indexOf("@") == -1) {
      return "Error: Invalid Address - No domain name entered.";
     } else if (address.indexOf(".") == -1) {
      return "Error: Invalid Address - No top-level domain entered.";
     } else if (address.indexOf("@")<5) {
      return "Error: Invalid Address - Address too short.";
     } else if (address.lastIndexOf(".")-5<=address.lastIndexOf("@")) {
      return "Error: Invalid Address - Domain name too short.";
     } else if (address.charAt(0) == "." || address.charAt(0) == "_" || address.charAt(0) == "-") {
      return "Error: Invalid Address - Address invalid.";
     } else if (address.split("@").length != 2) {
      return "Error: Invalid Address - Too many @s.";
     } else if (address.indexOf(" ") != -1 || address.indexOf("<") != -1 || address.indexOf(">") != -1 || address.indexOf("(") != -1 || address.indexOf(")") != -1 || address.indexOf("[") != -1 || address.indexOf("]") != -1 || address.indexOf("\\") != -1 || address.indexOf(",") != -1 || address.indexOf(";") != -1 || address.indexOf(":") != -1) {
      return "Error: Invalid Address - Invalid characters.";
     }
     return true;
    }
    OH GOD, MY EYES! That is so, SO bad. And broken, too.

    Try this for size. This will work with any email you throw at it, and follows all the web standards for a proper email address (unlike the above):

    Code:
    //returns true is the email address is valid, false if not.
    function validateEmail(email:String):Boolean {
        var regexp:RegExp = /^\w[\w_\.\-\+]*?@\w([\w_\-\+]*?\.)+?[a-zA-Z]+$/;
        return !(regexp.exec(email) == null);
    }
    Last edited by LordMoyne; June 6th, 2008 at 12:26 AM.

  13. #73
    TheCanadian's Avatar
    10,248
    posts
    Noo doot aboot it, eh?
    Again, AS2.
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

    Maybe getTimer() or TweenMax is the answer to your problem . . .

  14. #74
    Quote Originally Posted by corbanb View Post
    I tested this code exactly and it was working as described. i had to take alot of the double quotes to single quotes so it would begin to work correctly but then it wouldn't respond correctly tot he right email address. anyway just an FYI....
    Quote Originally Posted by ddffee1 View Post
    Adult videosFree online anal sex videosFree sex moviesVideos of teen sex actsWives hidden sex videos*****LesbiansGayBig ****
    How about regular expressions?
    Code:
    var email_pattern: RegExp =/^\w+([\-\w]+)*\w@\w+(([\.\-]\w)*\w+)*\.\w{2,4}$/i

    There are numerous problems with that. It doesn't accept . _ and +, which are valid in all but the domain. It also allows numerals in the domain, which is invalid. It also requires the domain to be between 2 and 4 characters, which is not required.

  15. #75
    Quote Originally Posted by ajcates View Post
    Random Color
    This function will return a random color as a unit value.
    ActionScript Code:
    function randomColor():uint {
    &nbsp;&nbsp;&nbsp;&nbsp;var colorArray:Array = new Array("0x");
    &nbsp;&nbsp;&nbsp;&nbsp;for (var i:Number = 1; i<=3; i++) {
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;co lorArray[i] = Math.floor(Math.random() * (255 - 0 + 1)) + 0;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;co lorArray[i] = colorArray[i].toString(16);
    &nbsp;&nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;&nbsp;var unitNum:uint = new uint(colorArray.join(""));
    &nbsp;&nbsp;&nbsp;&nbsp;return unitNum;
    }



    Example
    This will draw a box 200 pixels wide by 200 pixels tall and fill it with a random color.
    ActionScript Code:
    function randomColor():uint {
    var colorArray:Array = new Array("0x");
    for (var i:Number = 1; i<=3; i++) {
    colorArray[i] = Math.floor(Math.random() * (255 - 0 + 1)) + 0;
    colorArray[i] = colorArray[i].toString(16);
    }
    var unitNum:uint = new uint(colorArray.join(""));
    return unitNum;
    }

    import flash.display.Graphics;
    import flash.display.Sprite;
    var box:Sprite = new Sprite();
    box.graphics.beginFill(randomColor());
    box.graphics.drawRect(0,0,200,200);
    box.graphics.endFill();
    addChild(box);
    Man, these things just get worse and worse. I'm not even going to ask what the deal is with that (255 - 0 + 1).

    Code:
    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.

Page 5 of 9 FirstFirst ... 34567 ... LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012