Writing functions to Global Script for reuse

Community Beginner ,
Dec 27, 2020

Copy link to clipboard

Copied

Hello,
I have many movieclip instances on stage and I would like to apply same "on click" function/code to every one of them.

_this.instHorse.on('click', function () {
	_this.gotoAndStop('CatFrame');
         //and many more lines of codes
});


The movieclip instances are put on different frames of the main timeline so what I've tried was,
rather than writing above on click event codes for every single one of the moviclip instances on different frames, create a global function named onAnimalClick on the Action panel > Global > Script >

and apply this onAnimalClick  global function to each of the movieclip instances on stage.

 

I have created a function on Global>Script shown below:

function onAnimalClick() {
    var _this = this;	
    _this.instAnimal.instPaw.gotoAndStop('Swing');
    // and many more lines of codes
}

 

and tried to apply the above function to each movieclip instance on stage:

_this.instHorse.on('click', function () {
	onAnimalClick();
});

 

The above code indeed called the " onAnimalClick() " function written on the Global>Script  
However I am getting    undefined   javascript error "cannot read property....   of undefined" probably due to the fact that I have used " _this " in the global function.
What is the correct way of referring to the movie clip instances on stages in the global function? 
Is writing a function at Global>Script   and calling the function the best thing to do in this situation?

Views

66

Likes

Translate

Translate

Report

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

1 Correct Answer

Adobe Community Professional , Dec 28, 2020
JoãoCésar Adobe Community Professional , Dec 28, 2020
Hi.   The this keyword won't reference to the main timeline because it is inside a function and because it's in the Global Script. Use the automatically generated global var exportRoot that stores a reference to the main timeline or send the context as an argument to your onAnimalClick function. Like this:   Option 1: // global script function onAnimalClick() { exportRoot.instAnimal.instPaw.gotoAndStop('Swing'); } // ... // main timeline var _this = this; _this.instHorse.on('click', funct...

Likes

Translate

Translate
Jump to answer Jump to answer
Adobe Community Professional ,
Dec 28, 2020

Copy link to clipboard

Copied

Hi.

 

The this keyword won't reference to the main timeline because it is inside a function and because it's in the Global Script. Use the automatically generated global var exportRoot that stores a reference to the main timeline or send the context as an argument to your onAnimalClick function. Like this:

 

Option 1:

// global script
function onAnimalClick()
{
    exportRoot.instAnimal.instPaw.gotoAndStop('Swing');
}

// ...

// main timeline
var _this = this;

_this.instHorse.on('click', function ()
{
	onAnimalClick();
});

 

Option 2:

// global script
function onAnimalClick(root)
{
    root.instAnimal.instPaw.gotoAndStop('Swing');
}

// ...

// main timeline
var _this = this;

_this.instHorse.on('click', function ()
{
	onAnimalClick(_this);
});

 

I hope it helps.

 

Regards,

JC

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
ZKM128 AUTHOR
Community Beginner ,
Dec 28, 2020

Copy link to clipboard

Copied

Thank you very much, I have learnt from you a lot 🙂

Likes

Translate

Translate

Report

Report
Reply
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Adobe Community Professional ,
Dec 29, 2020

Copy link to clipboard

Copied

Awesome!

 

You're welcome!

Likes

Translate

Translate

Report

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