0

我正在尝试在 Flash AS3 中使用 XML。

var record:XML = 
<person name="Peter(Grandfather)" age=54>
      <person name = "john(son1)" age = 28 >
            <person name = "bran(grandson1)" age = 2 />
      </person>
      <Person name = "Rob(son2)" age = 25 >
            <person name = "lancel(grandson2)" age = 3 />
      </person>

<person>

此 XML 列表将动态更新。并且对于每个动态添加的新人,将创建一个带有该人姓名的电影剪辑并将其添加到舞台上。单击电影剪辑时,我希望该人的年龄增加 1。

我想到的一种方法是将人名保存在影片剪辑中。喜欢

...
record.person.addChild(newperson);
var newclip:movieClip = new MovieClip;
newClip.name = newperson.@name
...
<newClip mouse click event>
record..*.(@name==newClip.name).@age += 1; 
....

我唯一不喜欢这种方法的是它需要 @name == newClip.name 比较;我猜这需要与 xml 记录中的每个节点进行名称匹配,这对于大型 XML 记录来说是一个非常苛刻的过程。我只需要一个好的方法来保存给定 xml 节点的路径,以便以后可以直接访问它。

像这样的东西:所以,

...
record.person.addChild(newperson);
var newclip:movieClip = new MovieClip;
newClip.xmlref = "<What to put here?>"
...
<newClip mouse click event>
record.[newClip.xmlref].@age += 1; //I want to be able to do something like this
....

谢谢你。

4

1 回答 1

0
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;

/**
 * ...
 * @author Xiler
 */
// CLASS TO TEST IT
public class XMLtest extends MovieClip {

    // YOUR XML TREE HERE
    private var record:XML = 
    <person name="Peter(Grandfather)" age="54">
          <person name = "john(son1)" age = "28" >
                <person name = "bran(grandson1)" age = "2" />
          </person>

          <person name = "Rob(son2)" age = "25" >
                <person name = "lancel(grandson2)" age = "3" />
          </person>
    </person>
    public function XMLtest() {
        // HERE, WE CREATE A NEW XML, ADD IT TO THE TREE, MAKE A MOVIECLIP  WITH A REFERENCE TO IT, AND ADD IT TO THE STAGE WITH A LISTENER
        var newPerson:XML = < person name = "Bob(son3)" age = "19" />;
        record.appendChild(newPerson);
        var test1:MovieClip = new MovieClip();
        addChild(test1);
        test1.xmlRef = newPerson;
        test1.graphics.beginFill(0x41ED07, 1);
        test1.graphics.drawRect(0, 0, 100, 100);
        test1.addEventListener(MouseEvent.CLICK, testListener);
    }
    private function testListener(event:MouseEvent = null):void {
        // HERE, IT IS CLICKED, AND THE record XML IS UPDATED AND TRACED, SO THAT YOU KNOW IT CHANGED IT.
        var itsXML:XML = event.currentTarget.xmlRef;
        var newAge:Number = Number(itsXML.@age) + 1;
        itsXML.@age = String(newAge);

        trace(itsXML.@age);
        trace(record)
    }
 }
}

This worked for me. Just copy and paste it in a a main .as file to a .fla file, and test and see it work. I hope this answers your question, please respond if you need further help.

于 2013-10-06T18:29:21.277 回答