Actionscript 3 如何使用动作脚本为mxml中的位图图像设置源参数
我对actionscript非常陌生。我想访问mxml中已经存在的位图图像,以便更改其源位图图像已经存在于mxml中,并且具有其id。我不必动态创建它,只需设置其源。 我见过一些例子,但他们添加图像本身的动态我不希望这样。 将使用操作脚本3 代码如下: 请建议Actionscript 3 如何使用动作脚本为mxml中的位图图像设置源参数,actionscript-3,mxml,Actionscript 3,Mxml,我对actionscript非常陌生。我想访问mxml中已经存在的位图图像,以便更改其源位图图像已经存在于mxml中,并且具有其id。我不必动态创建它,只需设置其源。 我见过一些例子,但他们添加图像本身的动态我不希望这样。 将使用操作脚本3 代码如下: 请建议 <fx:Script> //code to set image source to url1(a string) </fx:Script> <s:Graphic> <s:Bitm
<fx:Script>
//code to set image source to url1(a string)
</fx:Script>
<s:Graphic>
<s:BitmapImage
id="ini_image"
source="@Embed('C:/horizontal_red.png')"
width="640"
height="480"
fillMode="scale"
includeIn="ready"
/>
</s:Graphic>
但它给出了名称空间中的错误,可能是因为ii正在使用相同的名称创建相同类型的新对象。这是不允许的。
如果我错了,请纠正我,并提出纠正建议 我假设应用程序中有多个状态,因此在设置BitmapImage源之前,必须注意就绪状态是否已初始化。希望下面的代码示例能够解决您的问题
<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
currentState="other">
<s:states>
<s:State name="ready"/>
<s:State name="other"/>
</s:states>
<fx:Script><![CDATA[
private function onButtonClick(event:MouseEvent):void {
currentState = 'ready';
ini_image.source = 'http://lorempixel.com/200/200';
}
]]></fx:Script>
<s:Graphic>
<s:BitmapImage
id="ini_image"
source="http://lorempixel.com/100/100"
width="640"
height="480"
fillMode="scale"
includeIn="ready"
/>
</s:Graphic>
<s:Button click="onButtonClick(event)"/>
</s:Application>
您还可以在这两种状态下包含图像,并使用不同的图像源
<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
currentState="other">
<s:states>
<s:State name="ready"/>
<s:State name="other"/>
</s:states>
<s:Graphic>
<s:BitmapImage
id="ini_image"
source.other="http://lorempixel.com/100/100"
source.ready="http://lorempixel.com/200/200"
width="640"
height="480"
fillMode="scale"/>
</s:Graphic>
<s:Button click="onToggleStates(event)"/>
<fx:Script><![CDATA[
private function onToggleStates(event:MouseEvent):void {
currentState = currentState == 'other' ? 'ready' : 'other';
}
]]></fx:Script>
</s:Application>
感谢您的回复。它在按钮点击时工作,我没有任何按钮在视图中,我必须在swf加载后立即显示该图像。为此,当我尝试访问与脚本中相同格式的ini_image.source时,它不起作用,因为出现错误“访问未定义的属性ini_image”。你能推荐一些方法让它工作吗?你不必为此指定任何“就绪”状态。只需设置图像源,它将在应用程序创建完成时显示。使用以下代码:是的,它可以工作,但我必须加载动态获取的url作为字符串。我使用了与ini_image.source=“url1”相同的语法,但它无法工作,因为我给出了系统上文件的文件地址。它只能通过mxml动态设置,因为它需要来自服务器的文件。一旦url被更改为服务器文件,它就工作了!!
<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
currentState="other">
<s:states>
<s:State name="ready"/>
<s:State name="other"/>
</s:states>
<s:Graphic>
<s:BitmapImage
id="ini_image"
source.other="http://lorempixel.com/100/100"
source.ready="http://lorempixel.com/200/200"
width="640"
height="480"
fillMode="scale"/>
</s:Graphic>
<s:Button click="onToggleStates(event)"/>
<fx:Script><![CDATA[
private function onToggleStates(event:MouseEvent):void {
currentState = currentState == 'other' ? 'ready' : 'other';
}
]]></fx:Script>
</s:Application>