作者cjcat2266 (CJ Cat)
看板Flash
標題Re: [問題] extends & super in書本上的例題
時間Mon Nov 19 13:43:28 2007
※ 引述《icedesign (不可或缺的咖啡因)》之銘言:
: function Bouncer(targetMC:MovieClip){
: super(targetMC,xVel,yVel)
: }
: 然後它又說不用寫也跑的動,但是我的不寫就是不動。
: 為什麼改了之後,ball_mc卻可以動呢?
沒錯,應該是書上寫錯了
因為少給xVel和yVel,super根本就不知道這兩個變數是從哪裡來的啊
或者可以利用 function接收變數數量沒有限制 這個特型
來做到一樣的效果
這樣的話Bouncer的constructor連一個參數都不需要定義
缺點是debug上面比較困難
public function Bouncer() {
if(arguments.length == 3) {
if((arguments[0] instanceof MovieClip)
&& (typeof arguments[1] == "number")
&& (typeof arguments[2] == "number")) {
super(arguments[0], arguments[1], arguments[2]);
} else {
throw Error("Wrong arguments passed");
}
} else {
throw Error("Wrong arguments passed");
}
}
--
CJ Cat = Croa'J Cat = Cockroach Cat = 西街凱特 = 蜚蠊貓 = 蟑螂貓
Gallery
http://cjcat2266.deviantart.com
ptt2 Board CJWorkshop - 阿多比閃光(Adobe Flash)研討區
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.4.234
推 icedesign:唉唷~限制這招讚喔!又多學到一招,感謝貓大~哈哈 11/19 14:40
※ 編輯: cjcat2266 來自: 140.112.4.234 (11/19 15:02)
推 cjcat2266:但是AS3就有限制arguments數量囉 11/19 15:39
推 cjcat2266:傳入的arguments數量必須比function definition的還多 11/19 15:42
推 aquarianboy:那就來個function overload囉,就沒這個限制了 :) 11/19 21:02
推 cjcat2266:我記得...Flash沒有function overloading吧? 11/20 00:01
→ cjcat2266:就只能靠arguments來決定 11/20 00:02
推 aquarianboy:啊,對喔,不支援,最近.NET寫昏頭了.. :) 11/20 01:04