AS3 concatenate rtmp connection string from xml between two functions

旧城冷巷雨未停 提交于 2019-12-24 18:23:44

问题


I keep getting a 'null' for the instance of the application under flash media server. I can't seem to pass the value from one function to another function

XML Sample

<bsettings>
<obj title="instance">19046</owner>
<obj title="id">uniqueid</owner>
<obj title="name">somename</owner>
<obj title="date">08/01/2012</owner>
<obj title="gender">female</owner>
</bsettings>

AS3 Code

<mx:Application 
    xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
    applicationComplete="initConnection(event)">

private var xinstance:String;
private var xmlstring:String = "http://www.blah.com/blahblah.xml";

protected function getXML():void{
XML.ignoreWhitespace = true;
var myXML:XML;
var myLoader:URLLoader=new URLLoader();
myLoader.load(new URLRequest(xmlstring));
myLoader.addEventListener(Event.COMPLETE, processXML);

function processXML(e:Event):void {
myXML = new XML(e.target.data);
for (var i:int = 0; i<myXML.*.length(); i++){
xinstance = myXML.obj[0];
xblah = myXML.obj[1];
xblah1 = myXML.obj[2];
xblah2 = myXML.obj[3];
xblah3 = myXML.obj[4];
}
}
}

private function initConnection(event:FlexEvent):void{
getXML();

//problem here, the xinstance isn't saved in the fmsstring
var fmsstring:String = "rtmp://blah.com/appname/" + xinstance;

nc = new NetConnection();
nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
nc.connect(fmsstring);
nc.client = this;
}

回答1:


Your problem is you're not waiting for the XML to load before using the xinstance string. On the initConnection function you call getXML() - which starts to load the xml, you need to wait until the xml is loaded before doing the rest of the code in the initConnection function. Here is a suggested change: (I added the go() function which is called AFTER the xml is processed)

private var xinstance:String;

protected function getXML():void {
    var xmlstring:String = "http://www.blah.com/blahblah.xml";
    XML.ignoreWhitespace = true;
    var myLoader:URLLoader=new URLLoader();
    myLoader.load(new URLRequest(xmlstring));
    myLoader.addEventListener(Event.COMPLETE, processXML);
}

protected function processXML(e:Event):void {
    var myXML:XML = XML(e.target.data)

    //THIS IS NOT A GOOD WAY TO DO THIS, use e4x
    //for (var i:int = 0; i<myXML.*.length(); i++){
    //xinstance = myXML.obj[0];
    //xblah = myXML.obj[1];
    //xblah1 = myXML.obj[2];
    //xblah2 = myXML.obj[3];
    //xblah3 = myXML.obj[4];
    //}

    //MUCH cleaner/easier/more efficient
    xinstance = myXML.bsettings.obj.(@title == "instance");

    go();
}

private function initConnection(event:FlexEvent):void{
    getXML();
}

private function go():void {
    var fmsstring:String = "rtmp://blah.com/appname/" + xinstance;

    nc = new NetConnection();
    nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
    nc.connect(fmsstring);
    nc.client = this;
}


来源:https://stackoverflow.com/questions/11855918/as3-concatenate-rtmp-connection-string-from-xml-between-two-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!