Wednesday, July 4, 2007

ReferenceError: Error #1056: Caused by Declaring Stage Instances Private

If you declare a stage instance private you get the message: "ReferenceError #1056 Cannot create property my_mc on StageIntanceDeclarationsClass"

This error occurs when you uncheck the "Declare Stage Instances Automatically" checkbox in the "ActionScript 3.0 Settings" dialogbox and proceed to declare stage instances as private variables in the class associated with the containing MovieClip.

A Note on Inheritances and Declaring Stage Instances:
You cannot choose to simply always declare stage instances automatically without forging the use of inheritance in classes linked to MovieClip Symbols. If you have a class APrime which is derived from class A and APrime is linked to a MovieClip Symbol, all stage instances used in the base class A must be manually declared in class A. "Declare Stage Instances Automatically" only declares instances in the class linked to the MovieClip Symbol and does NOT make those references available to any base classes.

Example:
Assume that the class StageIntanceDeclarations is set as the class associated with a MovieClip which c0ntains the MovieClip my_mc. Then the following code will cause ReferenceError #1056 at runtime.



package
{
import flash.display.MovieClip;
public class StageIntanceDeclarations extends MovieClip
{
//Private Causes ReferenceError #1056
private var my_mc:MovieClip;
function StageIntanceDeclarations()
{
}
}
}


The output is as follows:
ReferenceError: Error #1056: Cannot create property my_mc on StageIntanceDeclarations.
at flash.display::Sprite/flash.display:Sprite::constructChildren()
at flash.display::Sprite$iinit()
at flash.display::MovieClip$iinit()
at TestStageIntanceDeclarationsBase$iinit()
at flash.display::Sprite/flash.display:Sprite::constructChildren()
at flash.display::Sprite$iinit()
at flash.display::MovieClip$iinit()

To avoid this error simply declare my_mc as public:
public var my_mc:MovieClip;

13 Comments:

Blogger nui said...

much help thanks! :)

July 16, 2007 3:44 AM  
Anonymous tom said...

thanx andrew, you saved me some time.
but i'm still a bit confused.
if i declare my_mc private in the base class A it should be perfectly reachable to class primeA because it inherited it. why can't primeA then control that private instance?
thanx

July 28, 2007 1:08 AM  
Blogger Donald said...

Inheriting classes cannot access a private property in AS3.

They could do so in AS2, because only "public" & "private" were supported. But AS3 now supports "protected"

public = unrestricted access
private = class only access
protected = class & extending class access

August 1, 2007 8:42 AM  
Anonymous Anonymous said...

Thanks Andrew,
It was really a relief.
I was going crazy after this.
Thanks once again!

September 18, 2007 10:57 PM  
Blogger PaiasoLoco said...

Thanks Andrew, this is driving me crazy!.

It is a pain to have to declare the properties manually though, any ideas of how could this be done parametrically?

September 8, 2008 9:58 AM  
Anonymous Brian Sexton said...

If your "Automatically declare stage instances" option is off, you can also get code 1056 errors by referring to manually created instances that have not been declared at all let alone as private instances. For example:

this.aProperlyDeclaredChildMovieClip['anUndeclaredGrandChildMovieClip'].someMethod();

*BOOM* says the compiler. Well, "Error #1056" says the compiler, anyway.

October 1, 2008 11:25 PM  
Blogger antoniobrandao said...

it also happens if you have turned off the "visibility" of the layer in the Flash CS4 IDE. The layer and it's contents are not compiled, and this same error is displayed.

January 5, 2009 7:08 AM  
Anonymous Anonymous said...

Just a note -- this occurred for me when declaring the instance protected, as well.

January 16, 2009 9:29 AM  
Anonymous Anonymous said...

hi all-

my problem acts like this, but doesnt seem to be the same problem:

I have a swf that works great when loaded directly, but throws error#1056 when loaded into another swf...


thoughts? dying on the vine, here

January 25, 2009 11:33 PM  
Anonymous Anonymous said...

how could this be done parametrically?

Great riff, Paiaso loco.

Thanks, Andrew for this. I came across this problem as another who'd never seen it before and was prepared to spin my wheels over it for awhile. I found this post in a search pretty quickly and it saved the day.

I never even know the toggle was there. I'm going to use it 100% of the time from now on, because then I can get code hinting and such when writing to objects that are .fla instantiated.

February 15, 2009 10:34 AM  
Anonymous Anonymous said...

Hi:
Thanks for this post, it was really helpful.
I tried declaring the vars as protected (to avoid possible interferences) but it gives the same error as when declared private.

February 25, 2009 8:43 AM  
Anonymous Anonymous said...

Hey Andrew,
I have made a class A that is attached to a movie clip that contains another movie clip which has attached the class B.
I am getting the #1056 error fired from the class B because it can't find the property in class A. The reason is that somehow class B is compiled before class A, which is weird because class B belongs to a movie clip that resides into a movie clip of type A.
Is there any way that I can control the class compile order, or any reasonable solution to this problem?

Cheers,
Alex

March 27, 2009 2:36 AM  
Anonymous Matt said...

THANKS!!

July 1, 2009 6:58 AM  
Anonymous Michael said...

One horrible gotcha to this is that if you want to declare stage instance names for multiple similar clips, you then have to define variables for each of them.

This is troubling if you are doing rudimentary data association using ID's, which is a not-so-rare technique... (e.g; associating slides with a product ID or something)

August 7, 2009 8:02 AM  
Blogger crazyFlash said...

Thanks Andrew

August 13, 2009 12:05 AM  
Blogger Pivo said...

Thanks Andrew! I had no idea what I was doing wrong...

January 7, 2010 8:00 AM  
Blogger Fritz said...

Thanks so much...that saved me a lot of headaches...

January 18, 2010 3:40 AM  
Blogger Aditya Game Programmer said...

when you save a flash 8 file into cs3 or higher the declare stage instances setting in as3 settings is automatically unchecked.
its nice that your post popped on google on just the search of the error.
/no1
reminded me i had to manually update the as3 settings.

January 28, 2010 10:17 PM  

Post a Comment

Links to this post:

Create a Link

<< Home