Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Page 3 of 9 FirstFirst 12345 ... LastLast
Results 31 to 45 of 129

Thread: Useful Code Snippets

  1. #31
    It's easier to link than it is to repeat x 50000 times

  2. #32
    133
    posts
    me.isCoding = true;
    This might be of assistance to some.

    http://www.kirupa.com/forum/showthre...ight=url+query

  3. #33
    imvain2's Avatar
    96
    posts
    I'm Vain aren't you?

    Clicking Dynamic MovieClip

    I was having problems with clicking on dynamic movieclips, the FAQ here didn't give me my answer, so I searched and found this to work.

    You can use this code to for more then one dynamic movieclip, by creating unique names for each movieclip, interval and setbutton.

    myDynamicMC = this.createEmptyMovieClip("myMC", 130);
    myDynamicMC.loadMovie("image.jpg");

    var interval:Number;
    this.myDynamicMC.onEnterFrame = function() {
    if(this.myDynamicMC.getBytesLoaded() == this.myDynamicMC.getBytesTotal()) {
    interval = setInterval(this, "setButton", 100);
    }
    }
    function setButton() {
    this.myDynamicMC.onRelease = function() {
    trace("I clicked, therfor I'am");
    }
    clearInterval(interval);
    }

  4. #34
    TheCanadian's Avatar
    10,305
    posts
    Noo doot aboot it, eh?
    The FAQ thread does answer your question:
    http://www.kirupa.com/forum/showpost...0&postcount=21
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

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

  5. #35
    imvain2's Avatar
    96
    posts
    I'm Vain aren't you?
    Quote Originally Posted by TheCanadian View Post
    The FAQ thread does answer your question:
    http://www.kirupa.com/forum/showpost...0&postcount=21

    I could never get that code to work. But the code that I posted worked for me.

  6. #36
    getAction (getAction method)

    getAction:Object
    Returns an the type of modification (paste or cut) done to a text field, returning a relative result. If neither a paste nor cut action are made, this function will return false. This method also will read a response even if changes are made using the text selection -> right-click -> modify methods. Text selections are done in the onMouseMove event and are vital to the code. To place more in the onMouseMove event, simply insert it around the current actions.


    Parameters
    field:TextField - The name of the text field from which the actions will be read.
    Example

    The following example creates a new input text field and places the phrase "lorum ipsum..." into it. It then calls the getAction method within the onChanged event of the text field and traces the result if it does not return false:
    Code:
    function getEditAction(field:TextField):Object {
     if ((!Key.isDown(Key.getCode()) && field.text.length != txtA.length+txtB.length) || (Key.isDown(Key.CONTROL) && Key.isDown(86))) {
      return "pasted";
     } else if ((!Key.isDown(Key.getCode()) && field.text.length == txtA.length+txtB.length) || (Key.isDown(Key.CONTROL) && Key.isDown(88))) {
      return "cut";
     }
     return false;
    }
    onMouseMove = function ():Void {
     var beginIndex:Number = Selection.getBeginIndex();
     var endIndex:Number = Selection.getEndIndex();
     var place:String = Selection.getFocus();
     txtA = eval(place).text.substr(0, beginIndex);
     txtB = eval(place).text.substr(endIndex, eval(place).text.length);
    };
    this.createTextField("edit_txt", this.getNextHighestDepth(), 10, 10, 160, 120);
    edit_txt.border = true;
    edit_txt.multiline = true;
    edit_txt.type = "input";
    edit_txt.text = "lorum ipsum...";
    edit_txt.onChanged = function():Void  {
     if (getEditAction(this)) {
      trace(getEditAction(this));
     }
    };
    Last edited by nathan99; February 28th, 2007 at 08:40 PM.

  7. #37
    wordCount (wordCount method)
    wordCount : Number
    Returns the amount of words in the string argument.

    Parameters
    string : String - The string in which the wordcount will be taken.

    Example
    The following example count the number of words in the string "An apple a day keeps the doctor away", and should return 8 (An,apple,a,day,keeps,the,doctor,away):
    Code:
    function wordcount(string:String):Number {
        var tmp:Array = string.split(" ");
        for (var i = tmp.length; i>0; i--) {
            if (tmp[i] == "") {
                tmp.splice(i,1);
            }
        }
        return tmp.length;
    }
    trace(wordcount("An apple a day keeps the doctor away"));

  8. #38
    is_str (is_str method), is_numeric(is_numeric method)

    is_str : Boolean
    Returns true if the what argument is a string, or can be used as a string. If the containsNum arguments is true it will also see if there are any numbers within the string. If the containsNum argument is true and there are numbers, or the what argument is a number the is_str() method will return false.

    Parameters
    what : Object - The data to check.
    containsNum : Boolean - See if there are any numbers in the what argument.

    is_numeric : Boolean
    Returns true if the what argument is a number. If the parsing arguments is true it will also see if, when parsed, what is a number. If the parsedargument is true and what can be parsed properly to return a number is_numeric () will return true, otherwise the what argument can never be a numberis_numeric () method will return false.

    Parameters
    what : Object - The data to check.
    parsing: Boolean - See if the what argument is a number when it's parsed.

    Example
    The following shows the usage of both functions:
    Code:
    function is_str(what:Object, containsNum:Boolean):Boolean {
    	if ((containsNum ? isNaN(parseInt(String(what))+1) : true) && what+1 === what+"1") {
    		return true;
    	}
    	return false;
    }
    function is_numeric(what:Object, parsing:Boolean):Boolean {
    	if (!isNaN((parsing ? parseInt(String(what)) : what)+1)) {
    		return true;
    	}
    	return false;
    }
    trace(is_str(1)); // returns false (not a string)
    trace(is_numeric(1)); // returns true (is a number)
    trace(is_str(1+"str")); // returns true (is a string)
    trace(is_numeric(1+"str")); // returns false (not a number)
    trace(is_numeric(1+"str", true)); // returns true (not a number, but there is a number within it)
    trace(is_str(1+"str", true)); // returns false (is a string, but contains a number)

  9. #39
    distanceBetween (distanceBetween method)

    distanceBetween : Number
    Calculates the real distance between 2 points.

    Parameters
    x1 : Number - The x of the first point.
    y1 : Number - The y of the first point.
    x2 : Number - The x of the second point.
    y2 : Number - The y of the second point.

    Example
    The following example creates 2 movie clips and calculates the distance between them using the distanceBetween() method:
    Code:
    function distanceBetween(x1:Number, y1:Number, x2:Number, y2:Number):Number {
     xx = x2-x1;
     yy = y2-y1;
     return Math.abs(Math.sqrt((xx*xx)+(yy*yy)));
    }
    this.createEmptyMovieClip("MC1", this.getNextHighestDepth());
    MC1.beginFill(0x000000);
    MC1.lineTo(2, 0);
    MC1.lineTo(2, 2);
    MC1.lineTo(0, 2);
    MC1.lineTo(0, 0);
    MC1._x = 50;
    MC1._y = 100;
    this.createEmptyMovieClip("MC2", this.getNextHighestDepth());
    MC2.beginFill(0x000000);
    MC2.lineTo(2, 0);
    MC2.lineTo(2, 2);
    MC2.lineTo(0, 2);
    MC2.lineTo(0, 0);
    MC2._x = 10;
    MC2._y = 10;
    trace(distanceBetween(MC1._x, MC1._y, MC2._x, MC2._y)); // returns 98.49
    Last edited by nathan99; February 28th, 2007 at 03:25 AM.

  10. #40
    TheCanadian's Avatar
    10,305
    posts
    Noo doot aboot it, eh?
    Cool, I'll add those up when I have some energy

    Oh yeah, it's kind of redundant to take the absolute value of a square root since it's always positive anyways.
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

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

  11. #41
    Good point.

  12. #42
    autoFormat (autoFormat method)

    autoFormat : String
    Formats a text string. All sentences are started with a capital, all Is are capitalised (ie: so I did this) and sentences can all be spaced to a specified amount of spaces.

    Parameters
    what : String - The string to format.
    numSpaces : Number - How many space to insert after full stops, default is 1.

    Example
    The following example will output the string "this is a test sentence.i'm experimenting with stuff. if i were you i'd be happy. lol. " seperated by 2 character and autoFormatted using the autoFormat() method:
    Code:
    function autoFormat(what:String, numSpaces:Number):String {
     var tmp:Array = what.split(".");
     if (!numSpaces) {
      numSpaces = 1;
     }
     var tmpStr:String = "";
     for (var i:Number = numSpaces; i>0; i--) {
      tmpStr += " ";
     }
     for (var i in tmp) {
      while (tmp[i].charAt(0) === " ") {
       tmp[i] = tmp[i].slice(1, tmp[i].length);
      }
      tmp[i] = tmp[i].charAt(0).toUpperCase()+tmp[i].slice(1, tmp[i].length);
     }
     return tmp.join("."+tmpStr).split(" i ").join(" I ").split(" i'").join(" I'");
    }
    trace(autoFormat("this is a test sentence.i'm experimenting with stuff. if i were you i'd be happy.      lol.", 2));//Outputs "This is a test sentence.  I'm experimenting with stuff.  If I were you I'd be happy.  Lol."

  13. #43

    accordion component with 3 child list components

    This code creates an accordion component with 3 list components as its children. Special note: if you use this code you must first drag to your stage an instance of the accordion and list components then delete them to place them in your library.

    http://www.kirupa.com/forum/showpost...28&postcount=2

  14. #44
    TheCanadian's Avatar
    10,305
    posts
    Noo doot aboot it, eh?
    Oh man I'm falling behind, when I get a spare moment I'll add these up
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

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

  15. #45
    centerEasing (centerEasing method)
    public centerEasing(x:Boolean, y:Boolean, [easing:Boolean, exclusions:Boolean, speed:Number], [targets...])

    Tells flash to keep content within a flash movie centered. This method allows to to target individual objects only, or exclude certain objects, and by defualt will affect every movie clip on the main stage. The _lockroot property for the _root is set to false.

    Parameters
    x : Boolean - A true or false value that specifies whether to center along the x.
    y : Boolean - A true or false value that specifies whether to center along the y.
    easing : Boolean - A true or false value which specifies whether to ease to the center.
    speed : Number - If easing is set to true, this will specify how fast it eases.
    exclusions : Boolean - Whether or not to center the target objects specified. If exclusions is true, flash will not center the target objects, if set to false, flash will only center the targets.
    targets - A list of movie clips that you want excluded/included. See the exclusions parameter.




    The Class
    Code:
    class com.centerObjects.centerEasing {
     private var allMcs:Array;
     private var recordArguments:Array;
     private var xx:Number;
     private var yy:Number;
     public function centerEasing(x:Boolean, y:Boolean, easing:Boolean, speed:Number, exclusions:Boolean) {
      recordArguments = arguments;
      Stage.align = "tl";
      _root._lockroot = false;
      if (exclusions) {
       allMcs = [];
       for (var i in _root) {
        if (typeof (_root[i]) == "movieclip" && !excluding(_root[i], arguments.slice(5, arguments.length))) {
         _root[i].posX = _root[i]._x-(Stage.width/2);
         _root[i].posY = _root[i]._y-(Stage.height/2);
         allMcs.push(_root[i]);
        }
       }
      } else {
       allMcs = arguments.slice(5, arguments.length);
       for (var i in allMcs) {
        allMcs[i].posX = allMcs[i]._x-(Stage.width/2);
        allMcs[i].posY = allMcs[i]._y-(Stage.height/2);
       }
      }
     }
     public function refresh():Void {
      for (var i in allMcs) {
       allMcs[i].rent = this;
       allMcs[i].onEnterFrame = function():Void  {
        if (this.rent.recordArguments[0]) {
         this._x = this.rent.recordArguments[2] ? this._x+((this.posX+(Stage.width/2)-this._x)/this.rent.recordArguments[3]) : this.posX+(Stage.width/2);
        }
        if (this.rent.recordArguments[1]) {
         this._y = this.rent.recordArguments[2] ? this._y+((this.posY+(Stage.height/2)-this._y)/this.rent.recordArguments[3]) : this.posY+(Stage.height/2);
        }
        if (this.rent.distanceBetween(Math.round(this._x), Math.round(this._y), (this.rent.recordArguments[0] ? Math.round(this.posX+(Stage.width/2)) : this._x), (this.rent.recordArguments[1] ? Math.round(this.posY+(Stage.height/2)) : this._y))<=1) {
         delete this.onEnterFrame
        }
       };
      }
     }
     private function distanceBetween(x1:Number, y1:Number, x2:Number, y2:Number):Number {
      xx = x2-x1;
      yy = y2-y1;
      return Math.sqrt((xx*xx)+(yy*yy));
     }
     private function excluding(what:MovieClip, between:Array):Boolean {
      for (var i in between) {
       if (between[i] == what) {
        return true;
       }
      }
      return false;
     }
    }
    Example
    The following example creates a new object named resizeListener and uses it as a listener on the stage and waits for the Stage, or flash movie, to be resized. On resize it will call the centerEasing.refresh() method that will tell every movieclip to start centering again. Every movieclip on the Stage except for one named contentMC will be told to ease at a speed of 5 and only along the x axis.
    Code:
    import com.centerObjects.centerEasing;
    var controlCenter:centerEasing = new centerEasing(true, false, true, 5, true, contentMC);
    var resizeListener:Object = {};
    Stage.addListener(resizeListener);
    resizeListener.onResize = function() {
     controlCenter.refresh();
    };
    FLA Attached.
    Attached Files Attached Files
    Last edited by nathan99; March 7th, 2007 at 03:18 AM.

Page 3 of 9 FirstFirst 12345 ... 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