Beenden
  • Globale Community
    • Sprache:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티

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

Beitragender ,
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? 

 

THEMEN
Code
471
Übersetzen
Melden
Community-Richtlinien
Seien Sie freundlich und respektvoll, geben Sie die ursprüngliche Quelle der Inhalte an und suchen Sie vor dem Absenden Ihres Beitrags nach Duplikaten. Weitere Informationen
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.

Übersetzen
Melden
Community-Richtlinien
Seien Sie freundlich und respektvoll, geben Sie die ursprüngliche Quelle der Inhalte an und suchen Sie vor dem Absenden Ihres Beitrags nach Duplikaten. Weitere Informationen
community guidelines
Beitragender ,
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?

Übersetzen
Melden
Community-Richtlinien
Seien Sie freundlich und respektvoll, geben Sie die ursprüngliche Quelle der Inhalte an und suchen Sie vor dem Absenden Ihres Beitrags nach Duplikaten. Weitere Informationen
community guidelines
Community Expert ,
Jun 17, 2020 Jun 17, 2020
AKTUELL

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));
Übersetzen
Melden
Community-Richtlinien
Seien Sie freundlich und respektvoll, geben Sie die ursprüngliche Quelle der Inhalte an und suchen Sie vor dem Absenden Ihres Beitrags nach Duplikaten. Weitere Informationen
community guidelines
Beitragender ,
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);
Übersetzen
Melden
Community-Richtlinien
Seien Sie freundlich und respektvoll, geben Sie die ursprüngliche Quelle der Inhalte an und suchen Sie vor dem Absenden Ihres Beitrags nach Duplikaten. Weitere Informationen
community guidelines