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?
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
...
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
Copy link to clipboard
Copied
Thank you very much, I have learnt from you a lot 🙂
Copy link to clipboard
Copied
Awesome!
You're welcome!