AS3 selecting a Combo Box inside a Movie Clip - actionscript-3

I am not sure if there is a better way to do this, and if there is please let me know. But right now, I have a list of combo boxes with names in them.
The combo boxes are stu1, stu2, stu3, ect all the way to 63 and held in the allStudents MovieClip
for(var i = 0; i < allStudents.length; i++)
{
var newTempStudent:ComboBox = allStudents.getChildAt(i);
newTempStudent.dataProvider.addItem({label: fullName, data:fullName});
newTempStudent.getChildAt(i).dataProvider.sortOn("label");
newTempStudent.getChildAt(i).selectedItem = allStudents.getChildAt(i).getItemAt(i);
}
essentially i am trying to:
get all 63 combo boxes to update from the same dataProvider,
sort them alphabetically,
then set the default selected to each student (stu1 should display dataProvider(0), stu2 should display dataProvider(1) as it's default selection)
The error I am getting is:
Scene 1, Layer 'Layer 1', Frame 1, Line 83 1118: Implicit coercion of a value with static type flash.display:DisplayObject to a possibly unrelated type fl.controls:ComboBox.
Now I am assuming I am getting that becasue it is looking is the movie clip which is the display object and executing combobox commands, but I'm not sure how to do this per se.
Originally I was going to do this with a dataGrid, but it became too complicated when I was trying to link the dataGrid to checkBoxes for attendance.
Any and all help is greatly appreciated!

Try this code :
for(var i = 0; i < allStudents.length; i++)
{
var newTempStudent:ComboBox = allStudents.getChildAt(i) as ComboBox;
newTempStudent.dataProvider.addItem({label: fullName, data:fullName});
newTempStudent.getChildAt(i).dataProvider.sortOn("label");
newTempStudent.getChildAt(i).selectedItem = allStudents.getChildAt(i).getItemAt(i) as ComboBox;
}
getChildAt() returns a DisplayObject, so you need to cast it as a ComboBox.

Related

AS3 concat // MovieClip(parent).variable.gotoAndStop(otherVariable);

Is there any concatenation problem in
MovieClip(parent).robes_position_num_clicked.gotoAndStop(traje1);
When I click on item n°2, trace('robes_position_num_clicked') returns "robes_position2", which is correct & what expected.
var index_clicked:int = myPositions.indexOf(e.target); // = Position num in array
var robes_position_num_clicked ="robes_position"+(index_clicked+1).toString();
trace (robes_position_num_clicked);
var traje1 = MovieClip(parent).robes_position1.currentLabel;
if ( MovieClip(parent).robes_position1.currentLabel==traje1 && MovieClip(parent).robes_position1.currentLabel!=null)
{ /*1*/ MovieClip(parent).robes_position2.gotoAndStop(traje1);
/*2*/ MovieClip(parent).robes_position_num_clicked.gotoAndStop(traje1);
MovieClip(parent).robes_position1.gotoAndStop(1);
}
The first "sentence" /* 1*/ does the expected job,
while sentence /* 2*/ which is supposed to be a synonym, doesn't, and returns an error "One term is undefined and has no property".
Thanks for your lights as we say in french !
You should know that robes_position_num_clicked here is just a String containing the name of your target MovieClip and not the MovieClip itself, so this line :
MovieClip(parent).robes_position_num_clicked.gotoAndStop(traje1);
should give you an error.
So, to access your MovieClip using its name, you can simply use the DisplayObjectContainer.getChildByName() function like this :
MovieClip(parent.getChildByName(robes_position_num_clicked)).gotoAndStop(traje1);
you can also do like this :
MovieClip(parent)[robes_position_num_clicked].gotoAndStop(traje1);
Hope that can help.
robes_position_num_clicked is a string not a MovieClip
"gotoAndStop" should be used with MovieClips not Strings ;
secondly: "robes_position_num_clicked" is defined in the current MovieClip not in the parent MovieCLip so it will be null in the parent MovieClip

How do i populate an Array in one class based on a textfield in another class?(Actionscript 3.0)

i have a class (TheList.as). in which i have an array "Data" and it has a couple of values. Then i have a loop through which i am creating a scrollable list which uses the values from "Data" array. [I am trying make a unit converter]
Then i have another class "Units.as". In that class i have created three instances of "TheList". A main list ("myList"), and to sublists "ListFrom" and "ListTo". They are using values from "Data" array. Now i have text field whose value changes to whatever item is clicked. When i click "Angle" in the main list, i want the sublists to get populated with ("Degree", "Radian" etc)..
Here is what i tried
if(myList._TextLabel.text == "Angle")
{
ListFrom.Data = ["Degree", "Radian"];
}
But nothing happens, i do not get any error either. When i do this in an "ENTER_FRAME" event and trace (ListFrom.Data), i can see that the values change, but they do not get assigned to the list items in the list. I would really appreciate the help. Thanks!
Here are complete Classes for understanding the situation better(the code is pretty messy, as i am a newbie to OOP)
TheList.as: http://pastebin.com/FLy5QV9i
Units.as : http://pastebin.com/z2CcHZzC
where you call ListFrom.Data = ["Degree","Radian"], make sure when the data changed, the renders in the ListFrom have been set new data. for example, you may use MyRender in ListFrom for show, you should debug in the set data method in MyRender.
you should call the code below after you call ListFrom.Data = ["Degree","Radian"];
for (var i:int = 0; i < Data.legnth;i++) {
var render:MyRender = ListFrom[i] as MyRender;
if (render) {
render.data = Data[i];
} else {
var render:MyRender = new MyRender();
render.data = Data[i];
ListFrom.addChild(render);
}
}
You can use event listeners, singleton classes or reference one class to another, depending on the style you want. All are equally valid and fast / efficient.

Trying to remove all children, receiving error #1009

The idea with my setup is that I have an input text field and three separate buttons on stage. When you type something in the text field and press the input button, the text inside the field is added to an array.
When you press the display button the contents of the array are displayed on screen (each value of the array is displayed underneath the last value).
The final button is supposed to remove all the current values on the array, and clear all displayed values on screen. But I cannot get my code to work as intended, since I receive,
TypeError: Error #1009: Cannot access a property or method of a null object reference.
With this block of code:
import flash.text.TextField;
import flash.events.MouseEvent;
var myArray:Array = new Array ("");
var tf:TF;
btnInput.addEventListener(MouseEvent.CLICK, txtInput);
function txtInput(event:MouseEvent):void
{myArray.push(txtInput.text);}
btnDisplay.addEventListener(MouseEvent.CLICK, txtDisplay);
function txtDisplay(event:MouseEvent):void
{for (var i:int = 0; i < myArray.length; i++)
{var tf:TF = new TF();
tf.txt.text = myArray[i];
tf.y = 280 + (i * 25);
tf.x = 265;
addChild(tf);
tf.name="test";}
}
btnClear.addEventListener (MouseEvent.CLICK, txtClear);
function txtClear(event:Event){
myArray.splice(1);
if (tf.numChildren != 0){
removeChild(getChildByName("test"));}
}
Alternatively, when I add
var tf:TF = new TF;
It removes only one displayed value on screen. Also I might add, that "TF" is a movie clip in the library that contains a dynamic text field that's instance name is txt. Is the problem only with the last button, or should I change something else as well? I don't know how to make this work as I want it to. I'm pretty new to coding so any tips or help would be much appreciated. Thanks in advance!
In your condition if(tf.numChildren != 0) the tf object is null since you never created it. That's why you're getting the #1009 error.
On the other hand, in order to delete all items you should replace the if (tf.numChildren != 0) with while (tf.numChildren != 0).

Creating a user generated list in flash

I'm trying to create a flash application that will keep track of user generated values. The app should basically allow the user to input the name of the item and it's cost. The total costs should then be added up to show a total value to the user. I can probably figure out how to add the values together, but I'm not really sure how to allow the user to create a list and then allow the user to save it. Can anyone point me towards a tutorial or point me in the right direction?
I am using variables to add user inputed numbers to come up with a total. The first problem is that actionscript 3.0 does not allow variables for texts. I just converted it to 2.0 to fix this. The second problem, is when I test the app and put in my values and click submit, I get NaN in the total values field. Is there a reason why it wouldn't add the values?
Here is the code I used for the submit button:
on (release) {
total = Number(rent) + Number(food) + Number(travel) + Number(entertainment) + Number(bills);
}
Am I missing anything?
Can I give the input text instance names and then give them variables? How are some ways to go about this?
Thanks for the help!
Have an object array, say for example
var stack:Array = new Array();
Then push the item name and it's cost to that array when user inputs, like
stack.push({item:AAA, cost:xx});
So that you can generate the list whenever you want with that array.
You have to see how this works in code. A list in actionscript could be stored inside an array, vector, dictionary or even an Object.
Var myList:Array = [];
myList.push({name: "item 1", cost: 5 });
myList.push({name: "item 2", cost: 7.5 });
If you want to grab the 'product' of "item 1" from the list, you have to create a function for that, lets call it getProductByName
function getProductByName(name:String):Object
{
for each(var product:Object in myList)
{
if (product.name === name) return product;
}
return null; // no match found
}
You can call that function like this:
var product = getProductByName("item 1");
trace(product.cost); // 5
And you can alter the product, so lets make it more expensive
product.cost += 1;
trace(product.cost); // 6
Have fun! If you are using classes, you would create one for the product, with public name and cost, and in that case you'de better use a vector, to ensure working with the right type.
This is what fixed the issue for me in action script 3.0:
myButton.addEventListener(MouseEvent.CLICK, addThem);
function addThem(e:MouseEvent)
{
totalField.text = String ( Number(field1.text) + Number(field2.text) + ....);
}
I also had to name the instances appropriately.

Re-stacking MovieClips in an Array

I was trying to make a similar thing with the game SameGame (ie. the block above the removed blocks fall downward). Before trying this with an Array that contains MovieClips, this code worked (tried it with int values). With MovieClips on the array, it seems not working the same way.
With int values, example:
popUp(0, 4): Before: 1,2,3,4,5,6,7,8,9,10; After: 1,2,3,4,6,7,8,9,10
But with MovieClips:
popUp(0, 4): Before: 1,2,3,4,5,6,7,8,9,10; After; 1,2,3,4
// Assume the numbers are movieclips XD
Basically, it strips everything else, rather than just the said block >_<
Here's the whole method. Basically, two extra arrays juggle the values above the soon-to-be removed value, remove the value, then re-stack it to the original array.
What could be wrong with this? And am I doing the right thing for what I really wanted to emulate?
function popUp(col:uint, row:uint)
{
var tempStack:Array = new Array();
var extraStack:Array = new Array();
tempStack = IndexArray[col];
removeChild(tempStack[0]);
for(var ctr:uint = tempStack.length-(row+1); ctr > 0; ctr--)
{
removeChild(tempStack[ctr]);
extraStack.push(tempStack.pop());
trace(extraStack);
}
tempStack.pop();
for(ctr = extraStack.length; ctr > 0; ctr--)
{
tempStack.push(extraStack.pop());
//addChild(tempStack[ctr]);
}
IndexArray[col] = tempStack;
}
PS: If it's not too much to ask, are there free step-by-step guides on making a SameGame in AS3 (I fear I might not be doing things right)? Thanks in advance =)
I think you just want to remove an element and have everything after that index shift down a place to fill what you removed. There's an inbuilt function for this called splice(start:uint, length:uint);
Parameters:
start - the index to start removing elements from
length - the amount of elements to remove
var ar:Array = ["hello","there","sir"];
ar.splice(1, 1);
ar is now -> ["hello", "sir"];
As per question:
Here's an example with different types of elements:
var ar:Array = [new MovieClip(), "some string", new Sprite(), 8];
ar.splice(2, 1);
trace(ar); // [object MovieClip], some string, 8
And further example to display the indexes being changed:
trace(ar[2]); // was [object Sprite], is now 8