PDA

View Full Version : FLASH: TypeError: Error #1010 when calling a class (Flash 9 - AS3)



Gerrit
May 9th, 2007, 06:37 AM
First of all, hello all, this is my 1st post here, so I'd like to take the opportunity to greet you all.

Now,
I got this class desat.menus.HorizontalMenu
I want to make an instance of this class in a fla-file, but I get the following runtime error:



TypeError: Error #1010: A term is undefined and has no properties.
at desat.menus::HorizontalMenu$iinit()
at fcms_fla::MainTimeline/fcms_fla::frame1()
The forum search nor google gave me a solution for this.

This is my code

FLA-file


import desat.menus.HorizontalMenu;
var arr:Array = new Array();
arr.push("home");
arr.push("news");
var $root:MovieClip = this;
var menu:HorizontalMenu = new HorizontalMenu($root, arr, 10, 10 ,120);
HorizontalMenu.as (inside /desat/menus/ )


//************************************************** ****************************************
// HorizontalMenu class for FCMS.
//
// @author: Gerrit Bertier
// @contact: gerrit@tensixtytwo.com
// http://desaturated.be
//
// @properties: parentMovie (MovieClip) = the parent movie for the menu item
// menuText (Array) = the array containing the text for the items
// xpos (Number) = the x-position for the 1st menu item
// ypos (Number) = the y-position for the 1st menu item
// itemWidth (Number) = the width of a menu item (default: 120)
//
//************************************************** ****************************************

package desat.menus {
// Imports
import flash.display.MovieClip;
import flash.text.TextFormat;
import flash.text.TextField;
//
public class HorizontalMenu {
// Properties
var _parentMovie:MovieClip;
var _menuText:Array = new Array();
var _xpos:Number = 0;
var _ypos:Number = 0;
var _itemWidth:Number = 120;
//
// Getters & setters
public function get parentMovie():MovieClip {
return this._parentMovie;
}
public function set parentMovie(newValue:MovieClip):void {
this._parentMovie = newValue;
}
public function get MenuText():Array {
return this._menuText;
}
public function set MenuText(newValue:Array):void {
this._menuText = newValue;
}
public function get xpos():Number {
return this._xpos;
}
public function set xpos(newValue:Number):void {
this._xpos = newValue;
}
public function get ypos():Number {
return this._ypos;
}
public function set ypos(newValue:Number):void {
this._ypos = newValue;
}
public function get itemWidth():Number {
return this._itemWidth;
}
public function set itemWidth(newValue:Number):void {
this._itemWidth = newValue;
}
// Methods
function HorizontalMenu(parentMovie:MovieClip, menuText:Array, xpos:Number, ypos:Number, itemWidth:Number = 120):void {
_parentMovie = parentMovie;
_menuText = menuText;
_xpos = xpos;
_ypos = ypos;
_itemWidth = itemWidth;
// Text formats for the menu items
var tfmt_normal:TextFormat = new TextFormat();
tfmt_normal.align = "center";
tfmt_normal.color = 0x333333;
tfmt_normal.font = "Trebuchet MS";
tfmt_normal.size = 11;
var tfmt_hover:TextFormat = new TextFormat();
tfmt_hover.align = "center";
tfmt_hover.color = 0x666666;
tfmt_hover.font = "Trebuchet MS";
tfmt_hover.size = 11;
// Creating a movie clip and the textfield withing that movieclip for each menu item
for (var i = 0; i < _menuText.length; i++) {
var my_mc:MovieClip = new MovieClip();
my_mc.name = "menuitem"+i+"_mc";
var menu_txt:TextField = new TextField();
menu_txt.name = "menuitem_txt";
my_mc.addChild(menu_txt);
_parentMovie.addChild(my_mc);
my_mc.menuitem_txt.mouseEnabled = false;
my_mc.menuitem_txt.setTextFormat(tfmt_normal);
my_mc.menuitem_txt.text = _menuText[i];
}
}
}
}
Any experiences or help with this would be greatly appreciated, as I'm really stuck here.

Edit: I narrowed down the problem and found it is somewhere inside the for-block.

Gerrit
May 9th, 2007, 07:29 AM
Ok, it seems to work now, the only thing I changed is the code in the for-loop.



for (var i = 0; i < _menuText.length; i++) {
var my_mc:MovieClip = new MovieClip();
my_mc.name = "menuitem"+i+"_mc";
var menu_txt:TextField = new TextField();
trace(menu_txt);
menu_txt.mouseEnabled = false;
menu_txt.setTextFormat(tfmt_normal);
menu_txt.text = _menuText[i];
menu_txt.x = _xpos + (i*_itemWidth);
menu_txt.y = _ypos;
my_mc.addChild(menu_txt);
_parentMovie.addChild(my_mc);
}
As you can see, I set the properties of the text_txt before I used the addChild method.
Could anyone confirm this solution, or is this just some weird behavior?

senocular
May 9th, 2007, 11:59 AM
the name property does not work the same as _name in ActionScript 2. In ActionScript 2, _name not only provided a string "name" for an object, but it also defined the variable in the timeline in which the object existed. In ActionScript 3, the name property is just a string property. Setting it defines no variable within the display object in which it exists. Your

menu_txt.name = "menuitem_txt";
does not define

my_mc.menuitem_txt
When referencing menu_txt, just use menu_txt.

menu_txt.mouseEnabled = false;
menu_txt.setTextFormat(tfmt_normal);
menu_txt.text = _menuText[i];

Gerrit
May 9th, 2007, 06:41 PM
Yea, figured that out to.

That wasn't the big issue though, the biggest issue was that I used the addChild before adding the properties, which just didn't work.

senocular
May 9th, 2007, 07:12 PM
the biggest issue was that I used the addChild before adding the properties, which just didn't work.

that shouldnt be a problem