从xml导入到fla错误

时间:2016-02-25 19:30:33

标签: xml actionscript-3

我有这个xml树

<?xml version="1.0" encoding="UTF-8"?>
<letters>
 <all>
      <explain>
          <word>slope</word>
      </explain>
      <description>
          <definition>a</definition>
      </description>
  </all>

   <all>
      <explain>
          <word>slope2</word>
      </explain>
      <description>
          <definition>b</definition>
      </description>
  </all>
</letters>

这是舞台上的新代码我有3个动态文本(mydata和titleofmydata和activeletter)并且有2个按钮(a,b)但是当我第一次点击按钮数据显示正常但第二次或第二次按钮fla展示

  

TypeError:错误#2007:参数文本必须为非null。在   flash.text :: TextField / set text()at   glossary_fla :: MainTimeline / createbtn()at   glossary_fla :: MainTimeline / letterclick()

import flash.events.MouseEvent;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.sampler.Sample;
stop();
var n:int = 0;
var i:int = 0;
var myxml:XML;
var myxmllist:XMLList;
activeletter.alpha = .5;
var currentword:String;
var currentletter:String;
var myLoader:URLLoader= new URLLoader();
myLoader.load(new URLRequest("eL366.xml"));
myLoader.addEventListener(Event.COMPLETE,loadXML);
function loadXML(e:Event)
{
    myxml = new XML(e.target.data);
    a.addEventListener(MouseEvent.CLICK,letterclick);
    b.addEventListener(MouseEvent.CLICK,letterclick);
function letterclick(e:MouseEvent)
{
        myxmllist = new XMLList(myxml.word.(@clas==e.target.name).@wordname);
        for each (var word:XML in myxmllist)
        {
            var btn:MovieClip = createbtn(myxmllist[i]);
            btn.x = 100;
            btn.y = 130 + n;
            addChild(btn);
            n +=  50;
            btn.addEventListener(MouseEvent.CLICK,wordclick);
            i++;
        }
        activeletter.text = a.toUpperCase()
}
function wordclick(e:MouseEvent)
{
    var word:MovieClip = MovieClip(e.currentTarget);
    currentword = word.label;
    titleofmydata.text = String(currentword + " is :");
    mydata.text = String(myxml.word.(@wordname==currentword).@definition);
}
function createbtn(label:String):MovieClip
{
    var txt:TextField = new TextField();
    txt.defaultTextFormat = new TextFormat('Cooper Black',25,0xFFFFFF);
    txt.text = label;
    txt.autoSize = TextFieldAutoSize.LEFT;
    txt.background = txt.border = false;
    txt.selectable = false;
    var btn:MovieClip = new MovieClip();
    btn.mouseChildren = false;
    btn.buttonMode = true;
    btn.addChild(txt);
    btn.label = label;
    return btn;
}

1 个答案:

答案 0 :(得分:1)

首先ataShow函数必须是ataShow(e:MouseEvent),因为当点击按钮时,您将按钮数据发送到ataShow函数,

第二:使用description[i]代替description[i-1]

ArrayaTa

使用btn

并尝试以下代码

import flash.events.MouseEvent;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Sprite;
import flash.display.MovieClip;
import fl.controls.TextArea;

stop();

var btnArray:Array=new Array;
var txtArray:Array=new Array;

var ChangebtnPosition:int = 50;
var definition:Array = new Array  ;
var MydataAreaPosition:int = 580;

var myLoader:URLLoader= new URLLoader();
myLoader.load(new URLRequest("eL366.xml"));
myLoader.addEventListener(Event.COMPLETE,loadXML);
function loadXML(e:Event)
{
    var myxml:XML = new XML(e.target.data);
    var lenngth:int = myxml.children().length();
    for (var i:int = 0; i < lenngth; i++)
    {
        var btn:MovieClip = ButtonForWord(String(myxml.all.explain[i].word));
        var aTa:TextField = new TextField;
        aTa.x=100;
        aTa.y=200;
        aTa.width=200;
        aTa.height=200;
        aTa.condenseWhite = true;
        aTa.htmlText = String(myxml.all.description[i].definition);
        addChild(aTa);
        aTa.visible = false;
        MydataAreaPosition +=  40;
        btn.x = 165;
        btn.y = 165 + n;
        addChild(btn);
        txtArray.push(aTa);
        btnArray.push(btn);
        ChangebtnPosition +=  50;
        btn.addEventListener(MouseEvent.CLICK,aTaShow);
    }
}
function ButtonForWord(label:String):MovieClip
{
    var txt:TextField = new TextField();
    txt.defaultTextFormat = new TextFormat('Cooper Black',20,0x8b3a3a);
    txt.text = label;
    txt.autoSize = TextFieldAutoSize.LEFT;
    txt.background = txt.border = false;
    txt.selectable = false;
    var btn:MovieClip = new MovieClip();
    btn.mouseChildren = false;
    btn.buttonMode = true;
    btn.addChild(txt);
    btn.label = label;
    return btn;
}
function aTaShow(e:MouseEvent)
{
    txtArray[btnArray.indexOf(e.currentTarget)].visible=true;
}