[英]Combo Box history in as3
I am trying to create a Combo Box (from the components list in flash cs4). 我正在尝试创建一个组合框(从Flash CS4中的组件列表中)。 I have gotten it to work with 我已经可以使用了
import fl.accessibility.ComboBoxAccImpl;
import fl.data.DataProvider;
import fl.events.ComponentEvent;
ComboBoxAccImpl.enableAccessibility();
var weapons:Array = [
{label:"Sword", data:"SwordSelected"},
{label:"Bow", data:"BowSelected"},
{label:"Knife", data:"KnifeSelected"}];
testtxt.text = String("sword");
weaponselector.dataProvider = new DataProvider(weapons);
weaponselector.addEventListener(ComponentEvent.ENTER, weaponsadd);
weaponselector.addEventListener(Event.CHANGE, weaponboxselecter);
function weaponboxselecter(event:Event):void
{
if (ComboBox(event.target).selectedItem.data != "SwordSelected")
{
selectSword();
testtxt.text = String(ComboBox(event.target).selectedItem.label);
}
else if (ComboBox(event.target).selectedItem.data != "BowSelected")
{
selectBow();
testtxt.text = String(ComboBox(event.target).selectedItem.label);
}
else if (ComboBox(event.target).selectedItem.data != "KnifeSelected")
{
selectKnife();
testtxt.text = String(ComboBox(event.target).selectedItem.label);
}
}
but then i tryed to create a history if what is being clicked(selectKnife/selectBow/selectSword in the weaponboxselecter function) 但是然后我尝试创建一个历史记录是否被点击(武器箱选择器功能中的selectKnife / selectBow / selectSword)
var weaponsHistory:Array = ["Sword"];
trace(weaponsHistory);
function selectBow()
{
var Bowadds:Bowadd = new Bowadd();
Bowadds.x = 300
Bowadds.y = 300
stage.addChild(Bowadds);
trace(weaponsHistory);
weaponsHistory.splice(1, "Bow");
}
function selectKnife()
{
var Knifeadds:Bowadd = new Bowadd();
Knifeadds.x = 300
Knifeadds.y = 300
stage.addChild(Knifeadds);
weaponsHistory.splice(1, "Knife");
trace(weaponsHistory);
}
function selectSword()
{
var Swordadds:Bowadd = new Bowadd();
Swordadds.x = 300
Swordadds.y = 300
stage.addChild(Swordadds);
weaponsHistory.splice(1, "Sword");
trace(weaponsHistory);
}
but it always traces sword 但它总是可以追溯到剑
any suggestions 有什么建议么
thanks 谢谢
[New Code] [新代码]
var weaponsHistory:Array = ["Sword"];
function weaponboxselecter(event:Event):void{
if(ComboBox(event.target).selectedItem.data != "SwordSelected"){
selectSword();
testtxt.text = String(ComboBox(event.target).selectedItem.label);
weaponsHistory.splice(0,0,"Sword");
}else if(ComboBox(event.target).selectedItem.data != "BowSelected"){
selectBow();
testtxt.text = String(ComboBox(event.target).selectedItem.label);
weaponsHistory.splice(0,0,"Bow");
}else if(ComboBox(event.target).selectedItem.data != "KnifeSelected"){
selectKnife();
testtxt.text = String(ComboBox(event.target).selectedItem.label);
weaponsHistory.splice(0,0,"Knife");
}
[new code]
trace(weaponsHistory);// just less code
[end new code]
}
this still doesn't work. 这仍然行不通。 I will explain it a bit better: 我会更好地解释一下:
The Sword is first selected. 首先选择剑。 Then when i click on the Bow/Knife the Sword is replaced with Bow/Knife. 然后,当我单击“弓/刀”时,剑被替换为“弓/刀”。 That way i can add and remove objects based on what was last clicked(if you have another way of doing this i am up for other options.) 这样,我可以根据上次单击的内容添加和删除对象(如果您有另一种方法可以使用其他选项。)
What is happening is that the Sword is being traced than if i click on knife, sword is traced and if i click on bow, Sword is traced but if the click on sword or knife again than bow is traced. 发生的事情是,如果我单击刀,就跟踪了剑,如果我单击弓,则跟踪了剑;如果我单击弓,则跟踪了剑,但是如果跟踪了比弓再次单击剑或刀,则跟踪了。
thanks 谢谢
[new Code] [新代码]
function selectBow(){
var Bowadds:Bowadd = new Bowadd();
Bowadds.x = 300
Bowadds.y = 300
stage.addChild(Bowadds);
trace(weaponsHistory);
}
function selectKnife(){
var Knifeadds:Bowadd = new Bowadd();
Knifeadds.x = 300
Knifeadds.y = 300
stage.addChild(Knifeadds);
trace(weaponsHistory);
}
function selectSword(){
var Swordadds:Bowadd = new Bowadd();
Swordadds.x = 300
Swordadds.y = 300
stage.addChild(Swordadds);
trace(weaponsHistory);
}
function weaponsadd(event:ComponentEvent):void {
var newRow:int = 0;
if (event.target.text == "Add") {
newRow = event.target.length + 1;
event.target.addItemAt({label:"screen" + newRow, data:"screenData" + newRow},
event.target.length);
}
}
function weaponboxselecter(event:Event):void{
if(ComboBox(event.target).selectedItem.data != "SwordSelected"){
selectSword();
testtxt.text = String(ComboBox(event.target).selectedItem.label);
weaponsHistory.push("Sword");
}else if(ComboBox(event.target).selectedItem.data != "BowSelected"){
selectBow();
testtxt.text = String(ComboBox(event.target).selectedItem.label);
weaponsHistory.push("Bow");
}else if(ComboBox(event.target).selectedItem.data != "KnifeSelected"){
selectKnife();
testtxt.text = String(ComboBox(event.target).selectedItem.label);
weaponsHistory.push("Knife");
}
}
It all works except for the knife. 除刀外,其他所有功能均有效。 It only traces sword... 只是剑的痕迹
You should look through step-by-step at what your code is doing. 您应该逐步查看代码的功能。
In your second code example, on the first line, you set weaponsHistory
to ["sword"]
. 在第二个代码示例中,在第一行中,将weaponsHistory
设置为["sword"]
。 So it will be sword when you trace it. 因此,当您跟踪它时它将是剑。
Then in your selectKnife
and selectSword
methods you add "Knife"
or "Sword"
to index 1
of your weaponsHistory
array, then trace it. 然后在selectKnife
和selectSword
方法中,将"Knife"
或"Sword"
到weaponsHistory
数组的索引1
中,然后对其进行跟踪。
Seems like you problem is it the selectBow
method. 似乎您的问题是selectBow
方法。 Looks like you trace weaponsHistory
before you even modify it. 看起来您在修改weaponsHistory
之前就已经对其进行了跟踪。
Just a note; 只是一个音符; If you are trying to add an item to the end of an array you should probably use push rather than splice (use weaponsHistory.push("Bow");
rather than weaponsHistory.splice(1, "Bow");
) 如果您尝试将项目添加到数组的末尾,则可能应该使用push而不是splice(请使用weaponsHistory.push("Bow");
而不是weaponsHistory.splice(1, "Bow");
)
You're using splice(...) incorrectly. 您使用了错误的splice(...) 。 I'm not entirely certain what is occuring, but the proper use of splice includes a start index , a delete count and and the optional additions. 我不完全确定发生了什么,但是对splice的正确使用包括开始索引 , 删除计数和可选的添加。 Try ...splice(1,0,"...");
尝试...splice(1,0,"...");
. 。
Alternatively, if you want the addition to come first in the array, ...splice(0,0,"...");
或者,如果希望加法运算在数组中排在首位,则...splice(0,0,"...");
may be a better choice. 可能是一个更好的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.