Beginner function help

Community Beginner ,
Nov 07, 2020 Nov 07, 2020

Copy link to clipboard

Copied

What is wrong with this code? (HTML5 canvas) The first way works, but the second way doesn't. 

var _this = this;
stage.enableMouseOver(3);
_this.icon_1.on('mouseover', function(){
	_this.icon_1.gotoAndPlay('over');
	_this.icon_1.cursor = "pointer"; 
});

_this.icon_1.on('mouseout', function(){
	_this.icon_1.gotoAndPlay('out');
});

I need to do this for over 10 icons so I wanted to make it more module so I tried to convert it like this but it does nothing - 

var _this = this;
stage.enableMouseOver(3);
_this.icon_1.addEventListener("mouseover", fl_MouseOverHandler);

function fl_MouseOverHandler()
{
    gotoAndPlay('over');
	cursor = "pointer"; 
}

_this.icon_1.addEventListener("mouseout", fl_MouseOutHandler);

function fl_MouseOutHandler()
{
    gotoAndPlay('out');
}

 

TOPICS
Code

Views

115

Likes

translate

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 ,
Nov 08, 2020 Nov 08, 2020

Copy link to clipboard

Copied

This is my final code, works like a charm.

 

var frequency = 3;
stage.enableMouseOver(frequency);


var c = this;
var arrChildren = c.children;
for (var i = 0; i < arrChildren.length; i++) {

	var mc_name = String(arrChildren[i].name);
	var ourSubstring = "icon_";

	if (mc_name.includes(ourSubstring)) {
		this[mc_name].addEventListener("mouseover", overF.bind(this));
		this[mc_name].addEventListener("mouseout", outF.bind(this));
		this[mc_name].icon_text.text = String(arrChildren[i].name).replace('icon_', '');
	} 
}
function overF(e) {
	e.currentTarget.gotoAndPlay("over");
	this.cursor = "pointer";
}
function outF(e) {
	e.currentTarget.gotoAndPlay("out");
	this.cursor = "default";
}

Likes

translate

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
Adobe Community Professional ,
Nov 08, 2020 Nov 08, 2020

Copy link to clipboard

Copied

no matter what you add/change, your for-loop params have to match what exists.

 

 

for (var counter = startNum; counter <= endNum; counter++) {
	this["icon_" + counter].addEventListener("mouseover", overF.bind(this));
	this["icon_" + counter].addEventListener("mouseout", outF.bind(this));
this["icon_"+counter].icon_text.text = counter.toString();
}

 

 

Likes

translate

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