A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: Some cool actionscript 1-liners. Add yours!

  1. #1
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920

    Some valuable actionscript snippets. Add yours!

    A list of cool 1-3 line actionscript tricks. Add yours to this thread!

    Try to keep them to 3 lines or under...

    Code:
    // In a string, replace all occurences of '[' with '<'.
    txt = txt.split('[').join('<');
    
    // Randomly shuffle an array:
    myArray.sort(function(a,b){return random(3)-1;});
    
    // Randomly shuffle an array and get M items from it.
    myArray.sort(function(a,b){return random(3)-1;});
    mySubset = myArray.slice(0,M);
    
    // Swap two variables, x and y
    var tmp = x;        
    x = y;        
    y = tmp;
    
    // Swap two integers (no temporary value needed)
    x ^= y;   
    y ^= x; 
    x ^= y;
    
    // Create an array of N numbers from 1 to N.
    myArray = new Array();	
    for (var i = 1; i <= N; ++i)	
        myArray.push(i);
    
    // Convert r,g,b values to a single color value.
    myColor = (r << 16) | (g << 8) | b;
    
    // Make a random color
    myColor = random(0x1000000);
    
    // Lighten a dark color
    myColor |= 0x808080;
    
    // Darken a light color
    myColor &= 0x7F7F7F;
    - Jim
    Last edited by jbum; 03-11-2004 at 11:13 PM.

  2. #2
    Flashkit Jedi

    Join Date
    Jul 2001
    Location
    Coruscant System
    Posts
    3,426
    Here's some duplicating code:

    Code:
    mcnum++;
    duplicateMovieClip(this, "newname" + mcnum, mcnumn);
    _y = Math.random() * Stage.height;
    XFM

  3. #3
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    From some recent posts...

    Code:
    // Reverse the lines in a text field named myTextField
    myTextField.text = myTextField.text.split("\r").reverse.join("\r");
    
    // Shrink a movieclip 'mc' to a box the size of SQUARE x SQUARE
    mc._xscale = mc._yscale = 100*SQUARE/Math.max(mc._width,mc._height);
    - jbum

  4. #4
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    Generate an X coordinate in a random range with a gap in the middle. In this example, mc is placed either from 60-120 or from 330-390. Note: You don't need a loop to do this.

    Method 1:
    Code:
    var r = random(120);
    mc._y = r + ((r > 120)? 330 : 60);
    Method 2:
    Code:
    var r = random(120);
    mc._y = r + 60 + 110*(r > 120);
    Randomly position a movieclip within a rectangle from 10,5 to 200,120. Make sure entire movieclip fits within this area. Note that width of this rectangle is 190 and the height is 115.

    Code:
      mc._x = 10 + random(190 - mc._width);
      mc._y = 5 + random(115 - mc._width);

  5. #5
    ActionScript Insomniac
    Join Date
    Jan 2003
    Location
    43d03.21'N, 89d23.65'W
    Posts
    1,173
    Place movie clip number i at the ith place in a rectangular array. Running off the end of the first row puts you into the second row, etc.

    code:

    mc._x = (i % rowLength) * horizontalSpacing;
    mc._y = Math.floor(i / rowLength) * verticalSpacing;



    ----------------------------------------

    A lifesaver when you have trouble finding out what the exact path to a movieClip is:

    code:

    on(press) { trace(this); }



    Attach this to your movie clip, test the movie, and press on the movie clip to see its full name.

    ----------------------------------------

    Set the squares of a chessboard to alternating colors. Here, the square is in row i, column j.
    code:

    var c = new Color(this);
    c.setRGB( (i+j) % 2 ? 0xFFFFFF : 0);


  6. #6
    100% Bored
    Join Date
    Jul 2007
    Location
    Mzansi Africa
    Posts
    33
    learnt this one from "a_modified_dog";
    It is an itenary toggle switch.

    code:
    toggled = (pressed = !pressed) ? trace("on") : trace("off");

    The universe applauds Actionscript... not thoughts.

  7. #7
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156
    This is from the help files, not that complicated but can be useful. A function that returns a random number between a set minimum and maximum number.

    function randRange(min:Number, max:Number):Number {
    var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
    return randomNum; }
    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

  8. #8
    Member
    Join Date
    Jul 2007
    Posts
    77
    A great little scripted motion spring effect to be placed within a movie clip

    Code:
    var moveY:Number = 0
    this.onEnterFrame = function() {
    	moveY += (0-this._y)/3;
    	moveY *= 0.8
    	this._y += moveY;
    }

  9. #9
    Member
    Join Date
    Jul 2007
    Posts
    77
    Check if a number is odd or even

    Code:
    var evalNumber:Number = 3
    if ((evalNumber%2) == 0) {
    		//even
    	} else {
    		//odd
    }

  10. #10
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    to toggle a Boolean value
    for example the selected property of a Checkbox component
    Code:
    cb1.selected = true;
    btn.onRelease = function(){ cb1.selected = !(a=!a); };
    to compare two arrays -
    Code:
    if (Array1.join('')==Array2.join('')){ trace ('they are the same')}
    to sort a numerical array
    Code:
    function lowToHigh(a,b){ return a>b; };  arr.sort(lowToHigh); 
    function highToLow(a,b){ return a<b; };  arr.sort(highToLow);
    to find numbers within a range of numbers -
    Code:
    function inRange(a,b,c){ var d = a-b; var e = (d != 0) ? d/Math.abs(d) : 1;
    return (((c-b)-(d))*e>=0); };
    for(var ii=0; ii<25; ii++){ trace(ii+": "+(inRange(ii,5,20))); }
    to add a leading zero -
    Code:
    function pad(n){ return ("0"+String(n)).substr(-2, 2); };
    for(var i=0; i!=11; i++){ trace(pad(i)); }
    // for handling negative numbers -
    function pad(n){ var str = String(Math.abs(n));
    return (str.length >= 2) ? str : (n<0 ? '-' : '' ) +'0'+str; };
    for(var i=-5; i<=5; i++ ){ trace(pad(i)) }
    for more, visit the Code Bank link in my sig.

  11. #11
    Senior Member
    Join Date
    Nov 2004
    Posts
    928
    flip a mc

    mc._xscale*=-1;

  12. #12
    14yr old Member shavingcream's Avatar
    Join Date
    Jun 2007
    Location
    Santa Cruz CA
    Posts
    272
    pretty simple one put this on a mc and it will become the mouse

    onClipEvent(enterFrame){
    _y = _root._ymouse;
    _x = _root._xmouse;
    Mouse.hide();
    }

  13. #13
    14yr old Member shavingcream's Avatar
    Join Date
    Jun 2007
    Location
    Santa Cruz CA
    Posts
    272
    very simple and yet extremely usefull if you want to place more than one mc w/out the first one dissapearing type this before you place it

    var newDepth = ++depth;
    var name = "ball"+newDepth;
    var bullet = _root.attachMovie("ball", name, newDepth);

    ball is what i named the mc when i exported it for actionscript

  14. #14
    14yr old Member shavingcream's Avatar
    Join Date
    Jun 2007
    Location
    Santa Cruz CA
    Posts
    272
    this one is to repeat music using actionscript

    var my_music:Sound = new Sound();
    my_music.attachSound("music");
    my_music.onSoundComplete = function(){
    my_music.start();
    }
    my_music.start();

    to make this work import your music into the movie and right click (or control click) on it and select linkage then type in: music

  15. #15
    14yr old Member shavingcream's Avatar
    Join Date
    Jun 2007
    Location
    Santa Cruz CA
    Posts
    272
    not exactly 3 lines but this will allow you to draw


    this.createEmptyMovieClip("myClip",1);

    function changeColor(col){
    myClip.lineStyle(2,col,100);

    onMouseDown = function () {
    myClip.moveTo(_xmouse, _ymouse);
    onMouseMove = function () {
    myClip.lineTo(_xmouse, _ymouse);};
    };
    onMouseUp=function(){
    onMouseMove=null;
    }
    };

  16. #16
    14yr old Member shavingcream's Avatar
    Join Date
    Jun 2007
    Location
    Santa Cruz CA
    Posts
    272
    and to change the color put this script in

    red.onRelease = function(){
    changeColor(0xFF0000);
    };

    give your button and instance name on red

  17. #17
    14yr old Member shavingcream's Avatar
    Join Date
    Jun 2007
    Location
    Santa Cruz CA
    Posts
    272
    o and sorry the drawing one wont work unless you put this at the end

    changeColor(0x000000);

    and for those of you who just started to figure out the code for a color it will be shown when you change the color in the drawing tool next to the color at the top

  18. #18
    14yr old Member shavingcream's Avatar
    Join Date
    Jun 2007
    Location
    Santa Cruz CA
    Posts
    272
    put this on an mc and it will face the direction of your mouse


    onClipEvent(enterFrame){
    onMouseMove = function() {
    updateAfterEvent();
    xdiff = _root._xmouse-_x;
    ydiff = _root._ymouse-_y;
    angle = Math.atan2(ydiff, xdiff);
    angle = angle*180/Math.PI;
    _rotation = angle;
    }
    }

  19. #19
    Senior Member
    Join Date
    Sep 2000
    Posts
    272

    Don't know if it is cool....

    For the type of projects I work on this is very helpful. In the example below I am disabling _items 1 - 100. Can also use it for _visible, alpha, etc etc etc.

    function whatEverYouWant() {
    for (var i = 1; i<100; i++) {
    this["_item"+i].enabled = false;
    }
    }
    function whatEverYouWant2() {
    for (var i = 1; i<100; i++) {
    this["_item"+i].enabled = true;
    }
    }

    Cheers

  20. #20
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156
    gravity function that will make the item fall depending on the size of the object. The bigger the object, the faster it falls.

    Code:
    function gravity(clip){
    		clip._y += (clip._width+clip._height)/15
    }
    I just changed it to work with multiple clips. Still has a flaw as pointed out in the next post.
    Last edited by mneil; 07-11-2007 at 01:02 PM. Reason: didn't work with multiple clips
    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center