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

Movieclip in canvas throws error when I try to target it.

Contributor ,
Jun 16, 2020 Jun 16, 2020

I have a movieclip that I made on the timeline (a blue square) which I have given the instance name of "blueSquare".

 

The following code works:

this.blueSquare.x = 800;

 

But when I try to put that in a function, it throws an error saying blueSquare is undefined:

function moveMe() {
	this.blueSquare.x = 800;
}
moveMe();

This also is undefined:

function moveMe() {
	blueSquare.x = 800;
}
moveMe();

I've tried calling my square 'stage.blueSquare', 'stage.canvas.blueSquare', and others.

 

Any idea why this doesn't work? 

 

TOPICS
Code
474
Translate
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 ,
Jun 17, 2020 Jun 17, 2020

because "this" is undefined in moveMe.

 

use:

 

moveMe.bind(this)();

 

if you want "this" to be defined in moveMe.

Translate
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
Contributor ,
Jun 17, 2020 Jun 17, 2020

Thank you for the help!

 

Perhaps you can help me with my resize issue. I want to be able to move a movieclip around when the window is resized. I have this:

function resizeMe(evt) {
	blueSquare.x = 800; //throws error because blueSquare is undefined
	this.blueSquare.x = 800; //throws error because blueSquare is undefined
}
window.addEventListener("resize", resizeMe);

I think I have scope issues, but I don't know how to fix them. If I create an element with code and do stage.addChild(element) I can target that element fine. However, elements that are already on the stage in the IDE (like my blueSquare) cannot be targeted. They are undefined. Do you know why that is?

Translate
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 ,
Jun 17, 2020 Jun 17, 2020
LATEST

and for others this is the preferred solution:

 

function resizeMe(evt) {
	//blueSquare.x = 800; //throws error because blueSquare is undefined
	this.blueSquare.x = 800; //throws error because blueSquare is undefined
}
window.addEventListener("resize", resizeMe.bind(this));
Translate
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
Contributor ,
Jun 17, 2020 Jun 17, 2020

I think I found it! 

https://community.adobe.com/t5/animate/problem-dynamically-adding-from-library/td-p/8914885?page=1

 

Stuff on the IDE timeline is not 'stage'. It is 'exportRoot'. Man is that confusing.

 

In other words, (for anyone else) the following works:

function resizeMe(evt) {
	exportRoot.blueSquare.x = 800; //where 'blueSquare' is the instance name of a movieclip on the main timeline
}
window.addEventListener("resize", resizeMe);
Translate
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