Flash / Flex / ActionScript/Function/function

Материал из Web эксперт
Перейти к: навигация, поиск

Accessing Definitions from Within a Function

 
public function m (  ) {
  var currentObject = this;
  function f (  ) {
    trace(currentObject);  
  }
}



A function that calculates the area of a rectangle and returns the value as a number

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(calculateArea(2,3));
    }
    function calculateArea(nA:Number, nB:Number):Number {
      var nArea:Number = nA * nB;
      return nArea;
    } 
  }
}



Calling Functions

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
      displayGreeting();
    }
    function displayGreeting():void {
      trace("Hello.");
    }
  }
}



Creating Recursion function

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace(factorial(3));
    }
    function factorial(nOperand:Number):Number{
      if(nOperand > 0){
        return nOperand * factorial(nOperand - 1);
      }
      else{
        return 1;
      }
    }
  }
}



Define a function

 
/*
function functionName():datatype {
  statements
}
*/
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
       displayGreeting();
    }
    function displayGreeting():void {
      trace("Hello.");
    } 
  }
}



Functions as Objects

 
package{
  import flash.display.Sprite;
  import flash.events.MouseEvent;
  public class Main extends Sprite{
    public function Main(){
        addEventListener(MouseEvent.CLICK, onClick);
    }
    function onClick(event:MouseEvent):void {
         trace(" Handle click.");
    }
  }
}



Function to search an array

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

    }
    function findMatchingIndex(aTitles:Array,
                               sTitle:String):Number
    {
      for(var i:Number = 0; i < aTitles.length; i++) { 
    
        if(aTitles[i] == sTitle) {
          return i;
        }
      } 
    
      return null;
    }
  }
}



Global Functions

 
package {
  import flash.system.*;
  public function isLinux (  ) {
    return Capabilities.os == "Linux";
  }
}



Nested function call

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        function1();
        function2();
    }
    function function1():void{
        function2();
    } 
    function function2():void{
      if(arguments.caller == function1)
        trace("function2 called from function1");
      else
        trace("function2 not called from function1");
    } 
  }
}
// function2 not called from function1
// function2 not called from function1



Nested Functions

 
public function a (  ) {
  b(  );
  function b (  ) {
  }
}

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

    }
    public function getRandomPoint () {
      var randomX = getRandomInteger(5, 10);
      var randomY = getRandomInteger(10, 15);
    
      function getRandomInteger (min, max) {
        return min + Math.floor(Math.random(  )*(max+1 - min));
      }
    }
  }
}



Package-Level Functions

 
package packageName {
  public function identifier (  ) {
  }
}
To make the function accessible only in that package, we precede the definition with the access control modifier internal:
package packageName {
  internal function identifier (  ) {
  }
}
package utilities {
  import flash.system.*;
  public function isMac (  ) {
    return Capabilities.os == "MacOS";
  }
}



Passing Parameters to Your Function

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
      trace(getCircumference(10));
    } 
    function getCircumference(diameter:Number):Number {
      return Math.PI * diameter;
    }
  }
}



Source-File-Level Functions

 
package {
  class A {
    public function A (  ) {
    }
  }
}
function f (  ) {
}
Access-control modifiers cannot be included in source-file-level function definitions.



Writing Recursive Functions

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
         trace(factorial(3)); // Displays : 6
    }
     function factorial(i:uint):uint {
         if (i == 0) {
             return 1;
         } else {
             return (i * factorial(i-1));
         }
    }
  }
}