如何获取使用 (MovieClip) 或 (Sprite) 创建的按钮的值 class
How to get values for buttons created with (MovieClip) or (Sprite) class
在最后一行,我希望 XML 函数读取单击的按钮的名称并显示 XML 文件中的相应标签(XML 中的标签文件具有相同的按钮名称),否则我将不得不使函数或 if 语句等于我拥有的按钮数量!这对我来说没有任何意义!
stop();
import flash.events.MouseEvent;
import flash.events.Event;
import flash.net.URLLoader;
import flash.display.MovieClip;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
var myVar:String = new String();
//var myFrame:Array=new Array(26);
var words:Array =['Amon','Ahmos','Ramsis'];//the words
for (var j:int=0; j<words.length; j++)
{
var word:MovieClip = CreatTextButton(words[j]);//loop in myDictionary and name each button with a different name
addChild(word);
word.x = 130;
word.y = 16 + (j * 30);// to change the position of each word
word.addEventListener(MouseEvent.CLICK,onWordClick);
function CreatTextButton(label:String):MovieClip //creat buttons
{
var txt:TextField = new TextField();
txt.defaultTextFormat = new TextFormat('Calibri',20,0x000000);
txt.text = label;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.background = txt.border = true;
txt.selectable = false;
var btn:MovieClip = new MovieClip();
btn.label=label;
btn.mouseChildren = false;
btn.addChild(txt);
btn.buttonMode = true;
myVar=label.valueOf();
trace(myVar);
return btn;
}
function onWordClick(event:MouseEvent)
{
var myLoader:URLLoader= new URLLoader();
myLoader.load(new URLRequest("1.xml"));
myLoader.addEventListener(Event.COMPLETE,loadXML);
var label:String = event.currentTarget.label;
trace(label);
}
function loadXML(e:Event)
{
var myxml:XML = new XML(e.target.data);
mydata.text += String(myxml.label)+"\n";
}
}
我的XML是
<Pharaohs>
<Ahmos id='_1'>
reigned c. 1539–14 bce and founder of the 18th dynasty
</Ahmos>
<Amon id='_2'>
The chief deity of the Egyptian Empire
</Amon>
<Ramsis id='_3'>
The third Pharaoh of the 19th dynasty
</Ramsis>
</Pharaohs>
抱歉这么慢和愚蠢:D
您可以使用 event.currentTarget
来确定单击了哪个按钮。由于您使用动态 MovieClip
作为按钮,因此您可以在创建按钮时直接将标签指定为 属性:btn.label = label
。然后从您的按钮单击处理程序中,您可以使用 event.currentTarget.label
.
确定单击按钮的按钮文本
您可以在 E4X 过滤器表达式中使用此标签字符串值来提取 XML 中具有匹配 name()
值的节点。例如,如果您的 XML 看起来像这样:
<Pharoahs>
<Amon>Some text about Amon</Amon>
<Ahmos>All about Ahmos</Ahmos>
<Ramsis>This is Ramsis</Ramsis>
</Pharoahs>
您可以像这样提取名称与按钮标签匹配的 XML 节点:
var label:String = event.currentTarget.label;
var node:XML = xml.children().(name() == label)[0];
请注意,将 XML 节点命名为与您的文字相同可能不是一个好主意,因为对于如何命名不适用于字符串的 XML 节点有很多限制一般来说。例如,空格会成为问题,某些字符是不允许的,区分大小写会带来挑战,等等。相反,您可以使用属性(ex <pharoah name="Amon">
或子节点(ex <pharoah><name>Amon</name></pharoah>
)作为每个 XML 节点的键。这也使 XML 查找更容易:分别为 xml.pharoah.(@name == label)
或 xml.pharoah.(name == label)
。
另请注意,您无需在每次单击按钮时都重新加载 XML。预加载 XML、 然后 显示按钮可能更有意义,每次点击只需要在 XML 对象中查找数据。
将它们放在一起并进行一些小的代码改进:
- 重命名各种事物更有意义。
- 将所有函数移出 for 循环和其他函数。
- 首先加载 XML 并且只加载一次,然后渲染列表,生成的点击处理程序要简单得多。
结果:
const listOrigin:Point = new Point(130, 16);
const listItemSpacing:Number = 30;
const xmlPath:String = "1.xml";
var pharoahs:Array = ['Amon', 'Ahmos', 'Ramsis'];
var pharoahsXML:XML;
// First load your XML
loadXML();
function loadXML():void {
var urlLoader:URLLoader = new URLLoader();
urlLoader.load(new URLRequest(xmlPath));
urlLoader.addEventListener(Event.COMPLETE, loadXMLComplete);
}
function loadXMLComplete(e:Event) {
pharoahsXML = new XML(e.target.data);
// Render your buttons after the XML is loaded
renderPharoahsButtons();
}
function renderPharoahsButtons():void {
for (var i:int = 0; i < pharoahs.length; i++) {
var btn:MovieClip = createTextButton(pharoahs[i]);
btn.x = listOrigin.x;
btn.y = listOrigin.y + (i * listItemSpacing);
addChild(btn);
btn.addEventListener(MouseEvent.CLICK, pharoahButtonClick);
}
}
function createTextButton(label:String):MovieClip {
var txt:TextField = new TextField();
txt.defaultTextFormat = new TextFormat('Calibri', 20, 0x000000);
txt.text = label;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.background = txt.border = true;
txt.selectable = false;
var btn:MovieClip = new MovieClip();
btn.mouseChildren = false;
btn.buttonMode = true;
btn.addChild(txt);
btn.label = label; // Store the button label for reference
return btn;
}
function pharoahButtonClick(e:MouseEvent):void {
// When a button is clicked is the label to extract the XML node
var label:String = e.currentTarget.label;
var pharoah:XML = pharoahsXML.children().(name() == currentPharoah)[0];
pharoahText.text += pharoah + "\n";
}
在最后一行,我希望 XML 函数读取单击的按钮的名称并显示 XML 文件中的相应标签(XML 中的标签文件具有相同的按钮名称),否则我将不得不使函数或 if 语句等于我拥有的按钮数量!这对我来说没有任何意义!
stop();
import flash.events.MouseEvent;
import flash.events.Event;
import flash.net.URLLoader;
import flash.display.MovieClip;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
var myVar:String = new String();
//var myFrame:Array=new Array(26);
var words:Array =['Amon','Ahmos','Ramsis'];//the words
for (var j:int=0; j<words.length; j++)
{
var word:MovieClip = CreatTextButton(words[j]);//loop in myDictionary and name each button with a different name
addChild(word);
word.x = 130;
word.y = 16 + (j * 30);// to change the position of each word
word.addEventListener(MouseEvent.CLICK,onWordClick);
function CreatTextButton(label:String):MovieClip //creat buttons
{
var txt:TextField = new TextField();
txt.defaultTextFormat = new TextFormat('Calibri',20,0x000000);
txt.text = label;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.background = txt.border = true;
txt.selectable = false;
var btn:MovieClip = new MovieClip();
btn.label=label;
btn.mouseChildren = false;
btn.addChild(txt);
btn.buttonMode = true;
myVar=label.valueOf();
trace(myVar);
return btn;
}
function onWordClick(event:MouseEvent)
{
var myLoader:URLLoader= new URLLoader();
myLoader.load(new URLRequest("1.xml"));
myLoader.addEventListener(Event.COMPLETE,loadXML);
var label:String = event.currentTarget.label;
trace(label);
}
function loadXML(e:Event)
{
var myxml:XML = new XML(e.target.data);
mydata.text += String(myxml.label)+"\n";
}
}
我的XML是
<Pharaohs>
<Ahmos id='_1'>
reigned c. 1539–14 bce and founder of the 18th dynasty
</Ahmos>
<Amon id='_2'>
The chief deity of the Egyptian Empire
</Amon>
<Ramsis id='_3'>
The third Pharaoh of the 19th dynasty
</Ramsis>
</Pharaohs>
抱歉这么慢和愚蠢:D
您可以使用 event.currentTarget
来确定单击了哪个按钮。由于您使用动态 MovieClip
作为按钮,因此您可以在创建按钮时直接将标签指定为 属性:btn.label = label
。然后从您的按钮单击处理程序中,您可以使用 event.currentTarget.label
.
您可以在 E4X 过滤器表达式中使用此标签字符串值来提取 XML 中具有匹配 name()
值的节点。例如,如果您的 XML 看起来像这样:
<Pharoahs>
<Amon>Some text about Amon</Amon>
<Ahmos>All about Ahmos</Ahmos>
<Ramsis>This is Ramsis</Ramsis>
</Pharoahs>
您可以像这样提取名称与按钮标签匹配的 XML 节点:
var label:String = event.currentTarget.label;
var node:XML = xml.children().(name() == label)[0];
请注意,将 XML 节点命名为与您的文字相同可能不是一个好主意,因为对于如何命名不适用于字符串的 XML 节点有很多限制一般来说。例如,空格会成为问题,某些字符是不允许的,区分大小写会带来挑战,等等。相反,您可以使用属性(ex <pharoah name="Amon">
或子节点(ex <pharoah><name>Amon</name></pharoah>
)作为每个 XML 节点的键。这也使 XML 查找更容易:分别为 xml.pharoah.(@name == label)
或 xml.pharoah.(name == label)
。
另请注意,您无需在每次单击按钮时都重新加载 XML。预加载 XML、 然后 显示按钮可能更有意义,每次点击只需要在 XML 对象中查找数据。
将它们放在一起并进行一些小的代码改进:
- 重命名各种事物更有意义。
- 将所有函数移出 for 循环和其他函数。
- 首先加载 XML 并且只加载一次,然后渲染列表,生成的点击处理程序要简单得多。
结果:
const listOrigin:Point = new Point(130, 16);
const listItemSpacing:Number = 30;
const xmlPath:String = "1.xml";
var pharoahs:Array = ['Amon', 'Ahmos', 'Ramsis'];
var pharoahsXML:XML;
// First load your XML
loadXML();
function loadXML():void {
var urlLoader:URLLoader = new URLLoader();
urlLoader.load(new URLRequest(xmlPath));
urlLoader.addEventListener(Event.COMPLETE, loadXMLComplete);
}
function loadXMLComplete(e:Event) {
pharoahsXML = new XML(e.target.data);
// Render your buttons after the XML is loaded
renderPharoahsButtons();
}
function renderPharoahsButtons():void {
for (var i:int = 0; i < pharoahs.length; i++) {
var btn:MovieClip = createTextButton(pharoahs[i]);
btn.x = listOrigin.x;
btn.y = listOrigin.y + (i * listItemSpacing);
addChild(btn);
btn.addEventListener(MouseEvent.CLICK, pharoahButtonClick);
}
}
function createTextButton(label:String):MovieClip {
var txt:TextField = new TextField();
txt.defaultTextFormat = new TextFormat('Calibri', 20, 0x000000);
txt.text = label;
txt.autoSize = TextFieldAutoSize.LEFT;
txt.background = txt.border = true;
txt.selectable = false;
var btn:MovieClip = new MovieClip();
btn.mouseChildren = false;
btn.buttonMode = true;
btn.addChild(txt);
btn.label = label; // Store the button label for reference
return btn;
}
function pharoahButtonClick(e:MouseEvent):void {
// When a button is clicked is the label to extract the XML node
var label:String = e.currentTarget.label;
var pharoah:XML = pharoahsXML.children().(name() == currentPharoah)[0];
pharoahText.text += pharoah + "\n";
}