Highlighted

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

Contributor ,
Jun 16, 2020

Copy link to clipboard

Copied

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

Views

73

Likes

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

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

Contributor ,
Jun 16, 2020

Copy link to clipboard

Copied

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

Views

74

Likes

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
Jun 16, 2020 0
Adobe Community Professional ,
Jun 17, 2020

Copy link to clipboard

Copied

because "this" is undefined in moveMe.

 

use:

 

moveMe.bind(this)();

 

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

Likes

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
Reply
Loading...
Jun 17, 2020 0
Contributor ,
Jun 17, 2020

Copy link to clipboard

Copied

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?

Likes

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
Reply
Loading...
Jun 17, 2020 0
kglad LATEST
Adobe Community Professional ,
Jun 17, 2020

Copy link to clipboard

Copied

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));

Likes

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
Reply
Loading...
Jun 17, 2020 0
Contributor ,
Jun 17, 2020

Copy link to clipboard

Copied

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);

Likes

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
Reply
Loading...
Jun 17, 2020 0