-
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.
-
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?
-
Would look something like this:
PHP Code:
var funcs:Array = [stop, play, thunkify(trace, "last")];
funcs[2].call(); // same as calling trace("last");
(using Flax' thunkify util - it is sooo choice)
-
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.
Originally Posted by 5TonsOfFlax
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.
-
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()});
*/
-
PHP Code:
function funcA():void{
trace('a');
}
function funcB():void{
trace('b');
}
var funcs:Array = [funcA, funcB];
funcs[0].call(); // a
funcs[1].call(); // b
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|