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

Writing functions to Global Script for reuse

Community Beginner ,
Dec 27, 2020 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

260

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

correct answers 1 Correct answer

Community Expert , Dec 28, 2020 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
...

Votes

Translate

Translate
Community Expert ,
Dec 28, 2020 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

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 Beginner ,
Dec 28, 2020 Dec 28, 2020

Copy link to clipboard

Copied

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

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 ,
Dec 29, 2020 Dec 29, 2020

Copy link to clipboard

Copied

LATEST

Awesome!

 

You're welcome!

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