A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: So, if I want functions to be called in a very specific order...

  1. #1
    Member
    Join Date
    Apr 2006
    Posts
    37

    So, if I want functions to be called in a very specific order...

    Is there a way to do that without using a bunch of "if" or "switch" statements, like shoving them all in an array? I have a rough idea of how it would work, but I have no idea how it would look in code.

    And just to clarify, I mean an order than can be changed. Otherwise I wouldn't need to think about this at all.
    Last edited by Sub Tank; 03-04-2009 at 12:38 PM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes, you could shove a bunch of functions in an array or vector to be executed later. If they take arguments, you'll have to store the arguments as well (or turn it into a thunk with something like thunkify from my FunctionUtils package).

    What's the application?

  3. #3
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Would look something like this:

    PHP Code:
    var funcs:Array = [stopplaythunkify(trace"last")];
    funcs[2].call();  //  same as calling trace("last"); 
    (using Flax' thunkify util - it is sooo choice)

  4. #4
    Member
    Join Date
    Apr 2006
    Posts
    37
    Haha, I think you guys are going way over my head.

    What I'm really having trouble with is defining my functions so that I can stick them in an array, and it knows that they're functions. I don't think I have a chance at understanding how your super code works until I figure some of the basics out. My functions don't take arguments anyway.

    Quote Originally Posted by 5TonsOfFlax View Post
    What's the application?
    I'm just trying to come up with some sort of priority list that a computer player can use, that changes depending on the situation. So I guess it's a little AI test. Nothing big right now.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If your functions don't take arguments, then you don't need to worry about the "super code". Nez's example is pretty straightforward. In AS3 (and AS2 as well, actually) Functions are first class objects, which means that you can pass them around and do things with them just like any other piece of data. So your array could be build like this:
    Code:
    public function someFunction():void{
      trace('someFunction');
    }
    
    public function someOtherFunction():void{
      trace('someOtherFunction');
    }
    
    public var funcs:Array = [someFunction, someOtherFunction];
    //in flash10, use Vectors for typed arrays:
    //public var funcs:Vector.<Function> = new Vector.<Function>([someFunction, someOtherFunction]);
    
    for (var i:int = 0; i < funcs.length; i++){
      funcs[i].call(); //could also just do funcs[i]();
    }
    
    //or you can use the nifty forEach syntax
    /*
    funcs.forEach(function(f){f.call()});
    */

  6. #6
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    PHP Code:
    function funcA():void{
        
    trace('a');
    }

    function 
    funcB():void{
        
    trace('b');
    }

    var 
    funcs:Array = [funcAfuncB];
    funcs[0].call();    //  a
    funcs[1].call();    //  b 

  7. #7
    Member
    Join Date
    Apr 2006
    Posts
    37
    Ok, cool! Simple enough. I don't know why I was having so much trouble with that.

    Thanks guys.

    Looks like I can push functions in, and swap them around, so it should be perfect for what I'm trying to do.

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