Im trying to access an individual tile inside of my 2d array of tiles
Ive created my array like so:
private var map:Array = [[25,25]];
ive tried several ways including:
map[1][1] = 1;
all that does is give me this:
ReferenceError: Error #1056: Cannot create property 1 on Number.
ive also tried:
map[[1,1]] = 1;
nothing happends that I can tell
The only way that ive tried so far that gets me a result thats not an error is:
map[1,1] = 1;
The issue here is this selects the whole row.
Any help would be apreciated..thanks!
This is not correct way to create 2D array:
private var map:Array = [[25,25]];
This array contains one array which contains two elements. You can't address to the second element like this:
map[1][1] = 1;
because the second element (array) of map doesn't exist.
You can create 2D array this way:
var map:Array = [];
for (var i:int = 0; i < rows; i++)
{
map[i] = [];// this line adds new row to the 2D array
// To fill the array by zeros add next loop
for (var j:int = 0; j < cols; j++)
{
map[i][j] = 0;
}
}
To start, I think that to get the error mentioned in your question, your array should looks like this :
var map:Array = [
[25, 25], // map[0] = [25, 25] ( array of two elements )
35 // map[1] = 35 ( just a simple number )
];
So, if you write :
trace(typeof map[1]); // gives : number
You will get : number, and that's why you can not right : map[1][1] = value; and it's normal that you got the #1056 error.
Here, I don't know if you meant assign the value 1 to your 2nd 25 of map[0] or you want really add or edit map[1][1], in the 1st case, you can simply write :
map[0][1] = 1; // gives : map[0] = [25, 1]
In the 2nd case, you can do :
map[1] = [any_other_value, 1]; // gives : map[1] = [any_other_value, 1]
Last remark, forget that you got an error and suppose that your map array was just:
var map:Array = [
[25, 25]
];
Here, you can not also write map[1][1] = value;, why ? Let's use the same method :
trace(map[1]); // gives : undefined
So sure you can not add a property to an undefined, that's why when you write :
map[1][1] = value;
You will get an #1010 error : "undefined has no properties. ".
Of course here, we should firstly create map[1] :
map[1] = []; // or directly map[1] = [any_other_value, value]
And then :
map[1][1] = value;
Hope that can help.
Related
This is probably easy but I just cannot get the answer. Here is a simple Array:
I want the information to be distributed from an Input textBox to different dynamic textBox after a click. I am OK with buttons.
var ERLQ1:Array = ["ERLQ1", "N09°02.61 / E100°49.11", "ErawanLq"];
InputText = "ERLQ1";
//I want to display:
Txt1 = "ERLQ1" //Being first part of the array as main reference.
Txt2 = "N09°02.61 / E100°49.11" // Should be: String(ERLQ1[1])
Txt3 = "ErawanLq" // Should be: String(ERLQ1[2])
First time I write in a forum like this. Please forgive if not perfect. Thanks in advance.
Andre
If I understand correctly, an array of objects would work well. Since you have a set number of textfields I assume that you also have a set number of details that you want to display in them. If that is the case, this solution should work fine.
arr:Array = [{_name:"ERLQ1",ans1:"N09°02.61 / E100°49.11",ans2:"ErawanLq"},
{_name:"ERLQ2",ans1:"question 2 answer 1",ans2:"ques2ans1"}];
So, I don't really "get" your application, but if it were some sort of a quiz, you'd have a new array element for each question, and that element has a name, and two answers. Easy to modify it to grab answers from an answer pool. Now to find the element in the array that has ._name == "ERLQ1" you will need to loop through all the elements and return the one that has the ._name property that matches your search. Here is an example function:
private function matchName(arr:Array, term:String):int{
for (var i:int = 0; i < arr.length; i++){
if (arr[i]._name == term){
return i;
}
}
return -1;
}
This function will return the array index number of the matching term. If no match exists, it returns -1. So you could use it like this (pseudocode):
// on submit search{
// find the index number in the array of the element that matches the search term
var ind:int = matchName(arr, searchTerm);
// assign the textfield texts to the corresponding associated values
textBox1:text = arr[ind]._name;
textBox2:text = arr[ind].ans1;
textBox3:text = arr[ind].ans2;
}
I perhaps misunderstood (because of my English), but :
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
var ERLQ1:Array = ["ERLQ1", "N09°02.61 / E100°49.11", "ErawanLq"];
var Txt1 : TextField = new TextField();
Txt1.autoSize=TextFieldAutoSize.CENTER;
Txt1.type = TextFieldType.INPUT;
Txt1.border = true;
var Txt2 : TextField = new TextField();
Txt2.autoSize=TextFieldAutoSize.CENTER;
Txt2.type = TextFieldType.INPUT;
Txt2.border = true;
var Txt3 : TextField = new TextField();
Txt3.autoSize=TextFieldAutoSize.CENTER;
Txt3.type = TextFieldType.INPUT;
Txt3.border = true;
addChild(Txt1);
addChild(Txt2);
addChild(Txt3);
Txt1.x = 20, y =40;
Txt2.x = 180, y =40;
Txt1.x = 300, y =40;
Txt1.text = ERLQ1[0]; // is now : first part of the array as main reference (String(ERLQ1[0]).
Txt2.text = ERLQ1[1]; // is now : String(ERLQ1[1]);
Txt3.text = ERLQ1[2]; // is now : String(ERLQ1[2]);
This will display 3 TextFiels as input text like this :
If I misunderstood your question, please tell me more about what You expect!
Best regards.
Nicolas
was wondering if someone could show me what I'm doing wrong here.
I have some old AS2 flash code I'm trying to get working.
First I create a few arrays in frame 1 of the main timeline like so-
var typeArr:Array = new Array();
for (var i:Number = 1; i < 5; i++)
{
_root.typeArr[i] = "data goes here";
}
Then I have a movieclip dynamically attached on the main stage that when clicked appends one of the arrays we created by pushing the string 'foo' to it-
stop();
_root.myType=3;//this can be any of our array numbers
this.onPress=function(){
var foo:String="test";
_root.typeArr[_root.myType].push(foo);
trace(_root.typeArr[_root.myType]);
}
Where _root.typeArr[_root.myType] is the array name and number _root.typeArr3, but pushing the data does not work and returns nothing.
However, if I test it directly using-
_root.typeArr[_root.myType]=foo;
It will store the data once (_root.typeArr3=test), so I can't see why it won't push to that array as multiple elements each time like- "test,test,test"
It's driving me crazy.
Thanks! :)
_root.typeArr[_root.myType] is equal to "data goes here" so you are pushing string to a string, which doesn't work.
If you would like to append the new string, you should do something like:
_root.typeArr[_root.myType]+=foo;
and you will get: data goes heretest
If you have different data structure instead of "data goes here" the key may lie in the format of this data.
var typeArr:Array = new Array();
// 'i' must start from 0 because the first element is typeArr[0]
for (var i:Number = 0; i < 5; i++)
{
typeArr[i] = i;
// trace(typeArr[i]); // 0,1,2,3,4
}
// trace(typeArr); // 0,1,2,3,4
myType = 3;
bt.onPress = function()
{
var foo:String = "test";
// push method puts the element at the end of your array
// typeArr.push(foo);
// trace(typeArr); // 0,1,2,3,4,test
// splice method replace the 4e element (index 3) of your array
typeArr.splice(myType, 1, foo);
trace(typeArr); // 0,1,2,test,4
}
I have an array that makes multiple textfields based off a user variable. The code below is connected to a function that fires when text is added. From there it's supposed to find the widest text field, and then apply the width to rest of the textfields so they all are the same size.
Here is the code that is giving me problems. I could use a second look to see perhaps if my syntax is improper or if there's a better way at going at this.
for (var ied: int=1; ied>=electoff.electinput.textArray.length; ied++)
{
trace("one");
widest=(electoff.electinput.textArray["b"+ied].width>widest)?electoff.electinput.textArray["b"+ied].width:widest;
}
for (i =ied; i<electoff.electinput.textArray.length; ied++)
{
electoff.electinput.textArray["b"+ied].width = widest;
trace("two");
}
Here's the error I'm getting:
TypeError: Error #1010: A term is undefined and has no properties. at
NewPlv2_fla::MainTimeline/ajWidth()[NewPlv2_fla.MainTimeline::frame1:650] at NewPlv2_fla::MainTimeline/_keys()[NewPlv2_fla.MainTimeline::frame1:774]
AS3.0 Array is indices are zero-based, which means that the first element in the array is [0], the second element is [1], and so on.
so FYI you trying to access textArray["b"+ided]. but this syntax is a Associative arrays
I tested following code. check please.
import flash.text.TextField;
var arr:Array = [];
for(var i:int = 0; i<10; i++)
{
var tf:TextField = new TextField();
tf.width = 200 * Math.random();;
tf.name = "tf" + i;
tf.height = 100;
arr.push(tf);
trace(tf.name + ": " + tf.width);
}
var widest:Number = 0;
for (i=0;i<arr.length;i++)
{
if(arr[i].width>widest) widest = arr[i].width;
}
trace("widest: " + widest);
trace (suallar); - is written 2 times
1st time - HERE IT SHOWS ALL THE ELEMENTS OF THE ARRAY suallar
2nd time - BUT HERE THIS ARRAY SEEMS TO BE EMPTY, EVEN THOUGH I DIDN'T MANIPULATE WITH IT OR MAKE IT EQUAL TO ANYTHING I MANIPULATE WITH IN BETWEEN
var suallar:Array = new Array();
var i:int;
var cavablar:Array=new Array();
suallar.push(["sual1", "duz1", "sehv11", "sevh12", "sevh13","sevh14"]);
suallar.push(["sual2", "duz2", "sehv21", "sevh22","sevh23","sevh24" ]);
suallar.push(["sual3", "duz3", "sehv31", "sevh32","sevh33","sevh34"]);
suallar.push(["sual4", "duz4", "sehv41", "sevh42","sevh43","sevh44"]);
suallar.push(["sual5", "duz5", "sehv51", "sevh52","sevh53","sevh54"]);
var cavablar_temp:Array = suallar.concat();
for (i=0; i<suallar.length; i++){
cavablar_temp[i].shift();
}
trace (suallar);
for (i=0; i<suallar.length;i++){
var number_array:Array = cavablar_temp[i];
var final_array:Array = [];
var count_selected:int = 5;
for (var u = 0; u < count_selected; u++)
{
if (number_array.length == 0)
{
break;
}
else
{
final_array.push(number_array.splice(Math.floor(Math.random() * number_array.length), 1)[0]);
}
}
cavablar.push(final_array);}
trace(cavablar.join("\n"));
trace (suallar);
As per the Array.splice() documentation:
Adds elements to and removes elements from an array. This method modifies the array without making a copy.
When you do number_array.splice() in the middle of your loop, you're modifying the original arrays you pushed to suallar.
Take a look at Array.slice(), which returns a new array without modifying the original.
I am running into some trouble adding an array into another array to create a multi-dimensional array.
The code appears as below:
var slideDataArray:Array = new Array();
var slideShowDataArray:Array = new Array();
slideDataArray[0] = xmlData.SlideShowParameters.SlideShowImagesDirectory;
slideDataArray[1] = xmlData.SlideShowParameters.SlideShowTimeInterval.valueOf();
slideDataArray[2] = xmlData.SlideShowParameters.SlideShowWidth.valueOf();
slideDataArray[3] = xmlData.SlideShowParameters.SlideShowHeight.valueOf();
slideDataArray[4] = slides.length();
slideShowDataArray[0] = slideDataArray;
for (i = 0; i < slides.length(); i++) {
// Read data from Slides tag in the XML file into slideDataArray
slideDataArray[0] = slides[i].SlideImage.toString();
slideDataArray[1] = slides[i].SlideText.toString();
slideDataArray[2] = slides[i].SlideLink.toString();
// Input the data from slideDataArray into the array for the slideshow (slideShowDataArray)
slideShowDataArray[i + 1] = slideDataArray;
}
// end of FOR loop
I am looking for a means of placing the slideDataArray into a 'slot' or value of slideShowDataArray so that I can in the end pass the slideShowDataArray as a parameter to another function.
As of now, the last slideDataArray appears 11 times (the loop runs 11 times) in slideShowDataArray and the way the code is written the slideDataArray is unique every iteration of the loop.
Any help is appreciated.
Thanks in advance...
Remember you are not adding an array, but a reference to slideDataArray to your multidimensional array. Each reference points to the same array - which just contains different values on every iteration of the loop. In other words: Every time you add that reference, you "link" to the same address in memory.
To get around this, move the inner part of the loop to a separate function and create a new local array on every call:
function createDataArray ( slide:Object ) : Array {
var slideDataArray:Array = [];
slideDataArray[0] = slide.SlideImage.toString();
slideDataArray[1] = slide.SlideText.toString();
slideDataArray[2] = slide.SlideLink.toString();
return slideDataArray;
}
Then call it from your loop:
for (i = 0; i < slides.length(); i++) {
slideShowDataArray.push( createDataArray (slides[i]) );
}
You should end up with 11 unique arrays instead of one array that is overwritten 11 times.