Can't reference a child with a variable instance name [syntax] - actionscript-3

I want to generate villagers for my game and then organize them into a list but I'm having a bit of difficulty, here is what it's supposed to look like and here is what it actually looks like. The code for generating kinsmen is:
var k = new kinsmen ;
menuArea.kinsmenDivider.addChild(k);
totalKinsmen++;
totalKinsmenAlive++;
k.name = "kinsmen" + totalKinsmen;
The code used for sorting them is:
for (var i:int = 1; i < (totalKinsmen+1); i++) {
if (menuArea.kinsmenDivider.getChildByName("kinsmen"+i) !=null) {
menuArea.kinsmenDivider["kinsmen" + i].y = menuItemCount * 107.5;
menuItemCount++;}}
That should look through the ID's of every kinsmen that's ever existed and when it finds the ID of one that's alive it should give it a place in the menu and continue going through the other IDs.
trace ("kinsmen" + totalKinsmen);
trace (menuArea.kinsmenDivider.getChildByName("kinsmen"+i));
Both always have the same values in my tests. The debugger posts the following error during runtime:
TypeError: Error #1010: A term is undefined and has no properties.
When I remove ["kinsmen" + i] it stops giving errors and the entire menu moves down each time a new kinsmen is generated so I know that's the part causing the issue but I don't know why or how to fix it and would appreciate help, all the above code is in my document class. I only started coding AS3 a few days ago so I'm still a bit shaky. Cheers for reading.

You access the clip correctly everywhere except
menuArea.kinsmenDivider["kinsmen" + i].y
shouldn't it be :
menuArea.kinsmenDivider.getChildByName("kinsmen" + i).y

Related

AS3 : 2 error 1061 and an error 1119

Sorry ahead of time for french in my code or badly translated errors (idk what they are in english so Google translate). I'm working on a program at school to add all numbers, all even numbers or all odd numbers (different buttons) from an array (seperate file, called U2A2_Elements.as) and I'm getting multiple errors, I'm getting :
1061: Call for indexOf method might not be defined via the static int type reference at entier = (entier.indexOf(entierSaisi));
1119: Access to the length property can not be defined via the reference type static int" at for (var i=entier; i entier.length; i++).
1061: Call for pop method might not be defined via the reference type static int" at entier.pop();.
Any help would be greatly appreciated as I have no idea what to do with the code nor does the teacher or anyone else.
EDIT: Forgot to put the link to the code http://pastebin.com/5nyf3z7g
In your supprimerFunction() function, you forgot that your array is mesEntiers ( and not entier which is an int object ), so I think that you should write :
function supprimerFunction(event:MouseEvent):void {
var entierSaisi:String;
var entier:int;
entierSaisi = (txtEntier.text);
entier = int(entierSaisi);
entier = mesEntiers.indexOf(entier);
if (entier != -1) {
for (var i = entier; i < mesEntiers.length; i++) {
entier[i] = entier[i + 1];
}
mesEntiers.pop();
}
}
Of course, I try just to remove errors mentioned in your question, and not improve your function.
Hope that can help.

Creating a maze in actionscript3

I4d like to create a maze in actionscript 3 but without drawing it because a lot of people are saying that timeline code and drawing things is bad so i'd like to create it with an array. I've searched on google and looked over a lot of tutorials, all doing it differently but not one of them uses classes and all that stuff and I'd really like to do it. I have the idea of how to do it, using an array filled with different numbers of characters if there's a wall, nothing etc... And i know how to draw the block with the graphics things then put a if loop and if the number is 0 put nothing if the number is 1 create the block and place it, but then i'm a bit lost on HOW to make the block appear at the same spot where there is a 1 in the array, I looked at tuts where they did something with rows but I couldn't really understand it clearly.
And also I'm not sure if i have to create a new class for the block, and what do I have to put in this class if i do create it? Do i need to create the block in the class, or outside of it? =/
If someone knows what I mean then all help is welcome.
If you need more details please tell me, sorry if it's confused. =3
It looks like your best bet, your path of least resistance in the long run, may lie in doing a little bit more study and some smaller programs before embarking on this. However if you want a 2D maze made with an Array, you could just do this:
private var m_arrMaze:Array = new Array(40);
private function someFunc():void
{
for (var i:int = 0; i < m_arrMaze.length; i++)
{
m_arrMaze[i] = new Array(50);
}
m_arrMaze[0][0] = 1;
m_arrMaze[0][1] = 1;
.
.
.
m_arrMaze[24][24] = 3;
.
.
.
m_arrMaze[49][49] = 0;
}
This is because you seemed to mention using an array and setting its elements to certain int values to denote what each little spot or room or whatever in the maze is or has. The reason a lot of tutorials may not use a whole lot of classes is because, if this is all you're doing with it, you really don't need too many different classes to denote the stuff in the maze. Just instead of using hard-coded int values, go ahead and put them in constants at the top of your maze class:
private static const EMPTY_SPACE:int = 0;
private static const WALL:int = 1;
.
.
.
private static const PLAYER:int = 3;
private var m_arrMaze:Array = new Array(40);
private function someFunc():void
{
for (var i:int = 0; i < m_arrMaze.length; i++)
{
m_arrMaze[i] = new Array(50);
}
m_arrMaze[0][0] = WALL;
m_arrMaze[0][1] = WALL;
.
.
.
m_arrMaze[24][24] = PLAYER;
.
.
.
m_arrMaze[49][49] = EMPTY_SPACE;
}
If each type of contents within the maze is liable to have a whole different set of nouns, verbs, and adjectives associated with it, instead of just being a different type of marker of where something's at like in the examples above, and if the program is going to do a lot of different things with those contents, that's when you want to use a whole bunch of different classes. Hopefully this will get you started.

Can you help me make more OOP abstraction with this code?

This code is not java code, and I'm not getting any answer from ActionScript developers. So I tagged it with java, but Action Script is similar to java and this an OOP question.
I'm using Grid Data and I want to accomplish this following task:
Method 1: I want to multiply each row Row1num1 * Row1num2 and so on,
var Row1num1:String;
var Row2num2:String;
var Row2num1:String;
var Row2num2:String;
var Row3num1:String;
var Row3num2:String;
var event1:Object={num1:Row1num1,num2:Row1num2};
var event2:Object={num1:Row2num1,num2:Row2num2};
var event3:Object={num1:Row3num1,num2:Row3num2};
then add them to a dataGrid
dataGrid.columns =["num1","num2"];
dataGrid.addItem(event1);
dataGrid.addItem(event2);
dataGrid.addItem(event3);
but by using this method, if I have 20 rows, I will have a lot of variables, obviously it's bad.
method 2: In this method creating Grid Data rows at runtime and multiply them.
//button to add rowGrid
dd.addEventListener(MouseEvent.CLICK,ddd);
var numm:String="34";
function ddd(evt:MouseEvent):void
{
var event4:Object={num1:Rownum1,num2:Rownum2};
dataGrid.addItem(event4);
}
but when I use this method, I have a hard time accessing each row data and multiply them.
This example because I'm creating GPA calculator and I want to take each row credit Hours and multiply them with the scale value at the same row, first method is bad because there's not abstraction .
The second method what I'm hoping to work ,because I want user to add row depend on their number of courses.
I hope my English is not bad.
I hope my question don't get vote down, and by reading this question can you determine what I'm missing so I can learn it .
And is there any tutorial I can use to solve my problem?
I'm just addressing your first method for now, but it almost seems at though you want an array of some sort.
Here's a link on how to use Actionscript arrays.
If you need more dimensions, you can make an array of arrays. This will help you cut down on the number of variables.
I hope I correctly understood your question. I'll give it a go either way...
So, one of the best things about actionscript in comparison to most other strongly-typed Object-Oriented languages is how easy reflection is (probably thanks to its javascript origins).
That being said, what you can do is simply create an array using a "for" loop. What I am assuming is that the variables row1Num1 row2Num2 and so on already exist in your class. Otherwise, obviously it would be much more efficient to store them in an array and simply read from it into a new array. Anyhow, the code should look something like this:
method 1:
var eventsArr:Array = [];
for(var i:int = 1; this["row" + i + "Num1"] != undefined /*or i<=length*/; i++){
eventsArr.push({num1:this["row" + i + "Num1"], num2:this["row" + i + "Num2"]});
}
for(var j:int = 0; j < eventsArr.length; j++){
dataGrid.addItem(eventsArr[j]);
}
method 2:
dd.addEventListener(MouseEvent.CLICK,ddd);
var numm:String="34"; //I am assuming this refers to the row number you wanted to add.
function ddd(evt:MouseEvent):void
{
var event4:Object={num1:this["row" + numm + "Num1"],num2:this["row" + numm + "Num2"]};
eventsArr.push(event4);
dataGrid.addItem(event4);
}
Hope that helps.

AS3 tracing datagrid content

I cant seem to work out to trace the content of a data grid i have populated with info;
Once I can work out how to trace it or each row i would push it into a new array for exporting.
so for example: i have a datagrid instanceNamed(info) //populated from a CVS file; text file//
containing 150 rows and 15 columns. I would simply like to trace this in the output window .From then i will work out how to write to disk.
i have been searching around but cant seem to find a solution to this problem.
thank you
Check out this tutorial: Using the Flex Automation API with Fluint
What you need is implemented in the getValuesFromGrid()-method. Check how the DataGridTabularData is used.
I have worked out how to trace a single cell or an entire DataGrid, Below is an example:
function trace_dataGrid(){
for (var k:Number = 0; k < DataGrid.length - 1; k++) {
var info += DataGrid.getItemAt(k).Team_name + "," +
DataGrid.getItemAt(k).Player_name+"\r\n";
trace(info);
}
}
OutPut
Australia Footy,Bob
Sydney Soccer,David
Australia Cricket,Jim
RunThrough:
First creat a loop counter "k" which is checking the Data Grid "length"
"get item AT" will check the first location,matching to the variable in the DataGrid
"TeamName" / " PlayerName is the next reference. After this has done it will repeat this "k++" Same structure until the condition is met(counter == datagrids Length-1) for better understanding the best bet would be how to structure Arrays,tuts ect.

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