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

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

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

217

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

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

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

Copy link to clipboard

Copied

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

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

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