星期四, 9月 04, 2008

AS3:呼叫外部XML的連結(Using links from external xml file in AS3)

前陣子做了個Flash的Banner,這個連結之前是做在Flash裡面,但現在我想用連結外部XML檔案的方式來達到連結的效果。好處是之後若要改連結的話,只要改XML檔案的內容即可,無需再開啟Flash、輸出成SWF檔、再上傳。

XML程式碼如下:
<links>
<title link="http://www.homepage.com/promotion.htm">
The Promotion</title>

</links>
AS3的程式碼如下:
import flash.net.URLRequest;
import flash.events.MouseEvent;

var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("my-xml-file.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

var promotionLinkAttribute:XMLList;

function processXML(e:Event):void {
myXML = new XML(e.target.data);
promotionLinkAttribute = myXML.title[0].attribute("link");
}

senser.buttonMode=true;

senser.addEventListener(MouseEvent.MOUSE_OVER, hoverPromotionBanner);
senser.addEventListener(MouseEvent.MOUSE_OUT, visitedPromotionBanner);
senser.addEventListener(MouseEvent.CLICK, openPromotionPage);

function hoverPromotionBanner(e:MouseEvent):void{
MovieClip(getChildByName("senser")).gotoAndPlay("active");
this.gotoAndStop(currentFrame);
}
function visitedPromotionBanner(e:MouseEvent):void{
MovieClip(getChildByName("senser")).gotoAndPlay("visited");
this.play();
}
function openPromotionPage(e:MouseEvent):void {
var promotionLink:URLRequest = new
URLRequest(promotionLinkAttribute);
navigateToURL(promotionLink, "_self");
//trace(promotionLinkAttribute);
}

沒有留言: