Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

HTML5 Canvas - Detect button state

Engaged ,
Mar 16, 2022 Mar 16, 2022

I thought this would be simple.   

 

I just want to have a toggle button, so that when clicked, its state stays in the downState, and when clicked again, it goes to the upState.  For that to work I would need to know what state the button is in.

 

This works to the point the button stays in the downStte if clicked, but it's not working for toggling back to the upstate:

 

this.retinoscopeButton.addEventListener("click", retinoscope.bind(this));

function retinoscope(){
	if (retinoscopeButton.upState == true){
	retinoscopeButton.upState=retinoscopeButton.downState;
	} else if (retinoscopeButton.downState == true){
	retinoscopeButton.downState=retinoscopeButton.upState;
	}
}

 

322
Translate
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

correct answers 1 Correct answer

Engaged , Mar 16, 2022 Mar 16, 2022

This works for me...

 

var toggle;

if (!root.retinoscopeButton.hasEventListener("click")) {
	toggle = false;
	root.retinoscopeButton.addEventListener("click", retinoscopeButtonClickHandler.bind(this));
}


root.retinoscopeButton.addEventListener("mouseover", retinoscopeButtonRollOverHandler.bind(this));
root.retinoscopeButton.addEventListener("mouseout", retinoscopeButtonRollOutHandler.bind(this));


function retinoscopeButtonClickHandler() {
	if (toggle == false) {
		root.retinoscopeButton.gotoA
...
Translate
Engaged ,
Mar 16, 2022 Mar 16, 2022

and I have tried these:

 

this.retinoscopeButton.addEventListener("click", retinoscope.bind(this));

/*function retinoscope(evt) {
	var retinoscopeButtonState = evt.currentTarget.state = !evt.currentTarget.state;
	if (retinoscopeButtonState) {
		this.retinoscopeButton.upState = this.retinoscopeButton.downState;
	} else {
		this.retinoscopeButton.downState = this.retinoscopeButton.upState;
	}
}*/

var retinoscopeButtonState = 'up';

function retinoscope(evt){ 
	if (retinoscopeButtonState == 'up'){
	root.retinoscopeButton.upState = root.retinoscopeButton.downState;
		retinoscopeButtonState = 'down';
	} else if (retinoscopeButtonState == 'down'){
	root.retinoscopeButton.downState = root.retinoscopeButton.upState;
		retinoscopeButtonState = 'up';
	}
}
Translate
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
Engaged ,
Mar 16, 2022 Mar 16, 2022

also the following which works to turn 'on' the button, but not to turn 'off'.  This is using the button as a MovieClip...

 

if(!root.retinoscopeButton.hasEventListener("click")){
this.toggle = false;
root.retinoscopeButton.addEventListener("click", retinoscopeButtonClickHandler.bind(this));
}

//root.retinoscopeButton.addEventListener("click", retinoscopeButtonClickHandler.bind(this));

//root.retinoscopeButton.addEventListener("mouseover", retinoscopeButtonRollOverHandler.bind(this));

//root.retinoscopeButton.addEventListener("mouseout", retinoscopeButtonRollOutHandler.bind(this));


function retinoscopeButtonClickHandler() {
	if(this.toggle == false){
		root.retinoscopeButton.gotoAndStop(2);
	} else if(this.toggle == true){
		root.retinoscopeButton.gotoAndStop(0);
	}
}

 

Translate
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
Engaged ,
Mar 16, 2022 Mar 16, 2022
LATEST

This works for me...

 

var toggle;

if (!root.retinoscopeButton.hasEventListener("click")) {
	toggle = false;
	root.retinoscopeButton.addEventListener("click", retinoscopeButtonClickHandler.bind(this));
}


root.retinoscopeButton.addEventListener("mouseover", retinoscopeButtonRollOverHandler.bind(this));
root.retinoscopeButton.addEventListener("mouseout", retinoscopeButtonRollOutHandler.bind(this));


function retinoscopeButtonClickHandler() {
	if (toggle == false) {
		root.retinoscopeButton.gotoAndStop(2);
		toggle = true;
	} else if (toggle == true) {
		root.retinoscopeButton.gotoAndStop(0);
		toggle = false
	}
}

function retinoscopeButtonRollOverHandler() {
	if (toggle == false) {
		root.retinoscopeButton.gotoAndStop(1);
	}
}

function retinoscopeButtonRollOutHandler() {
	if (toggle == false) {
		root.retinoscopeButton.gotoAndStop(0);
	}
}
Translate
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