我正在创建一个Flash影片,该影片会加载一些XML,其中包含某些文字和图片网址的详细信息,需要替换Flash影片中的现有MovieClip。
在Flash中是否有办法替换现有的MovieClip或更新它以使更新的/新的显示对象保持原始显示对象的原始外观,并且原始显示对象的任何补间仍然有效我们新的或更新的显示对象。
由于
斯蒂芬
更新:这是我用来加载它的代码:
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.display.Loader;
import flash.text.TextField;
import flash.events.ProgressEvent;
// get the number of children in display list
var count:uint = numChildren;
// get the url for the Ceres server
//var configUrl:String = loaderInfo.parameters.configUrl;
// load the config file from the server using the configUrl
var configUrl:String = "C:\development\projects\ReadXMLInFlash\example_xml_ceres_response.xml"; //loaderInfo.parameters.configFileUrl;
var defaultImgUrl:String = loaderInfo.parameters.defaultImgUrl;
var urlRequest:URLRequest = new URLRequest( configUrl );
// create the XML loader
var urlLoader:URLLoader = new URLLoader();
urlLoader.load( urlRequest );
urlLoader.addEventListener(Event.COMPLETE, onDataLoaded);
//Error handling ;
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
//Could be an error or just a message;
urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus);
function onDataLoaded(evt:Event):void
{
var myXML:XML = new XML(evt.target.data);
var node:XML;
// loop through the XML
for each (node in myXML.dynamicGroups.DynamicGroup.assets.Asset.elements.Element)
{
// check the assetValue of the XML, if contains http then a link use 'createNewImageLoader'
if(String(node.assetTagName) == 'Image' )
{
createNewImageLoader( node.assetValue, node.elementName );
}
else
{
// else it is text (e.g. a product descrition) use createNewTextLoader
createNewTextLoader( node.assetValue, node.elementName );
}
}
}
//error callbacks
function onIOError(evt:IOErrorEvent)
{
trace("IOError: "+evt.text);
// if an error load the default image
var defaultImgLoader:Loader = new Loader();
var defaultImgUrl:URLRequest = new URLRequest( defaultImgUrl );
defaultImgLoader.load( defaultImgUrl );
addChild( defaultImgLoader );
}
function onHTTPStatus(evt:HTTPStatusEvent)
{
//trace("HTTPStatus: "+evt.status);
}
function onSecurityError(evt:SecurityErrorEvent)
{
trace("SecurityError: "+evt.text);
}
// loads a image asset
function createNewImageLoader(loadURL:String, elementname:String):void
{
for(var i:uint=0;i<numChildren;i++)
{
var display:DisplayObject = getChildAt(i);
if (elementname == display.name)
{
var loader:Loader = new Loader();
var urlRequest:URLRequest = new URLRequest(loadURL);
loader.addEventListener(Event.COMPLETE, function(){
loader.x = display.x;
loader.y = display.y;
addChild( loader );
removeChild(display);
});
loader.load( urlRequest );
}
}
}
// adds new text to a display object
function createNewTextLoader( textToLoad:String, elementname:String):void
{
trace( "elementname:" + elementname );
for(var i:uint=0;i<count;i++)
{
var display:DisplayObject = getChildAt(i);
trace( "displayName:" + display.name );
if (elementname == display.name)
{
var textToDisplay:TextField = new TextField();
textToDisplay.text = textToLoad;
textToDisplay.x = display.x;
textToDisplay.y = display.y;
addChild(textToDisplay);
removeChild(display);
break;
}
}
}
答案 0 :(得分:0)
我不确定你的问题是什么,或者你想要达到的目标,但我会尽力回答你的问题。
根据我在评论和您的代码中所读到的内容,您要做的是更改movieclip中的文本和图像。要执行此操作,只需更改此代码:
function createNewTextLoader( textToLoad:String, elementname:String):void
{
trace( "elementname:" + elementname );
for(var i:uint=0;i<count;i++)
{
var display:DisplayObject = getChildAt(i);
trace( "displayName:" + display.name );
if (elementname == display.name)
{
var textToDisplay:TextField = new TextField();
textToDisplay.text = textToLoad;
textToDisplay.x = display.x;
textToDisplay.y = display.y;
addChild(textToDisplay);
removeChild(display);
break;
}
}
}
为:
function createNewTextLoader( textToLoad:String, elementname:String):void
{
// If the display object by the name of the value of "elementname" exists and it is a text field
if (getChildByName(elementname) != undefined &&
getChildByName(elementname) is TextField)
{
var textToDisplay:TextField = (TextField)getChildByName(elementname);// I dont know if you need a cast here but just in case
textToDisplay.text = textToLoad;// change the text in the text field
}
}
并替换它:
function createNewImageLoader(loadURL:String, elementname:String):void
{
for(var i:uint=0;i<numChildren;i++)
{
var display:DisplayObject = getChildAt(i);
if (elementname == display.name)
{
var loader:Loader = new Loader();
var urlRequest:URLRequest = new URLRequest(loadURL);
loader.addEventListener(Event.COMPLETE, function(){
loader.x = display.x;
loader.y = display.y;
addChild( loader );
removeChild(display);
});
loader.load( urlRequest );
}
}
}
用这个:
function createNewImageLoader(loadURL:String, elementname:String):void
{
if (getChildByName(elementname) != undefined &&
getChildByName(elementname) is Loader)
{
var loader:Loader = (Loader)(getChildByName(elementname);
var urlRequest:URLRequest = new URLRequest(loadURL);
loader.load( urlRequest );
}
}
我还没有真正测试过这些代码,但很难说我是否回答了正确的问题。
答案 1 :(得分:0)
我真的不明白为什么你已经在舞台上安装了MC并且在加载XML之后必须更新它们...你不能在加载XML之后创建它们吗?
在这两种情况下,最好创建两个自己的类(MovieClip的子类):TextLoader
和ImageLoader
以及更新方法。
package
{
import flash.display.MovieClip;
public class TextLoader extends MovieClip
{
private var _txt:TextField;
public function TextLoader()
{
_txt = new TextField();
// do TextFormat stuff, set size and position of _txt
addChild(_txt);
// you could also set some initial text like:
_txt.text = "loading ...";
// this will be overwritten later
}
public function update(label:String):void
{
_txt.text = label;
}
}
}
ImageLoader也会如下所示,而不是TextField获得了通过update(url:String)
方法更新的Loader。
在加载XML之前创建MC,如下所示:
var allTxts:Array = [];
for (var i:int=0;i<10;++i)
{
var txt:TextLoader = new TextLoader();
txt.x = 50;
txt.y = (i+1) * 50;
txt.name = "txt_" + i; // the names you got in your xml
addChild(txt);
allTxts.push(txt);
}
加载XML时,更新所有MC:
function getTxtWithName(name:String):TextLoader
{
for (var i:int=0;i<allTxts.length;++i)
{
var txt:TextLoader = allTxts[i] as TextLoader;
if (txt != null && txt.name == name)
{
return txt;
}
}
return null;
}
function onDataLoaded(evt:Event):void
{
var myXML:XML = new XML(evt.target.data);
var node:XML;
// loop through the XML
for each (node in myXML.dynamicGroups.DynamicGroup.assets.Asset.elements.Element)
{
// check the assetValue of the XML, if contains http then a link use 'createNewImageLoader'
if(String(node.assetTagName) == 'Image' )
{
var txt:TextLoader = getTxtWithName(node.elementName);
if (txt != null)
{
txt.update(node.assetValue);
}
}
else
{
// else it is text (e.g. a product descrition) use createNewTextLoader
var img:ImageLoader = getImageWithName(node.elementName);
if (img != null)
{
img.update(node.assetValue);
}
}
}
}