简体   繁体   English

动态加载图像的AS3 Flex不允许图像的ID

[英]AS3 Flex dynamically loading images does not allow images' id

I need to load dynamically a few images (4-6) so that by clicking on particular image user would invoke particular action. 我需要动态加载一些图像(4-6),以便通过单击特定图像用户可以调用特定操作。 Embedding images solves the problem but at the expense of file size. 嵌入图像可以解决该问题,但要以文件大小为代价。 If I load them dynamically, they lose their ID. 如果我动态加载它们,它们将丢失其ID。

<comps:ExercisesScroller id="scroller" x="300" y="100"
        ex1="@Embed(source='assets/Exerc_1.png')" 
        ex2="@Embed(source='assets/Exerc_2.png')"/>

and so forth this works. 以此类推。 But instantiated in CDATA it does not work: 但是在CDATA中实例化它不起作用:

import components.ExercisesSCroller;
private var custScroller:ExercisesScroller;
private function init():void {
    custScroller = new ExercisesScroller();
    this.addElement(custScroller);
    custScroller.ex1 = "@Embed(source='assets/Exerc_1.png')";
}

I thought it should be quite a trivial task, but so far I can't solve it. 我认为这应该是一件微不足道的任务,但到目前为止我还无法解决。

You can't use @Embed that way. 您不能以这种方式使用@Embed。 The last line in your init() function should be something like this: init()函数的最后一行应该是这样的:

custScroller.ex1.source = "http://my.server.com/images/theRealImage.png";

This might need some adjustment, since I don't know what the innards of your ExercisesScroller component look like. 这可能需要一些调整,因为我不知道您的ExercisesScroller组件的内部结构是什么样。 You might, for instance, have to just take the URL as a string property, and have code inside the component to apply the change to the internal image. 例如,您可能只需要将URL作为字符串属性,并在组件内部添加代码以将更改应用于内部图像。

EDIT : By the way, I'm confused about why your code above isn't using a URL to reference the replacement image. 编辑 :顺便说一句,我很困惑为什么上面的代码没有使用URL来引用替换图像。 A relative path, like you show, assumes the files are local already, but you said you don't want to download them to the end user's machine until you're sure the user wants them. 就像您显示的那样,相对路径假定文件已经是本地文件,但是您说在确定用户需要它们之前,不希望将它们下载到最终用户的计算机上。 Giving a URL like this achieves the result you want: the image isn't downloaded until you change the image's source property. 提供这样的URL可达到所需的结果:只有在更改图像的source属性后,图像才会下载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM