Skip to main content
Inspiring
September 19, 2019
Question

ANIMATE CC: Adding a property through a forEach loop to various objects of THIS canvas

  • September 19, 2019
  • 2 replies
  • 394 views

Hi,

The aim is to make a figure, consisting of several parts, semi-transparent when it is clicked. While this works fine calling every single element of the figure individually, it would be nice to loop this call with an array. But doing so produces the error that "_this.element is undefined. What this the problem with the code?

 

_this.mc_Body.addEventListener('click', fn_AgentGlow.bind(_this));

//The short version (not working)
function fn_AgentGlow(){
	var agent = ["mc_LegLeft", "mc_LegRight", "mc_Torso", "mc_Scalp", "mc_ArmLeft", "mc_HandLeft", "mc_ArmRight", "mc_Handright"];
	agent.forEach( element => { _this.element.alpha = 0.5;
	});
}

//The long version (working)
function fn_AgentGlow(){
	_this.mc_Torso.alpha = 0.5;
	_this.mc_Skull.alpha = 0.5;
	_this.mc_LegLeft.alpha = 0.5;
	_this.mc_LegRight.alpha = 0.5;
	_this.mc_ArmLeft.alpha = 0.5;
	_this.mc_HandLeft.alpa = 0.5;
	_this.mc_ArmRight.alpha = 0.5;
	_this.mc_HandRight.alpha = 0.5;
}

 

As always, thank you for any help.

___________________________________________________________________________________________________

NOTE TO MODERATOR: Please move this post to Adobe Animate. Apologies

    This topic has been closed for replies.

    2 replies

    dcpustAuthor
    Inspiring
    September 19, 2019

    I found a workaround, though it is still not as sleek a code as I hoped it could be. But at least there is a bit of progress when storing the list of objects as complete variables in an array.

    _this.mc_Body.addEventListener('click', fn_AgentGlow.bind(_this));
    
    function fn_AgentGlow(){
    	var agent = [_this.mc_LegLeft, _this.mc_LegRight, _this.mc_Torso, _this.mc_Scalp, _this.mc_ArmLeft, _this.mc_HandLeft, _this.mc_ArmRight, _this.mc_Handright];
    	agent.forEach( element => { 
    		_this.element.alpha = 0.5;
    	});
    }

    Please come forward if you can come up with a better solution.

    John T Smith
    Community Expert
    Community Expert
    September 19, 2019

    Please post the name of the program you use so a Moderator may move this message to that forum
    -A program would be Photoshop or Dreamweaver or Muse or Premiere Pro or ???