发新话题
打印

『请教』概念性问题

funcName(param1, param2,...paramn) (You can almost detect the faint odor of smoldering mandrake root in the air.) The parentheses contain the values for any parameters required by the function. If the function requires no parameters (as determined by the function declaration), the parentheses are left empty. Lets try invoking our sayHi( ) function, which has no parameters, to get the hang of basic function invocation. Here, again, is our sayHi( ) function declaration: function sayHi () { trace("Hi there!"); } And heres our sayHi( ) function invocation: sayHi(); In this simple demonstration, we add the function invocation to the same script containing the function declaration, but well see later that this isnt always the case. When the sayHi( ) function call is executed, the sayHi function is invoked, so its function body runs, causing "Hi there!" to appear in the Output window. You can see that typing sayHi( ); is more convenient than typing the whole trace( ) statement, and the function name, sayHi, is a more meaningful description of what our code does. Using thoughtful function names makes our code more readable, almost like human sentences. In this book and in most programming documentation, function names are conventionally listed with the invocation parentheses, as in "the sayHi( ) function" versus "the sayHi function." This convention makes function names easy to spot in a sentence. Before we continue, notice the semicolon at the end of the function call: sayHi(); We add the semicolon because a function call is a complete statement, and good form dictates that all statements should end in a semicolon. Believe it or not, youve just learned the basics of creating and invoking functions梟ot too shabby. Functions keep our code centralized and easier to maintain, especially when we need to perform the same operation repeatedly throughout our program. Functions become even more powerful when used with parameters, which well consider next. 一般我用function(){} 从来部注意function后面()里面要如何写,写什么 现在在看ActionScript for Flash MX: The Definitive Guide 这个部分还是不太清楚。。。

TOP

括号里面为函数所引用的参数,这些参数只在函数内有效smile.gif 定义函数:
CODE
function say (content) { trace(content) }
使用:
CODE
say ("Hello , Colorbird !")

TOP

使用 say ("Hello , Colorbird !") say() 在这里的功能等于trace() 是这意思? 就是说。。 整个fucntion 就是定义say的 后面直接用say就可以了。。。 就象一个CSS 在头上定义了。下面直接调用就OK

TOP

可以再深一层理解
CODE
function resizeMC(mc,width,height) { mc._width=width mc._height=height }
CODE
function sum(a,b) { return a+b }

TOP

CODE
function resizeMC(mc,width,height) { mc._width=width mc._height=height }
这个感觉有点象在setProperty();了 如果这样setProperty();根本就可以被取代

TOP

你理解偏了,function就是function,为实现某一个功能而写。smile_sleepy.gif 我刚才只是举一个例子,说明function是可以如何运用参数 这是我正在做的一个项目中的一个函数,你是否觉得很“”一个函数? smile_wink.gif
CODE
function restrict(item, x, y) { var collision = false; var depth; var width = item.width; var width2; var lx, rx, tlx, trx; var centerx = x; leftItem=[], rightItem=[]; for (var i = 1; i<=itemNum; i++) { var temp = this["item_"+i]; if (temp == item) { continue; } if (temp._x<centerx) { leftItem.push({id:i, x:temp._x, mc:temp}); } else { rightItem.push({id:i, x:temp._x, mc:temp}); } if (Math.abs(centerx-temp._x)<(item.width+temp.width)/2) { collision = true; } } if (collision || centerx<0 || centerx>600) { leftItem.sortOn("x", 2 | 16); rightItem.sortOn("x", 16); lx = leftItem[0].x == undefined ? 0 : leftItem[0].x+leftItem[0].mc.width/2; rx = rightItem[0].x == undefined ? 600 : rightItem[0].x-rightItem[0].mc.width/2; if (Math.abs(rx-lx)<width) { if (Math.abs(centerx-lx)<Math.abs(rx-centerx)) { do { if (leftItem[0].x != undefined) { rightItem.unshift(leftItem.shift()); tlx = leftItem[0].x == undefined ? 0 : leftItem[0].x+leftItem[0].mc.width/2; trx = rightItem[0].x == undefined ? 600 : rightItem[0].x-rightItem[0].mc.width/2; } else { trace("Its FULL !"); item.removeMovieClip(); return; } } while (Math.abs(trx-tlx)<width); } else { do { if (rightItem[0].x != undefined) { leftItem.unshift(rightItem.shift()); tlx = leftItem[0].x == undefined ? 0 : leftItem[0].x+leftItem[0].mc.width/2; trx = rightItem[0].x == undefined ? 600 : rightItem[0].x-rightItem[0].mc.width/2; } else { trace("Its FULL !"); item.removeMovieClip(); return; } } while (Math.abs(trx-tlx)<width); } lx = tlx; rx = trx; } if (rx<oldlx) { if (Math.abs(oldlx-centerx)<Math.abs(centerx-rx)) { lx = oldlx; rx = oldrx; } } else if (lx>oldrx) { if (Math.abs(centerx-oldrx)<Math.abs(lx-centerx)) { lx = oldlx; rx = oldrx; } } leftline._x = lx; rightline._x = rx; leftEdge = lx; rightEdge = rx; oldlx = lx; oldrx = rx; if (collision) { if (Math.abs(centerx-leftEdge)<Math.abs(centerx-rightEdge)) { item._x = leftEdge+width/2; } else { item._x = rightEdge-width/2; } } } else { centerx = centerx<width/2 ? width/2 : centerx; centerx = centerx>(600-width/2) ? (600-width/2) : centerx; item._x = centerx; } item._y = bottomline; depth = Math.floor(item._x*2000)+100000; depth = checkDepth(depth); item.depth = depth; item.swapDepths(depth); }

TOP

smile_shock.gif 这么长的运算匣子!!!! 看死人啊! 眼睛痛!smile_disapprove.gif

TOP

CODE
function restrict(item, x, y)
一个function 写这么长,。。。我的天。。。 也许这就是junior 与 senior的区别把smile_disapprove.gif 谢了。。清楚一些了。。smile.gif

TOP

嘿嘿,要理解function是为了功能而写 功能简单,function就简单,功能复杂,function就复杂 smile.gif

TOP

发新话题