I am trying to create a Combo Box (from the components list in 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)
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’re using splice(…) incorrectly. 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. Try
...splice(1,0,"...");.Alternatively, if you want the addition to come first in the array,
...splice(0,0,"...");may be a better choice.