动作脚本3.0向量在函数退出后不存储变量

时间:2012-11-28 21:33:47

标签: flash actionscript vector scope

我是动作脚本的新手,我之前曾使用过OOP语言,但我遇到了一个问题,我必须为图书馆制作一本小册子,他们以XML格式递交了他们的书籍数据。我建了两个班:书和图书馆。

Book.as:

package
{
    import flash.display.Sprite;
    import flash.net.URLLoader;
    import flash.net.URLRequest;    
    import flash.display.Loader;
    import flash.events.Event;
    import flash.display.Sprite
    public class Book extends Sprite
    {
        public var _author_name:String;
        public var _book_title:String;
        public var _book_subject:String;
        public var _book_edition:String;
        public var _ISBN: String;
        public var _publication_year:String;
        public var _price:String;
        public var _in_stock:String;
        public var _book_cover:String;
        public function Book()
        {
        }
        public function  getAuthorName():String
        {
            return _author_name;
        }
        public function  getBookTitle():String
        {
            return _book_title;
        }
        public function  getBookSubject():String
        {
            return _book_subject;
        }
        public function  getBookEdition():String
        {
            return _book_edition;
        }
        public function  getISBN():String 
        {
            return _ISBN;
        }
        public function  getPublicationYear():String
        {
            return _publication_year;
        }
        public function  getPrice():String
        {
            return _price;
        }
        public function  getAvailability():String
        {
            return _in_stock;
        }
        public function getBookCover():String
        {
            return _book_cover;
        }
        public function setBookCover(temp:String):void
        {
            _book_cover = temp;
        }
        public function  setAvailability(temp:String):void
        {
            _in_stock = temp;
        }
        public function  setPrice(temp:String):void
        {
            _price = temp;
        }
        public function  setPublicationYear(temp:String):void
        {
            _publication_year = temp;
        }
        public function setISBN(temp:String):void
        {
            _ISBN = temp;
        }
        public function setBookEdition(temp:String):void
        {
            _book_edition = temp;
        }
        public function setBookSubject(temp:String):void
        {
            _book_subject = temp;
        }
        public function setBookTitle(temp:String):void
        {
            _book_title = temp;
        }
        public function setAuthorName(temp:String):void
        {
            _author_name = temp;
        }
        public override function toString():String
        {
            var book_desc:String;
            book_desc = "Author Name: " + _author_name + "\n";
            book_desc = book_desc + "Title: " + _book_title + "\n";
            book_desc = book_desc + "Subject: " +_book_subject + "\n";
            book_desc = book_desc + "Edition : " +_book_edition + "\n";
            book_desc = book_desc + "ISBN: " +_ISBN + "\n";
            book_desc = book_desc + "Publication Year: " + _publication_year + "\n";
            book_desc = book_desc + "Price: " + _price + "\n";
            book_desc = book_desc + "Available: " +_in_stock + "\n";
            book_desc = book_desc + "Book Cover URL : " +_book_cover + "\n";  
            return book_desc;
        }
        public function equals(temp:Book):Boolean
        {
            if(_ISBN == temp.getISBN())
                return true;
            else 
                return false;
        }

    }
}

Library.as

package
{
    import flash.net.URLLoader;
    import flash.net.URLRequest;    
    import flash.display.Loader;
    import flash.events.Event;
    import flash.display.Sprite;
    import flash.display.Sprite

    public class Library extends Sprite
    {
        public var xml:XML;
        public var url_loader:URLLoader;
        public var _book_vector:Vector.<Book>
        public var _xml_path:String;
        public function Library(xml_path:String)
        {
            _xml_path = xml_path;
            _book_vector = new Vector.<Book>(20);
        }
        public function getXMLPath():String
        {
            return _xml_path;
        }
        public function setXMLPath(temp:String):void
        {
            _xml_path = temp;
        }
        public function LoadXML():void
        {
            url_loader = new URLLoader(new URLRequest(_xml_path));
            url_loader.addEventListener(Event.COMPLETE,ParseXML);
        }
        public function ParseXML(temp:Event):void
        {
            xml = new XML(temp.target.data);
            LoadBooks();
        }
        public function LoadBooks():void
        {
            for(var i :int=0;i<xml.children().length();i++ )
            {
                _book_vector[i] = new Book();
                _book_vector[i]._author_name = xml.children()[i].AuthorName;
                _book_vector[i]._book_title =  xml.children()[i].BookTitle;
                _book_vector[i]._book_subject = xml.children()[i].Subject;
                _book_vector[i]._book_edition = xml.children()[i].Edition;
                _book_vector[i]._price = xml.children()[i].Price;
                _book_vector[i]._publication_year = xml.children()[i].Year;
                _book_vector[i]._in_stock = xml.children()[i].Availabilty;
                _book_vector[i]._book_cover = xml.children()[i].ImagePath;
                _book_vector[i]._ISBN = xml.children()[i].@ISBN;
            }
        }
        public function printLibrary():void
        {
            for(var i:int=0;i<this._book_vector.length;i++)
            {
                trace(_book_vector[i].toString());
            }
        }
    }
}

XML加载很好,但是当我尝试调用printLibrary()时,它会出现问题:错误#1009:无法访问空对象引用的属性或方法。

当我使用调试器时,我可以看到_book_vector正在加载“Books”并以XML读取数据形式存档,但是当LoadBooks退出时,矢量不再保留它们,为什么? _book_vector不是函数的本地!我做错了什么?有什么我没有得到正确的?

1 个答案:

答案 0 :(得分:0)

new Vector.<Book>(20);

而不是

new Vector().<Book>(20);

使用new Vector()...给出了这个错误:

[Fault] exception, information=TypeError: Error #1007: Instantiation attempted on a non-constructor.

但这可能是因为我的错误处理程序更加严格。