• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Participant ,
Mar 10, 2022 Mar 10, 2022

Copy link to clipboard

Copied

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.

TOPICS
Code , Error , How to

Views

262

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Mar 10, 2022 Mar 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

Votes

Translate

Translate
Community Expert ,
Mar 10, 2022 Mar 10, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 10, 2022 Mar 10, 2022

Copy link to clipboard

Copied

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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 10, 2022 Mar 10, 2022

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 10, 2022 Mar 10, 2022

Copy link to clipboard

Copied

Bang on, holy cow... I look for turorials and examples when I want to try and solve something, and all I found was code that referenced the stage, which seemed reasonable to me. 

 

Do you know if there is a way to make the newly added MC reside on a particular layer or is a new MC always going to the top of the stack when placed this way? 

Thanks man, I really appreciate the guidance. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 10, 2022 Mar 10, 2022

Copy link to clipboard

Copied

LATEST

Awesome!

 

I'm glad it worked.

 

Actually there's no concept of layers at runtime. What you can do is to change the index of a instance. For this, you can use methods like addChildAt or setChildIndex.

 

Regards,

JC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines