About Orland MediaDevelopmentTrainingBlogRSS FeedContact us
Microphone

getDefinitionByName() produces ReferenceError: Error #1065: Variable is not defined

Date: Thursday 3rd September 2009
Time: 16:44
Category: ActionScript 3.0
Views: 1,596
Comments: 5 Comments

Scenario

Something we often do here is get the “id” of a clicked menu button and instantiate a section of a site, depending on the ID passed. Usually all sections of a site inherit from a generic “Section” class, then have additional specific capabilities depending on the section. So, depending on the ID passed, we need to create an instance of the particular class. This means we need to get the class name dynamically from the string passed by the button.

Let’s imagine the “music” button was pressed. We’ll either have a listener for this or we’ll directly call a method called loadSection(), passing the ID (the name of the button usually). We name instances in camel notation, so that’ll be “music”.

This string goes over to the loadSection() method and in here we use a custom StringUtils class to covert the first letter to upper case, getting us the required class name as a string. So this gives us “Music”. A string is no good on its own though – we need to get the class reference for this, as follows:

// required "dummy" ref to ensure class is compiled...
var dummyRef:Music;
...
// get class reference
var classRef = getDefinitionByName(id);
// instantiate class instance as current section
section = new classRef();


Problem

All well and good. The trouble is sometimes it just doesn’t work and you’ll get:

ReferenceError: Error #1065: Variable Music is not defined

There’s a thread here where the person was getting this, and it drifted off into another debate without being solved. The import statement is there. The dummy class reference is there, but still it doesn’t work.


Solution

What is not immediately clear is that you need to provide a fully qualified class path to getDefinitionByName() – even though you have set up an import.

Amend as follows (for example) and your problem is solved:

// var classRef = getDefinitionByName(id);
var classRef = getDefinitionByName("sections." + id);

“sections.” here is a reference to our package structure.

We hope this helps you.

Rate this post
1 star2 stars3 stars4 stars5 stars   (1 votes, average: 5.00 out of 5)

AS3 training course

Date: Wednesday 29th July 2009
Time: 11:11
Category: ActionScript 3.0
Views: 174
Comments: 1 Comment

Due to popular demand, we are scheduling another ActionScript training course for 26-28th August 2009, in central London.

If you would like to reserve a place please get in touch.

Rate this post
1 star2 stars3 stars4 stars5 stars   (Roll over and click)

localToGlobal() in AS3 not working

Date: Wednesday 13th May 2009
Time: 17:06
Category: ActionScript 3.0
Views: 3,428
Comments: 15 Comments

localToGlobal() not working? The LiveDocs on this potentially very useful method, along with its partner globalToLocal() are not actually very helpful.

What they don’t make clear is that you need to be sure to overwrite your point when using it:

// WRONG:
var pt:Point = new Point(target.x, target.y);
target.parent.localToGlobal(pt);
parent.globalToLocal(pt);

// RIGHT:
var pt:Point = new Point(target.x, target.y);
pt = target.parent.localToGlobal(pt);
pt = parent.globalToLocal(pt);

Simply running a localToGlobal() method on a point in a given scope is not enough. You need to write the result back into the point. You might be forgiven for not seeing this, because it was not necessary for the equivalent method in AS2!

localToGlobal() and globalToLocal() can be the source of considerable frustration, especially as this differences such as this are not mentioned by Adobe. But properly understood these methods are not complicated. If this post helped you please let us know. :)

Rate this post
1 star2 stars3 stars4 stars5 stars   (16 votes, average: 4.25 out of 5)

AS3 course in July

Date: Thursday 7th May 2009
Time: 16:59
Category: ActionScript 3.0
Views: 572
Comments: No comments

Our next Applied ActionScript 3.0 course will more than likely be scheduled for early July, 2009.

The course is limited to only 5 delegates. If you would like to reserve a place please let us know – we’d be happy to have you there.

Rate this post
1 star2 stars3 stars4 stars5 stars   (Roll over and click)

Error #1007: Instantiation attempted on a non-constructor with APE

Date: Friday 1st May 2009
Time: 11:40
Category: ActionScript 3.0
Views: 4,364
Comments: No comments

We’re using APE – the ActionScript Physics Engine – for our current project.

If you run into the TypeError:

Error #1007: Instantiation attempted on a non-constructor

..when compiling with Flash CS4, the reason is that APE defines a class called Vector, which clashes with a new class, of a different kind, in Flash 10, also called Vector.

The solution is to either avoid publishing for Flash 10, if you don’t need it, or replace all references to Vector throughout the APE code with the fully qualified reference org.cove.ape.Vector.

Rate this post
1 star2 stars3 stars4 stars5 stars   (Roll over and click)
Older posts
Lenovo Microsoft Adobe Heart Internet Envirowise
Copyright © 2008 Orland Media Ltd | All rights reserved Company details Ethical policy Terms & conditions Privacy policy