Skip to main content
Jeffery.Wright
Inspiring
March 10, 2022
Answered

HTML5 Canvas: Load MC from Library - Assign Instance Name?

  • March 10, 2022
  • 1 reply
  • 628 views

This code does load either object from the library, but fails to assign the loaded object the specified Instance Name:

function ChinaPD(e)
{   
    window.PDisplay = stage.addChild(new lib.PDisp_CH());
	this.PDisplay.x = 1629.15;
	this.PDisplay.y = 96.65;
}

function NotChina(e)
{   
    window.PDisplay = stage.addChild(new lib.PDisp());
	this.PDisplay.x = 1629.15;
	this.PDisplay.y = 96.65;
}

 When referred to after either object is loaded (it's always one or the other, never both), the MC does not respond to commands, only producing the error: 

 

  • Uncaught TypeError: Cannot read properties of undefined (reading 'gotoAndStop')
  • exportRoot.PDisplay.gotoAndStop("CO2");  is the culprit...

 

Shouldn't this work? Any insight as to why it doesn't? 

 

Thanks.

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

 

This code worked for me:

function ChinaPD(e)
{   
    exportRoot.PDisplay = exportRoot.addChild(new lib.PDisp_CH());
	exportRoot.PDisplay.x = 300;
	exportRoot.PDisplay.y = 200;
}

ChinaPD();
exportRoot.PDisplay.gotoAndStop("CO2");

 

You're probably facing a context problem.

 

Please let us know.

 

Regards,

JC

1 reply

JoãoCésar17023019
Community Expert
Community Expert
March 10, 2022

Hi.

 

It's because you're storing the newly created instance in a property of the window object (window.PDisplay), but you're trying to access the created instance in the root object (this.PDisplay and exportRoot.PDisplay).

 

Assuming that the context of your functions is the main timeline, you could replace the window object by the root object:

this.PDisplay = stage.addChild(new lib.PDisp_CH());

 

Please let us know.

 

Regards,

JC

Jeffery.Wright
Inspiring
March 10, 2022

Thanks man, I did try that, I get the same result... it's baffling. This code:

function ChinaPD(e)
{   
    this.PDisplay = stage.addChild(new lib.PDisp_CH());
	this.PDisplay.x = 1629.15;
	this.PDisplay.y = 96.65;
}

function NotChina(e)
{   
    this.PDisplay = stage.addChild(new lib.PDisp());
	this.PDisplay.x = 1629.15;
	this.PDisplay.y = 96.65;
}

Produces the same error:

Uncaught TypeError: Cannot read properties of undefined (reading 'gotoAndStop')

exportRoot.PDisplay.gotoAndStop("CO2"); - which is fired from within a MC

 

There must be some way to place an MC with an instance name from the library, onto the stage that will react to commands as any normal object would, right? 

 

Thanks, again.

 

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
March 10, 2022

Hi.

 

This code worked for me:

function ChinaPD(e)
{   
    exportRoot.PDisplay = exportRoot.addChild(new lib.PDisp_CH());
	exportRoot.PDisplay.x = 300;
	exportRoot.PDisplay.y = 200;
}

ChinaPD();
exportRoot.PDisplay.gotoAndStop("CO2");

 

You're probably facing a context problem.

 

Please let us know.

 

Regards,

JC