作者terrylchen (某T)
看板Flash
标题[问题] 关於loadClip的问题
时间Fri Jan 18 00:44:21 2008
解决上次的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
1F:推 aquarianboy:直接附个fla会不会简单些? :) 01/18 02:43
2F:→ terrylchen:其实贴的那些程式码只是参考而已:),我的问题是关於 01/18 09:52
3F:→ terrylchen:onLoadComplete的观念而已 01/18 09:53