[問題] 關於loadClip的問題
解決上次的xml讀取圖片的位置和網址之後,
swf已經可以正確地將圖片和網址自動更新,
但是這時候又遇到另一個棘手的問題。
原本是用loadMovie來做載入圖片的動作,
但是發現這樣沒辦法控制圖片的大小,
造成縮圖只顯示小小一塊,常常看不到照片的主角。
查了F1之後,決定改用loadClip來做。
跟據F1上面的解釋,onLoadComplete是在「圖片載入完成之後」才執行,
所以邏輯上來說,我只要在onLoadComplete裡面抓取圖片的寬高,
接著和圖片的容器大小作等比例縮放,應該就可以完成縮圖的動作,
但是實際在做的時候卻沒這麼順利。
首先是,我先試著trace載入的圖片的大小,
得出來的值卻不是圖片的大小,而是容器的大小,甚至是0。
再者,我就算寫了判斷式來進行縮圖的動作,也沒有進行縮圖的動作。
最後解決的方法是在onLoadComplete中,
加入onEnterFrame來不停抓取容器的大小是否改變,
改變之後再依照改變後的寬高來決定怎麼縮圖。
到這裡算是完全做完了。
但是我有很深的疑問,官方的說法是,
onLoadComplete裡的函式是在圖片載入完成之後才執行,
所以應該是普通時候並不會執行縮圖的函式,等到個別的圖片載入之後,
再個別做縮圖不是嗎?
如果要用到onEnterFrame來存取圖片的大小那根本沒必要用loadClip來做了,
我直接將判斷圖片大小和縮圖的函式寫在容器裡不就好了嗎?
這地方的邏輯我覺得很不通,不知道哪位高手可以幫忙解釋一下嗎?
謝謝。
順便附上後來所寫的程式:
expXML.onLoad = function (success) {
if (success){
// trace("EXP Loaded successfully");
// trace(expXML.length);
var exp : Array = expXML.firstChild.childNodes; //讀取列表內資料
trace(exp[0].firstChild.firstChild.nodeValue); //取出圖片路徑
trace(exp[0].firstChild.nextSibling.firstChild.nodeValue); //取出網址
for(var j = exp.length + 1; j <= 14; j++){ //隱藏沒有圖片載入的縮圖
tmp = _root["photo" + j + "_mc"];
tmp.enabled = false;
var mc_tween:Object = new Tween(tmp, "_alpha", Regular.easeInOut,
100, 0, 0.75, true);
};
for(var i = 0; i <= exp.length -1; i++){ //執行載入圖片
tmp = _root["photo" + (i + 1) + "_mc"];
tmp.URL = exp[i].firstChild.nextSibling.firstChild.nodeValue;
//載入網址
imgLoader.loadClip(exp[i].firstChild.firstChild.nodeValue,
tmp.photoLoader_mc.photo_mc); //載入圖片
tmp.onRelease = function (){ //縮圖被點擊時
getURL(this.URL);
};
};
} else {
loadingImages_mc.gotoAndStop("fail");
};
};
function resizeImg (){
if (this._width < this._parent._parent.photoMask_mc._width &&
this._height < this._parent._parent.photoMask_mc._height){
//當載入圖片寬高皆小於縮圖寬高時
this._x = (this._parent._parent.photoMask_mc._width - this._width)/2;
this._y = (this._parent._parent.photoMask_mc._height -
this._height)/2;
var mc_tween:Object = new Tween(this._parent, "_alpha",
Regular.easeInOut, 0, 100, 0.75, true);
delete this.onEnterFrame;
} else if (this._width < this._parent._parent.photoMask_mc._width &&
this._height > this._parent._parent.photoMask_mc._height){
//當載入圖片寬度小於縮圖寬度而高度大於縮圖高度時
this._x = (this._parent._parent.photoMask_mc._width - this._width)/2;
var mc_tween:Object = new Tween(this._parent, "_alpha",
Regular.easeInOut, 0, 100, 0.75, true);
delete this.onEnterFrame;
} else if (this._width > this._parent._parent.photoMask_mc._width &&
this._height < this._parent._parent.photoMask_mc._height){
//當載入圖片高度小於縮圖高度而寬度大於縮圖寬度時
this._y = (this._parent._parent.photoMask_mc._height -
this._height)/2;
var mc_tween:Object = new Tween(this._parent, "_alpha",
Regular.easeInOut, 0, 100, 0.75, true);
delete this.onEnterFrame;
} else if(this._width > this._height){ //當寬度大於高度時
this._width =
this._width*(this._parent._parent.photoMask_mc._height/this._height);
this._height = this._parent._parent.photoMask_mc._height;
var mc_tween:Object = new Tween(this._parent, "_alpha",
Regular.easeInOut, 0, 100, 0.75, true);
delete this.onEnterFrame;
} else if(this._height > this._width){ //當高度大於寬度時
this._height =
this._height*(this._parent._parent.photoMask_mc._width/this._width);
this._width = this._parent._parent.photoMask_mc._width;
var mc_tween:Object = new Tween(this._parent, "_alpha",
Regular.easeInOut, 0, 100, 0.75, true);
delete this.onEnterFrame;
} else if (this._width == this._height && (this._width >
this._parent._parent.photoMask_mc._width || this._height >
this._parent._parent.photoMask_mc._height)){ //當載入圖片寬度高度相同且大於縮圖寬高時
this._width = this._height =
this._parent._parent.photoMask_mc._width;
var mc_tween:Object = new Tween(this._parent, "_alpha",
Regular.easeInOut, 0, 100, 0.75, true);
delete this.onEnterFrame;
} else {
};
};
/*---------- imgLoader ----------*/
var imgLoader = new MovieClipLoader();
imgLoaderListener = new Object();
imgLoaderListener.onLoadStart = function (target_mc){
};
imgLoaderListener.onLoadProgress = function (target_mc){
};
imgLoaderListener.onLoadComplete = function (target_mc){
target_mc.onEnterFrame = resizeImg;
};
imgLoaderListener.onLoadInit = function (target_mc){
};
imgLoaderListener.onLoadError = function (target_mc){
};
imgLoader.addListener(imgLoaderListener);
...糟糕好像太亂了<囧|||
--
某T:我喜歡妳>/////<
:You gonna try harder...╮(╯_╰)╭
某T:啥?試著硬一點>///<(羞)
:是叫你再努力一點啦<(# ̄皿 ̄)╮☆(__ __||)
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 125.232.150.160
推
01/18 02:43, , 1F
01/18 02:43, 1F
→
01/18 09:52, , 2F
01/18 09:52, 2F
→
01/18 09:53, , 3F
01/18 09:53, 3F
Flash 近期熱門文章
PTT數位生活區 即時熱門文章