Skip to main content
Known Participant
December 28, 2020
Answered

Writing functions to Global Script for reuse

  • December 28, 2020
  • 2 replies
  • 463 views

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?

    This topic has been closed for replies.
    Correct answer JoãoCésar17023019

    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

    2 replies

    ZKM128Author
    Known Participant
    December 29, 2020

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

    JoãoCésar17023019
    Community Expert
    Community Expert
    December 29, 2020

    Awesome!

     

    You're welcome!

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    December 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', 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