0

嗨,我对 AS3 很陌生,我正在尝试将一些数据从 php 解析为 VO 文件,然后将数据字符串传输到另一个 .as 文件中,它将数据放入框中。我被困在如何将VO文件中的数据解析到另一个.as文件中(将数据从php提取到BookVO,然后将BookVO解析到VectorTest)。我尝试在 BookVO 中跟踪数据,它工作正常,但我无法将数据从 BookVO 获取到 VectorTest。

请帮忙,谢谢

BookVO.as

package  com.clark
{

    import flash.display.*;
    import flash.net.*;
    import flash.events.*;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLVariables;


    public class BookVO 
    {
        public var nobed1:String;
        public var LoZip1:String;
        public var rangelow1:String;
        public var rangehigh1:String;
        public var Bend:URLRequest;
        public var variabless:URLVariables;
        public var nLoader:URLLoader;


        public function BookVO() {


            Bend = new URLRequest("http://localhost/Autoresult.php");
            Bend.method = URLRequestMethod.POST;



            variabless = new URLVariables();
            Bend.data = variabless;


            nLoader = new URLLoader();
            nLoader.dataFormat = URLLoaderDataFormat.TEXT;
            nLoader.addEventListener(Event.COMPLETE,Jandler);
            nLoader.load(Bend);


            // handler for the PHP script completion and return of status
             function Jandler(event:Event) {
                 var responseVariables: URLVariables = new URLVariables(event.target.data); 
        this.nobed1 = responseVariables.nobed1 ;
        this.LoZip1 = responseVariables.LoZip1;
         this.rangelow1 = responseVariables.rangelow1;
        this.rangehigh1 = responseVariables.rangehigh1;
    }           
            }       
    }
}

VectorTest.as

package  com.clark
{
    import flash.display.MovieClip;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    import flash.display.Sprite;

    public class VectorTest extends MovieClip 
    {

        public function VectorTest() 
        {
            super();


            var books:Vector.<BookVO> = new Vector.<BookVO>();

            for (var i:int = 0; i < length; i++) 
            {

                var book:BookVO = new BookVO();
                book.nobed1 = "nobed1";
                book.LoZip1 ="LoZip1";
                book.rangelow1 = "rangelow1";
                book.rangehigh1 ="rangehigh1";
                books.push(book);

            }


            for (var j:int = 0; j < books.length; j++) 
            {
                trace("Test", j, "has a name of", books[j].nobed1);
                trace("Test", j, "Zip", books[j].LoZip1);
                trace("Test", j, "ranglow", books[j].rangelow1);
                trace("Test", j, "rangehigh", books[j].rangehigh1);
                books[j].nobed1;
                books[j].LoZip1;
                books[j].rangelow1;
                books[j].rangehigh1;
            }


            var currentY:int = 270;

            for (var k:int = 0; k < books.length; k++) 
            {
                var Bolder:Listing2 = new Listing2();
                Bolder.x=80;

                var tf:TextField = new TextField();
                var tf1:TextField = new TextField();
                tf1.width = 100;
                var tf2:TextField = new TextField();
                tf2.width = 100;
                tf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);

                tf.width = 100;
                tf.autoSize = TextFieldAutoSize.CENTER;
                tf1.width = 100;
                tf1.autoSize = TextFieldAutoSize.CENTER;
                tf2.autoSize = TextFieldAutoSize.CENTER;
                tf2.width = 100;
                tf1.y= tf.height+5;


                    // Pulling the textfields content out from the current bookVO
                tf2.text = books[k].nobed1;
                tf1.text = books[k].rangelow1;
                tf.text = books[k].rangehigh1;
                tf1.x = (Bolder.height-tf.height)*.5
                tf2.x = (Bolder.height-tf.height)*.5


                tf.x = (Bolder.height-tf.height)*.5
                tf.y = (Bolder.height-tf.height)*.15
                Bolder.addChild(tf);
                Bolder.addChild(tf1);
                Bolder.addChild(tf2);

                    // position the object based on the accumulating variable.
                Bolder.y = currentY;


                addChild(Bolder);
                currentY += Bolder.height + 35;
            }
        }

    }

}
4

1 回答 1

1

首先,你的课做得很好。比大多数自称是 AS3 新手的人要好得多。;)

那就是说...

  • VectorTest中,您正在调用super()MovieClip。你不需要这个。
  • 如果您不使用 MovieClip 的时间线(我建议您不要使用它),请Sprite改为扩展;它很轻,因为它不携带 MovieClip 的时间线包袱。
  • for loop在 VectorTest 构造函数中的第一个循环到length... 没有数组。
  • 您正在覆盖该循环中的属性,BookVO该循环应由您的URLLoader. 这是多余的。
  • 您的第二个循环引用 4 个属性BookVO,既不报告也不设置变量(即books[j].nobed1;)。可能想删除它。
  • BookVO中,您编写了一个嵌套函数。除非您要处理大量变量并且变量范围存在问题,否则不要这样做。就目前而言,唯一Jandler()正在访问的变量已经是类级别的全局变量。

加载数据是一个异步操作,这意味着数据不会立即填充(LoadRequest > Wait > ProgressEvent > Wait > LoadComplete)。因为您BookVO在同一个函数中实例化和读取属性,所以您将得到的只是空属性。与VectorTest构造函数不同,因为Jandler()Event.COMPLETE(异步)调用的,所以它可以访问您要查找的变量。


试试这个...

您仍然需要解决length要实例化的书籍数量,但是,我已经将您从构造函数中读取的属性分开,并添加了对加载完成时调用的方法的引用。

它会打印出所有变量,如果它是 Vector 中的最后一本书,它会调用finish()which... um... 完成您正在做的其他事情。:)

-干杯

BookVO 更新于 2013.11.07 @ 12:30 AM

package  com.clark{
    import flash.display.*;
    import flash.net.*;
    import flash.events.*;
    import flash.net.URLRequest;
    import flash.net.URLRequestMethod;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLVariables;

    public class BookVO {
        public var nobed1:String;
        public var LoZip1:String;
        public var rangelow1:String;
        public var rangehigh1:String;
        public var Bend:URLRequest;
        public var variabless:URLVariables;
        public var nLoader:URLLoader;
        private var callMethod:Function;
        public var data:Object;

        public function BookVO(listener:Function = null) {
            Bend = new URLRequest("http://localhost/Autoresult.php");
            Bend.method = URLRequestMethod.POST;

            variabless = new URLVariables();
            Bend.data = variabless;

            nLoader = new URLLoader();
            nLoader.dataFormat = URLLoaderDataFormat.TEXT;
            nLoader.addEventListener(Event.COMPLETE,Jandler);
            nLoader.load(Bend);

            if (listener != null) {
                callMethod = listener;
            }
        }

        public function Jandler(event:Event) {
            // handler for the PHP script completion and return of status
            var responseVariables:URLVariables = new URLVariables(event.target.data);
            data = event.target.data;
            report(data);

            if (callMethod != null) {
                callMethod(this);
            }
        }

        private function report(obj:*, prefix:String = ""):void {
            for (var k in obj) {
                var type:String = getType(obj[k]);
                if (type == "Array" || type == "Object" || type == "Vector") {
                    trace(prefix + k + ": (" + type + ") ¬")
                    report(obj[k], prefix + "  ")
                } else {
                    trace(prefix + k + ":" + obj[k] + " (" + type + ")")
                }
            }
        }

        private function getType(value:*):String {
            // Returns the class name of object passed to it.
            var msg:String = flash.utils.getQualifiedClassName(value);
            if (msg.lastIndexOf("::") != -1) {msg = msg.split("::")[1];}
            return msg;
        }
    }
}

向量测试

package  com.clark {
    import flash.display.MovieClip;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    import flash.text.TextFormatAlign;
    import flash.display.Sprite;

    public class VectorTest extends MovieClip {
        public var books:Vector.<BookVO>;
        public function VectorTest() {
            books = new Vector.<BookVO>();
            for (var i:int = 0; i < length; i++)  {
                var book:BookVO = new BookVO(response);
                books.push(book);
            }
        }

        private function response(book:BookVO):void {
            trace("Name: ", book.nobed1);
            trace("Zip: ", book.LoZip1);
            trace("ranglow: ", book.rangelow1);
            trace("rangehigh: ", book.rangehigh1);

            // call finish() if this is the last book.
            if (books.indexOf(book) == books.length - 1) {
                finish();
            }
        }

        private function finish():void {
            var currentY:int = 270;
            for (var k:int = 0; k < books.length; k++) {
                var Bolder:Listing2 = new Listing2();
                Bolder.x=80;

                var tf:TextField = new TextField();
                var tf1:TextField = new TextField();
                tf1.width = 100;
                var tf2:TextField = new TextField();
                tf2.width = 100;
                tf.defaultTextFormat = new TextFormat("Arial", 12, 0, null, null, null, null, null, TextFormatAlign.CENTER);

                tf.width = 100;
                tf.autoSize = TextFieldAutoSize.CENTER;
                tf1.width = 100;
                tf1.autoSize = TextFieldAutoSize.CENTER;
                tf2.autoSize = TextFieldAutoSize.CENTER;
                tf2.width = 100;
                tf1.y = tf.height+5;

                // Pulling the textfields content out from the current bookVO
                tf2.text = books[k].nobed1;
                tf1.text = books[k].rangelow1;
                tf.text = books[k].rangehigh1;
                tf1.x = (Bolder.height-tf.height)*.5
                tf2.x = (Bolder.height-tf.height)*.5


                tf.x = (Bolder.height-tf.height)*.5
                tf.y = (Bolder.height-tf.height)*.15
                Bolder.addChild(tf);
                Bolder.addChild(tf1);
                Bolder.addChild(tf2);

                // position the object based on the accumulating variable.
                Bolder.y = currentY;

                addChild(Bolder);
                currentY += Bolder.height + 35;
            }
        }
    }
}
于 2013-11-05T16:21:39.673 回答