我正在尝试在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
....
谢谢。
答案 0 :(得分: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)
}
}
}
这对我有用。只需将其复制并粘贴到主.as文件中的.fla文件中,然后测试并查看它是否有效。我希望这能回答您的问题,如果您需要进一步的帮助,请回复。