我有这两个类:
每个都链接到库中的符号。 分别是dynamic_single_image和dynamic_image。
我将一个dynamic_image实例拖到我的时间轴上...并根据movieclip实例的名称...我有代码inloadMyImage来加载一个图像文件名及其相关的替代图像。
理想情况下,会调用DynamicImage的loadMyImage(加载alt图像)......它依次调用super.loadMyImage(加载英文图片)。应该加载两个图像文件。
但是,这对图像加载了两次......请参阅下面的跟踪声明。
我觉得即使我的movieclip实例是DynamicImage ......首先调用super的构造函数,然后调用DyanmicImage的构造函数!
public class DynamicSingleImage extends Sprite
{
var eLoader:Loader; //for english image
public function DynamicSingleImage()
{
trace("DynamicSingleImage...for: " + this.name);
loadMyImage();
}
public function loadMyImage () :void
{
...
trace( "always load ENGLISH file :" + engFilename );
...
}
}
public class DynamicImage extends DynamicSingleImage
{
var aLoader:Loader; //for alternate language image
public function DynamicImage()
{
trace("DynamicImage for: " + this.name);
this.loadMyImage();
}
override public function loadMyImage () :void
{
trace("============= START ========================");
trace("loading images for mc: " + this.name);
...
super.loadMyImage(); //first call the super to load the English version
trace( "load image for alternate language..." + altFilename
...
trace("============= END ========================");
}
这是跟踪输出:
DynamicSingleImage...for: eel_hotspot_content_mc
============= START ========================
loading images for mc: eel_hotspot_content_mc
always load ENGLISH file :eel_hotspot_content.jpg
load image for alternate language...eel_hotspot_content_alt.jpg
============= END ========================
DynamicImage for: eel_hotspot_content_mc
============= START ========================
loading images for mc: eel_hotspot_content_mc
always load ENGLISH file :eel_hotspot_content.jpg
load image for alternate language...eel_hotspot_content_alt.jpg
答案 0 :(得分:1)
当在扩展构造函数中不存在对它的特定调用时,确实首先调用超级构造函数。我认为最简单的解决方案是强制构造函数被调用为秒并且有一个标志,检查图像是否已经加载一次。见下文:
public class DynamicSingleImage extends Sprite
{
var eLoader:Loader; //for english image
var loaded:Boolean;
public function DynamicSingleImage()
{
trace("DynamicSingleImage...for: " + this.name);
if (!loaded)
loadMyImage();
}
public function loadMyImage () :void
{
...
trace( "always load ENGLISH file :" + engFilename );
loaded = true;
...
}
}
public class DynamicImage extends DynamicSingleImage
{
var aLoader:Loader; //for alternate language image
public function DynamicImage()
{
trace("DynamicImage for: " + this.name);
this.loadMyImage();
super();
}
override public function loadMyImage () :void
{
trace("============= START ========================");
trace("loading images for mc: " + this.name);
...
super.loadMyImage(); //first call the super to load the English version
trace( "load image for alternate language..." + altFilename
...
trace("============= END ========================");
}
答案 1 :(得分:0)
想出来。 由于超级构造函数是自动调用的,我从子类的构造函数中取出了loadMyImage。
就是这样。现在发生这种情况......
现在一切都很好。 'english'图像从super.loadMyImage加载......'alt'图像加载在子类的loadMyImage中。