About Orland MediaDevelopmentTrainingBlogRSS FeedContact us
Microphone

Why use init methods in AS3

Date: Tuesday 29th September 2009
Time: 23:43
Category: ActionScript 3.0
Views: 970
Comments: No comments

Checking our stats, we just saw that some people are searching for “why use init methods as3″. There are several reasons to consider using init methods in AS3, rather than have all of your initialisation code in your constructor functions:

  • Constructor functions run slower than “regular” methods. (See here for details.)
  • You might want to re-run initialisation code without recreating the instance.
  • You might want to run delayed initialisation code, for example once an object has been added to the Display List. (See our post here regarding this issue.)
Rate this post
1 star2 stars3 stars4 stars5 stars   (1 votes, average: 4.00 out of 5)

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

Date: Thursday 3rd September 2009
Time: 16:44
Category: ActionScript 3.0
Views: 1,623
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)
Lenovo Microsoft Adobe Heart Internet Envirowise
Copyright © 2008 Orland Media Ltd | All rights reserved Company details Ethical policy Terms & conditions Privacy policy