Skip to main content
Inspiring
March 22, 2022
Answered

Canvas project - Target a movieclip from a nested function problem

  • March 22, 2022
  • 1 reply
  • 442 views

I cant seem to target a movieclip from a nested function it works fine from a function "ArgentinaBtn" that's called onClick... but cant target it if I place a function "defaultFade"within the function"ArgentinaBtn". Please anyhelp 🙂

function defaultFade() {
	//alert("defaultFade worked!")
	this.page_map.interactive_map.Argentina.alpha = 0.5;
	this.page_map.interactive_map.Australia.alpha = 0.5;
	this.page_map.interactive_map.Austria.alpha = 0.5;
}

this.page_map.interactive_map.Argentina.addEventListener("click", ArgentinaBtn.bind(this));

function ArgentinaBtn()
{
	//this.page_map.interactive_map.Argentina.alpha = 0.5;
	//this.page_map.interactive_map.Australia.alpha = 0.5;
	//this.page_map.interactive_map.Austria.alpha = 0.5;
	defaultFade();
	
	//overwrite
	this.page_map.interactive_map.Argentina.alpha = 1;
}

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

    Hi.

     

    The context of the defaultFade function is not the main timeline but rather the window object. You need to bind the defautFade function to the main timeline.

     

    One way of doing this in your code is to replace the call:

    defaultFade();

    By:

    defaultFade.call(this);

     

    But I think it's easier in most cases to store a reference to the main timeline in a variable. Like this:

    var root = this;
    
    function defaultFade()
    {
    	root.page_map.interactive_map.Argentina.alpha = 0.5;
    	root.page_map.interactive_map.Australia.alpha = 0.5;
    	root.page_map.interactive_map.Austria.alpha = 0.5;
    }
    
    function ArgentinaBtn()
    {
    	defaultFade();
    	root.page_map.interactive_map.Argentina.alpha = 1;
    }
    
    root.page_map.interactive_map.Argentina.addEventListener("click", ArgentinaBtn);

     

    I hope it helps.

     

    Regards,

    JC

     

    1 reply

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    March 22, 2022

    Hi.

     

    The context of the defaultFade function is not the main timeline but rather the window object. You need to bind the defautFade function to the main timeline.

     

    One way of doing this in your code is to replace the call:

    defaultFade();

    By:

    defaultFade.call(this);

     

    But I think it's easier in most cases to store a reference to the main timeline in a variable. Like this:

    var root = this;
    
    function defaultFade()
    {
    	root.page_map.interactive_map.Argentina.alpha = 0.5;
    	root.page_map.interactive_map.Australia.alpha = 0.5;
    	root.page_map.interactive_map.Austria.alpha = 0.5;
    }
    
    function ArgentinaBtn()
    {
    	defaultFade();
    	root.page_map.interactive_map.Argentina.alpha = 1;
    }
    
    root.page_map.interactive_map.Argentina.addEventListener("click", ArgentinaBtn);

     

    I hope it helps.

     

    Regards,

    JC

     

    Inspiring
    March 22, 2022

    thanks that perfect... i thought i had tried that.

    JoãoCésar17023019
    Community Expert
    Community Expert
    March 22, 2022

    You're welcome.