actionscript 3 - How do I use this URLRequest script?

▼魔方 西西 提交于 2019-12-25 02:07:27

问题


I started learning ActionScript 3 a week ago and have stumbled across a huge learning curve. I found this script on the internet:

var _loader:URLLoader;
var _request:URLRequest;

function loadData():void {
    _loader = new URLLoader();
    _request = new URLRequest("http://www.travoid.com/game/Purchase.php?gid=1");
    _request.method = URLRequestMethod.POST;
    _loader.addEventListener(Event.COMPLETE, onLoadData);
    _loader.addEventListener(IOErrorEvent.IO_ERROR, onDataFailedToLoad);
    _loader.addEventListener(IOErrorEvent.NETWORK_ERROR, onDataFailedToLoad);
    _loader.addEventListener(IOErrorEvent.VERIFY_ERROR, onDataFailedToLoad);
    _loader.addEventListener(IOErrorEvent.DISK_ERROR, onDataFailedToLoad);
    _loader.load(_request);
}
function onLoadData(e:Event):void {
    trace("onLoadData",e.target.data);
}
function onDataFailedToLoad(e:IOErrorEvent):void {
    trace("onDataFailedToLoad:",e.text);
}

This all seems to work and is generating no errors or output, however my issue comes about when I use this next part of code (which I made)

function vpBuy(e:MouseEvent):void{
    loadData();
    if (e.target.data == "false") {
        inf_a.visible = true;
        inf_b.visible = true;
        inf_c.visible = true;
        inf_d.visible = true;
        btn_ok.visible = true;
    }
}

I get this error:

ReferenceError: Error #1069: Property data not found on flash.display.SimpleButton and there is no default value. at travoid_fla::MainTimeline/vpBuy() onLoadData

The part that is probably throwing this is:

if (e.target.data == "false") {

I was hoping e.target.data was what stored the value on the web page (which displays as false) but apparently not. With the code I found on the internet, what stores the information on the web page?

Thanks, Ethan Webster.


回答1:


that's trying to check data before it's loaded. because data loading is a time based process, not instant. so, you've to make your data check condition wait for data really load

i think you should use a callback function for vpBuy from "onLoadData"

or you can try moving your data check conditional into onLoadData function like

function onLoadData(e:Event):void {
    trace("onLoadData",e.target.data);
    if (e.target.data == "false")
    {
            inf_a.visible = true;
            inf_b.visible = true;
            inf_c.visible = true;
            inf_d.visible = true;
            btn_ok.visible = true;
    }
}


来源:https://stackoverflow.com/questions/21661990/actionscript-3-how-do-i-use-this-urlrequest-script

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