Skip to main content
Known Participant
December 13, 2007
Answered

Using ComboBox data property with addChild

  • December 13, 2007
  • 3 replies
  • 440 views
I'm using AS3 with a ComboBox component and when the items inside the comboBox are clicked on, I want to add items from the library to my stage.

What I'm trying to do is access the data property of the addItem method and pass that into the addChild method. Since it's passing the data property in as a string, it won't work. And I'm unable to find a workaround other than a bunch of if statements based on the selectedIndex property of the comboBox. I prefer not to use the if statements... it's messy and even more so when I have to removeChild. Below is my code which is not working because the data property is being passed in as a string which isn't accepted by the addChild method. Any suggestions?


This topic has been closed for replies.
Correct answer Newsgroup_User

var newCat:Cat = new Cat();
var newDog:Dog = new Dog();
var newHam:Hamster = new Hamster();

combo.addItem({label:"Cat", data:newCat});
combo.addItem({label:"Dog", data:newDog});
combo.addItem({label:"Hamster", data:newHam});

combo.addEventListener(Event.CHANGE, showImage);

function showImage(myEvent:Event):void {
var theTarget:DisplayObject =
myEvent.currentTarget.selectedItem.data as DisplayObject;

stage.addChild(theTarget);
}


3 replies

_B_Author
Known Participant
December 21, 2007
Thank you Raymond. It works perfect!!!

Brenda
Newsgroup_UserCorrect answer
Inspiring
December 14, 2007

var newCat:Cat = new Cat();
var newDog:Dog = new Dog();
var newHam:Hamster = new Hamster();

combo.addItem({label:"Cat", data:newCat});
combo.addItem({label:"Dog", data:newDog});
combo.addItem({label:"Hamster", data:newHam});

combo.addEventListener(Event.CHANGE, showImage);

function showImage(myEvent:Event):void {
var theTarget:DisplayObject =
myEvent.currentTarget.selectedItem.data as DisplayObject;

stage.addChild(theTarget);
}


Damon Edwards
Inspiring
December 13, 2007
var newCat;
var newDog;
var newHam;

combo.addItem({label:"Cat", data:"newCat"});
combo.addItem({label:"Dog", data:"newDog"});
combo.addItem({label:"Hamster", data:"newHam"});

combo.addEventListener(Event.CHANGE, showImage);

function showImage(myEvent:Event):void {
var theTarget:Object = myEvent.currentTarget.selectedItem.data;
if(myEvent.currentTarget.selectedItem.data == "newCat"){
newCat = new Cat();
addChild(newCat);
}
if(myEvent.currentTarget.selectedItem.data == "newDog"){
newDog = new Dog();
addChild(newDog);
}
if(myEvent.currentTarget.selectedItem.data == "newHam"){
newHam = new Hamster();
addChild(newHam);
}
}
_B_Author
Known Participant
December 13, 2007
Hi dzedward, thank you for your reply. I was trying to avoid using if statements to get this to work. My code that you see below is just a sample... I have many more images than what you see in my code below. And because I have to do removeChild for each one, it just becomes messy with all the if statements for the addChild and all the code for the removeChild.

I'm trying to find a better workaround than that. I wish there was some way to convert the data string to something that the addChild method could work with. That way I can just pass it in and have it work.

Anyone have any other suggestions?