好的,我正在尝试访问由章节组成的本地 XML 文档,每个章节都有几页文本。我有一个条件,可以监听按钮点击并“翻页”。“如果”工作正常:它循环浏览页面直到它到达末尾。但是“其他”条件不起作用。它甚至不会追踪任何东西。我知道这是因为这个错误:
TypeError:错误 #2007:参数文本必须为非空。在 flash.text::TextField/set text() at thisArray/textLoader()
...但不知道如何解决它。我是否需要完全放弃循环阅读文本的方法,或者我只是错过了一些小事?抱歉,如果其中一些代码草率...我已经对其进行了相当多的调整以使其正常工作。谢谢您的帮助。
这是相关的代码:
package {
import as3101.ui.formatA;
import flash.display.*;
import flash.text.*;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.*;
import flash.utils.Dictionary;
import flash.events.*;
import flash.display.Loader;
import flash.net.URLLoader;
import flash.net.URLRequest;
public class thisArray extends MovieClip {
//XML Vars
private var xml:XML;
private var storyKeys:Dictionary = new Dictionary();
private var xmlLoader:URLLoader = new URLLoader();
private var url:URLRequest = new URLRequest("storyXML.xml");
//Text and Button Vars
private var formatting:formatA;
private var txta:TextField = new TextField();
private var storyBox:Sprite = new Sprite();
private var count:uint = 0;
private var storyText:XMLList;
private var nxtBtn:Sprite = new Sprite();
public function thisArray() {
addChild(storyBox);
storyBox.addChild(txta);
storyBox.x = 0;
storyBox.y = 0;
txta.x = 0;
txta.y = 0;
storyBox.width = 500
storyBox.height = 400;
storyBox.buttonMode = true;
storyBox.mouseChildren = false;
xmlLoader.load(url);
xmlLoader.addEventListener(Event.COMPLETE, onXmlLoad);
//external class formatA sets my text for size, font, etc...
formatting = new formatA();
formatting.defField = txta;
txta.defaultTextFormat = formatting.myFormat;
txta.wordWrap = true;
trace ("class loaded: " + this);
}
private function onXmlLoad(e:Event):void {
xml = new XML(xmlLoader.data);
trace("XML Loaded");
storyText = xml.chapters.chapter.page;
txta.text = storyText[count];
buttonMaker();
}
public function textLoader(e:MouseEvent):void {
count++;
if(count < txta.length)
{
trace("hi");
txta.text = storyText[count];
} else if (count == storyText.length) {
trace("bye");
removeChild(nxtBtn);
}
}
public function buttonMaker() {
nxtBtn.graphics.lineStyle(3,333333);
nxtBtn.graphics.beginFill(0x333333);
nxtBtn.graphics.drawRect(0,0,50,30);
nxtBtn.graphics.endFill();
nxtBtn.x = 480;
nxtBtn.y = 220;
addChild(nxtBtn);
nxtBtn.buttonMode = true;
nxtBtn.name = ("nextButton");
nxtBtn.addEventListener(MouseEvent.CLICK, textLoader);
}
这是我的 XML 文档:
< ?xml 版本="1.0" 编码="utf-8"? >
<故事文本>
<章节>
< 章节 id="0" 名称="一个" >
< page num="0" > Text Block A < /page >
< page num="1" > Text Block B < /page >
< page num="2" > Text Block C < /page >
< page num="3" > Text Block D < /page >
</章节>
<章节id="1" name="两个" > </chapter >
<章节id="2" name="三" > </chapter >
</storyAnswers>
< /章节 >
</故事文本>