as3中的组合框历史

时间:2012-02-13 19:47:31

标签: flash actionscript-3 flash-cs4

我正在尝试创建一个组合框(来自flash cs4中的组件列表)。我已经把它与

一起使用了
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);
    }
}

然后我尝试创建一个历史记录,如果点击了什么(在weaponboxselecter函数中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);
}

但它始终追踪剑

任何建议

感谢

[新规范]

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]
    }

这仍然无效。我会更好地解释一下:

首先选择剑。然后,当我点击弓/刀时,剑被刀/刀替换。这样我就可以根据最后点击的内容添加和删除对象(如果你有其他方法可以做到这一点,我可以选择其他选项。)

正在发生的是剑被追踪,如果我点击刀,剑被追踪,如果我点击弓,剑被追踪但是如果再次点击剑或刀而不是弓。

感谢

[新规范]

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");



}
}
除了刀之外,一切都有效。它只追踪剑......

2 个答案:

答案 0 :(得分:0)

您应该逐步了解您的代码正在做什么。

在第二个代码示例中,在第一行,您将weaponsHistory设置为["sword"]。所以当你追踪它时它将是剑。

然后,在selectKnifeselectSword方法中,您将"Knife""Sword"添加到1数组的索引weaponsHistory,然后跟踪它

似乎问题就是selectBow方法。您甚至可以在修改它之前跟踪weaponsHistory

只是一张纸条;如果您尝试将项目添加到数组的末尾,则应该使用推送而不是拼接(使用weaponsHistory.push("Bow");而不是weaponsHistory.splice(1, "Bow");

答案 1 :(得分:0)

您错误地使用了splice(...)。我不完全确定发生了什么,但正确使用splice包括起始索引删除计数和可选的附加内容。试试...splice(1,0,"...");

或者,如果您想要在数组中首先添加,...splice(0,0,"...");可能是更好的选择。