Can't create array of arrays - actionscript-3

I'm trying to create array of arrays (like a 2d array) however I'm getting error:
TypeError: Error #1006: value is not a function.
Here's my code:
I'm using Flash Professional CC 2015. How can I fix this error?
EDIT: Here's the full function:
function CreateMainMenu(xPos:Number, yPos:Number, depth:int, menu_xml:XML):void {
// Generate menu list
var arr:Array = new Array();
addChild(mainmenu_mc);
mainmenu_mc.x = xPos;
mainmenu_mc.y = yPos;
setChildIndex(mainmenu_mc, depth);
var num:int = 0;
for each (var tempNode:XML in menu_xml.elements()) {
var arr2:Array = new Array();
arr2.push(tempNode);
arr2.push("menu");
arr[num].push(arr2); // It gives error
num++;
}
trace (arr);
// GenerateMenu(this, "mainmenu_mc", xPos, yPos, depth, arr);
}
The first line number is 58, the last one is 79.
I'm getting this error:
TypeError: Error #1010: A term is undefined and has no properties.
at xmlmenu_05_fla::MainTimeline/CreateMainMenu()[xmlmenu_05_fla.MainTimeline::frame1:72]
at xmlmenu_05_fla::MainTimeline/processXML()[xmlmenu_05_fla.MainTimeline::frame1:118]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()

The problem is that you never add anything to arr.
You create the Array here:
var arr:Array = new Array();
but the only time you interact with it is the line that gives you the error:
arr[num].push(arr2); // It gives error
You are trying to access an element here, but you never added anything to the array.
Your variables names are not very descriptive and you likely got lost in this mess:
var arr2:Array = new Array();
arr2.push(tempNode);
arr2.push("menu");
arr[num].push(arr2); // It gives error
num++;
I cannot tell what your intentions are here. IF you just want to add arr2 as the next element, use push, there's no need for num here.
If you write code with meaningful variables names it's easier to keep track of your own code.

Related

How to "restart" my as3 code

I've got some AS3 codes on my 4th frame.
var words:Array = new Array; //a mini database to hold the words
var current_word:String; //used to hold the current word being tested
var ques_num:int; //used to track the question number
var tileArray:Array = new Array; //array of tile objects
var targetArray:Array = new Array; //array of target areas
var scramble_Array:Array = new Array; //array used to scramble the word
getword();
function getword() {
words=["screen"];
current_word=words[ques_num];
setTiles(current_word.length);
ques_num++;
}//getword
function setTiles(a) {tileArray=[ ];
for(i=0;i<a;i++){
var tempclip:Tile =new Tile;addChild(tempclip);
tempclip.x=150+(i*120);tempclip.y=500;tempclip.tag=i;
tempclip.original_posx=tempclip.x;
tempclip.original_posy=tempclip.y;
tileArray[i]=tempclip;
var tempclip2:Placeholder =new Placeholder;addChild(tempclip2);
tempclip2.x=150+(i*120);tempclip2.y=620;
targetArray[i]=tempclip2;
}//for i
scramble_word(a);
}//setTiles
I wanted to put a button where the user can click in order to go back to frame 1.
goBack1.addEventListener(MouseEvent.CLICK, gotoOne, false, 0, true);
function gotoOne(event:MouseEvent):void{
gotoAndStop(1);
}
When I do so, we can still see the child of the 4th frame..
So I removed them.
trace("clear_board");
removeChild(checker);
pauser=false;
for(i=0;i<tileArray.length;i++){
removeChild(tileArray[i]);
removeChild(targetArray[i]);
}
}
But now, when the user is going back to the 4th frame, an error is displayed :
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at RECUPERE_jeumuseev1_fla::Jules_108/getword()[RECUPERE_jeumuseev1_fla.Jules_108::frame4:95]
The line 95 is this :
setTiles(current_word.length);
I suppose that the error come from the fact that I've "removed" tileArray[i] but I'm not sure..
What do you suggest I could do to simply "restart" the all code from this frame, like the first time the user is coming to the 4th code ?

Flash Professional Error 2006

I have looked over this and I must be blind as I cannot see what the problem is. I looked a bit online and tried to modify it to work, but no luck.
function dragTheObject(event:MouseEvent):void {
var item:MovieClip = MovieClip(event.target);
item.startDrag();
var topPos:uint = (item, numChildren > 0 ? numChildren-1 : 0);
this.parent.setChildIndex(item, topPos);
}
The AS3 #2006 runtime error ( RangeError: Error #2006: The Supplied Index is Out of Bounds ) is fired by this line :
this.parent.setChildIndex(item, topPos);
because your trying to set an index to your item object which is greater than (or equal to) the DisplayObjectContainer's (this.parent) numChildren property.
So to put your object on the top, you can simply do :
function dragTheObject(event:MouseEvent):void
{
var item:MovieClip = MovieClip(event.target);
item.startDrag();
item.parent.setChildIndex(item, item.parent.numChildren - 1);
}
Hope that can help.

AS3 Remove items in an ENTER_FRAME

I have generated random bubbles, I used a code I found in the net. Now I want a click event that will hide a random bubble.
Here is exactly the code I used,
http://good-tutorials-4u.blogspot.com/2009/04/flash-bubbles-with-actionscript-3.html
I got the bubbles running good...
I have tried this, and so far no luck..
addEventListener(MouseEvent.CLICK, eventListener);
function eventListener(eventObject:MouseEvent) {
bubbles[i].splice(i,1,bubbles[i]);
}
I tried using an array but it returns me this output
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at Function/()
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at Function/()
If you have the bubbles in an array this should work.
var randomIndex:int = int(Math.random() * bubbles.length);
parent.removeChild(bubbles[randomIndex]);
bubbles.splice(randomIndex, 1);
Notice that you have to remove the bubble from the display list too.
You can try creating a new array without a random element from the original array. Then just reassign the old array to the new one, e.g.
// get the random index to remove element at
var randomIndex:int = 0 + bubbles.length * Math.random();
var index:int = 0;
// create new array containing all apart from the chosen one
var newBubbles:Array = [];
for each (var item:Object in bubbles) {
if (index != randomIndex) {
newBubbles.push(item);
}
index++;
}
// here you go new array without one random item
bubbles = newBubbles;
Or something like these.
Just a minor revision on Baris Usakli's code here, this is if you want the one that was clicked on to be removed.
var bubbles:Array = [];
function makeBubbles()
{
for(var i:int=0;i<100;i++)
{
var bubble:Bubble = new Bubble();
bubbles.push(bubble);
addChild(bubble);
bubble.addEventListener(MouseEvent.CLICK, eventListener);
}
}
function eventListener(eventObject:MouseEvent) {
var clickedBubbleIndex:int = bubbles.indexOf(eventObject.currentTarget);
parent.removeChild(eventObject.currentTarget);
bubbles.splice(clickedBubbleIndex:int, 1);
}
try this
bubbles.addEventListener(MouseEvent.CLICK, eventListener); // place this listener in moveBubbles function.
function eventListener(eventObject:MouseEvent):void {
eventObject.currentTarget.visible = false;
}

Remove the clicked objects

I'm trying to remove the array objects that are being clicked and add them into another array to display them else where. I posted the current code.
I think the problem maybe with .currentTarget. I tried replacing the .currentTarget to .target but the function wasn't getting past this line : if (socket_Array[i] == in_event.target) (in this version its .currentTarget, I am just saying when I tried changing it to .target)
The error I get is this:
TypeError: Error #1034: Type Coercion failed: cannot convert []#2c2a8f11 to flash.display.DisplayObject.
Function that creates the objects:
function createSockets():void
{
var socket_one:socket = new socket ();
var socket_two: socketyellow = new socketyellow ();
var socket_three: socketdarkorange = new socketdarkorange ();
var socket_four: socketlightgreen= new socketlightgreen ();
var socket_five: socketpurple = new socketpurple ();
var socket_six: socketdarkgreen = new socketdarkgreen ();
socket_Array.push(socket_one, socket_two,socket_three, socket_four, socket_five, socket_six);
for (var i:int=0; i<socket_Array.length; i++)
{
addChild(socket_Array[i]);
socket_Array [i].x = socket_x_position;
socket_Array [i].y = socket_y_position;
socket_Array[i].addEventListener(MouseEvent.MOUSE_DOWN, removeItemOnClick);
}
temp_update ();
}
Function that is suppose to get rid of the object clicked and add it to an array.
function removeItemOnClick(in_event:MouseEvent):void
{
var i:int = 0;
for (i=0; i<socket_Array.length; i++)
{
if (socket_Array[i] == in_event.currentTarget)
{
trace ("it goes here");
var removed = socket_Array.splice(i, 1);
trace (removed);
trace (socket_Array );
var drop:Sprite = in_event.currentTarget as Sprite;
removeChild (drop);
removedItem[removedItem.length] = removed;
createremovedItem ();
trace (removedItem);
updateDisplay ();
choice_updateDisplay ();
}
}
}
var removedItem_position = 0
function createremovedItem () {
for (removedItem_position; removedItem_position<removedItem.length; removedItem_position++){
addChild (removedItem [removedItem_position]);
}
}
First of all, .currentTarget is correct.
Secondly, there's no point in calling removeChild() and then calling addChild(). The net effect of both calls is nothing.
Almost all of the code in the second function is unnecessary. Here's a shorter version:
function removeItemOnClick(in_event:MouseEvent):void {
var index:int = socket_Array.indexOf(in_event.currentTarget);
var drop:Sprite = socket_Array.splice(index,1) as Sprite;
removedItem.push(drop);
updateDisplay();
choice_updateDisplay();
}
If you want to display the new item elsewhere, just change drop.x and drop.y.
As the error suggests it looks like it is problem with type coercion.
Try to replace the condition of your if statement with this:
if (DisplayObject(socket_Array[i]) == DisplayObject(in_event.currentTarget))
In case this is not working your might have more information while debugging by storing the two objects you want to compare into temporary variables

Cannot access a property or method of a null object reference Flex

I've been trying to learn flex/flash programming and am working on a project where I need to populate a spinner list in flex dynamically from a string. I have a function that separates the string using "split" and now I need to populate an array list. I have been working with this stupid big for hours now and can;t find help anywhere. I keep getting the following error:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at views::CommonPlaces/initApp()[/Users/twing207/Documents/Adobe Flash Builder 4.6/AmericanTaxi/src/views/CommonPlaces.mxml:30]
My code is here:
<fx:Script>
<![CDATA[
import mx.collections.ArrayList;
import spark.events.IndexChangeEvent;
var Arr1:Array;
var Arr2:Array;
var Arr3:Array;
[Bindable]
public var CommonPlacesArray:ArrayList;
var CommonPlacesData:String = new String("2133664:American Taxi Dispatch, Inc:Mount Prospect:834 E RAND RD|2133665:Walmart:Mount Prospect:930 Mount Prospect Plaza|2228885:Garage:Des Plaines:1141 Lee St|2228886:Asian Island:Palatine:1202 E Dundee Rd|2229464:Kohl's:Arlington Heights:700-856 W Dundee Rd|");
var CurrentSelect:String = new String();
private function initApp():void {
Arr1 = CommonPlacesData.split("|");
var arrLength:Number = new Number(Arr1.length);
for (var i:Number = 0; i < (arrLength - 1); i++) {
CurrentSelect = new String(Arr1[i]);
Arr2 = CurrentSelect.split(":");
//THE LINE BELOW IS WHERE IT STOPS:
CommonPlacesArray.addItem(Arr2[1]);
}
}
It doesn't seem to like the "CommonPlacesArray.addItem" line. Any help or a point in the right direction would be great. Thanks in advanced!
On another note, I am also getting the error: "Access of undefined property: data" on the following:
Here in another view I set the value for data.UserCommonReturnData to a string.
function LoginLoaded (e:Event):void {
trace(e.target.data);
var ServerReturn:String;
ServerReturn = new String(e.target.data);
data.UserCommonReturnData = ServerReturn;
navigator.pushView(CommonPlaces, data);
}
and here I try to pull it back:
var CommonPlacesData:String = new String();
var CurrentSelect:String = new String();
//The next line gives the error:
CommonPlacesData = data.UserCommonReturnData;
Any idea??
You never construct CommonPlacesArray, you just declare it.
var CommonPlacesArray:ArrayList = new ArrayList();
If you check the Array List API you can also clearly see it has a constructor that accepts an array, meaning you can copy the data to it without having to iterate over it yourself.