A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [F8] Functions

  1. #1
    Member
    Join Date
    Jun 2007
    Posts
    84

    [F8] Functions

    How do I use function? I wanna make a function that says that a few blocks will move left, so instead of writing :
    _root.block1._x -= 10
    _root.block2._x -= 10
    _root.block3._x -= 10
    _root.block4._x -= 10
    _root.block5._x -= 10

    I just wanna put em all together in a function so I can move em all. (and they aren't just 5, way more.

  2. #2
    Professional Air Guitarist Frag's Avatar
    Join Date
    Dec 2002
    Location
    Brain, Body, Clothes, Computer Chair, Room, House, Neighborhood, City, State, Country, Continent, World, Galaxy, Universe, and on?
    Posts
    811
    PHP Code:
    var blockNum:int 5;//however many you have
    function moveIt():void {
         for (var 
    i:int=1i<=blockNumi++)
              
    _root["block"+i]._x -= 10;
    }
    moveIt(); 
    That should do it.

  3. #3
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    You can make it self contained and send values as arguments to make it flexible for other situations where you need this with other clips:

    PHP Code:
    function moveFlexiblesIDName:StringnDistanceX:intnDistanceY:intnNumberToMove:intmcTarget:MovieClip ) : void {

       
    // default to _root if no target mc specified
       
    mcTarget mcTarget == undefined _root mcTarget;

       
    // loop through the clips and apply co-ordinate changes
       
    for( var i:int 1<= nNumberToMovei++ ) {
          
    mcTargetsIDName ]._x += nDistanceX;
          
    mcTargetsIDName ]._y += nDistanceY;
       }
    }
    moveFlexible"block"1005_root ); 
    RipX

  4. #4
    Member
    Join Date
    Jun 2007
    Posts
    84
    RipX, That's a little complicated for me, plus it would be easier to name them all block1, block2 etc...

    Oh and another question, if I put this code whenever I want them to move:

    var blockNum:int = 5;
    for (var i:int=1; i<=blockNum; i++)
    _root["block"+i]._x -= 10;

    would it work? Instead of using functions?

  5. #5
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    Yes, but it would become messy very quickly...the idea is that you can call the function with a single reference to that function.


    PHP Code:
    function sayHello( ) : void {
        
    trace"Hello" );
    }

    // call the function
    sayHello( );

    // call it again
    sayHello( );

    // one more time
    sayHello( ); 
    RipX

  6. #6
    Member
    Join Date
    Jun 2007
    Posts
    84
    Riiiiiiiiiiiiiiiight, so how do I call the function you posted?

    moveFlexible();

    only or what?

  7. #7
    Senior Member
    Join Date
    Jun 2002
    Location
    Manchester, UK
    Posts
    2,357
    Well...yes, but there is a little bit more to it. You can add arguments to functions, arguments are values that can be passed and reference inside your function to make it more reusable (reading this next bit thoroughly will save you a lot of head scratching)...

    for example:

    PHP Code:
    function squareNumbernNumber:Number ) : void {
       
    tracenNumber nNumber );
    }

    // trace : square the number 7. - traces 49
    squareNumber );

    // trace : sqare the number 89. - traces 7921
    squareNumber 89 ); 
    So as you can see this function can accept an argument and is re-usable, in this case the argument is a number, but you may pass more than 1 argument as a commer delimited list such as:

    PHP Code:
    function addNumbersnArgument1:NumbernArgument2:Number ) : void {
       
    tracenArgument1 nArgument2 );
    }

    addNumbers2012 ); // traces 32

    addNumbers456.65 ); // traces 51.65 
    And one last one for you...see the 'void' keyword in the previous examples? Well this is there because these functions do not return a value, but a function may return a value to store inside a variable, such as:

    PHP Code:
    function parseTwoWordssString1:StringsString2:String ) : String {
      return 
    sString1 " " sString2;
    }

    var 
    sMyParsedString:String parseTwoWords"Hello""world!" );

    // trace the variable where you stored the result of the function call.
    tracesMyParsedString ); // traces "Hello world!" 
    So, now you should know how to call that function

    RipX

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