Arguments in AS3
Is there arguments that work most of the time in AS3? I want a code setup that will works most of the time. Any suggestions?
Books break it down, but don't show how the programmers arrived at their conclusions. This could turn in to a discussion question, but if there's a secret I want to know.
WHAT I'M AFTER
- an argument structure - learn a process to perform function calls - expand variables beyond my "20 lines of code" - manage variables, events, and functions systematically2 examples that do the same thing, but are structured different "go figure"
//Example #1 "move the ball"
addEventListener(Event.ENTER_FRAME, onLoop, false, 0, true);
function onLoop(evt:Event):void{
ball1.x += 5;
}
//Example #1 "move the ball"
function moveBall(e:Event):void {
ball2.x += 5;
}
ball2.addEventListener(Event.ENTER_FRAME,move开发者_开发百科Ball);
The if...else argument "ball loop"
//growing collection of arguments
addEventListener(Event.ENTER_FRAME,myEnterFrame);
function myEnterFrame(event:Event) {
if (ball.x>800) {
ball.x=-160;
} else {
ball.x+=5;
}
}
DIFFERENT WAY OF DOING IT "from Adobe livedocs"
EQUIVILANT BOOLEANS
var flag:Boolean = true;
var flag:Boolean = new Boolean(true);
var flag:Boolean = Boolean(true);
EQUIVILANT STRINGS
var str:String = new String("foo");
var str:String = "foo";
var str:String = String("foo");
COMMENT
a functional style like lambda calculus would be a good example "more math less syntax and class structures"EVENT LISTENERS
http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.htmlI would suggest you read up on event handlers on Adobe's site. http://www.adobe.com/livedocs/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000139.html
From the site, here is the function definition of addEventListener:
function addEventListener(eventName:String,
listener:Object,
useCapture:Boolean=false,
priority:Integer=0,
useWeakReference:Boolean=false):Boolean;
The last three arguments have default values (false, 0, false) so those can be left out when you call the function. The first two arguments are always required.
I think that the rest of your question would be best answered by learning about object oriented programming in AS3. Here is one guide: http://www.adobe.com/devnet/actionscript/articles/oop_as3.html
Actually, many developers prefer to set useWeakReference to true, which requires that you plug in all five arguments into event listeners. The reason is that that way the listener doesn't hold a reference to the target that would prevent garbage collection - it's a memory management technique. Most really good AS3 devs will tell you to always use all five arguments, just to get to that point.
Also, generally speaking it's better to use a literal when you can. "text" instead of new String("text"), true instead of new Boolean(true). Complex reasons, but there's your short answer.
The answers to a lot of these questions can be found here, which is a document that attempts to standardize AS3 coding conventions. It's worth reading over!
精彩评论