Skip to main content
Inspiring
March 17, 2022
Answered

HTML5 Canvas - Detect button state

  • March 17, 2022
  • 1 reply
  • 370 views

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;
	}
}

 

    This topic has been closed for replies.
    Correct answer FlatChat

    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);
    	}
    }

    1 reply

    FlatChatAuthor
    Inspiring
    March 17, 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';
    	}
    }
    FlatChatAuthor
    Inspiring
    March 17, 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);
    	}
    }

     

    FlatChatAuthorCorrect answer
    Inspiring
    March 17, 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.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);
    	}
    }