Skip to main content
Known Participant
2월 24, 2022 8:37 오후 UTC
해결됨

My Number Counter Doesn't Work In Animate CC

Hi,

 

I've written javascript to cause a number to count up or down when the plus button or minus button is pressed. The problem I'm having is that after I press the plus button and, the numbers start incrementing, if I then press the down button the number increments one more before it starts decrementing. 

 

So if I count up to five, then press the minus button, the number goes to six before starting down again.

 

I couldn't get the fla to attach so I've attached a couple of screen caps to show my work.

  

 

Thanks

    이 주제는 답변이 닫혔습니다.
    JoãoCésar17023019
    Community Expert
    2월 24, 2022 9:15 오후 UTC

    Hi.

     

    If used postfix, with operator after operand (for example, x++), the increment operator increments and returns the value before incrementing.

     

    If used prefix, with operator before operand (for example, ++x), the increment operator increments and returns the value after incrementing.

     

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment

     

    So try this:

    var y = 0;
    
    function fl_MouseClickHandler_2()
    {
    	this.NumCounter.text = ++y;
    }
    
    function fl_MouseClickHandler_3()
    {
    	this.NumCounter.text = --y;
    }
    
    this.NumCounter.text = y;
    this.Btn1.addEventListener("click", fl_MouseClickHandler_2.bind(this));
    this.Btn2.addEventListener("click", fl_MouseClickHandler_3.bind(this));

     

    I hope it helps.

     

    Regards,

    JC

    3 답변

    JoãoCésar17023019
    Community Expert
    Community Expert
    2월 24, 2022 9:15 오후 UTC

    Hi.

     

    If used postfix, with operator after operand (for example, x++), the increment operator increments and returns the value before incrementing.

     

    If used prefix, with operator before operand (for example, ++x), the increment operator increments and returns the value after incrementing.

     

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment

     

    So try this:

    var y = 0;
    
    function fl_MouseClickHandler_2()
    {
    	this.NumCounter.text = ++y;
    }
    
    function fl_MouseClickHandler_3()
    {
    	this.NumCounter.text = --y;
    }
    
    this.NumCounter.text = y;
    this.Btn1.addEventListener("click", fl_MouseClickHandler_2.bind(this));
    this.Btn2.addEventListener("click", fl_MouseClickHandler_3.bind(this));

     

    I hope it helps.

     

    Regards,

    JC

    번역
    mlb172작성자
    Known Participant
    2월 24, 2022 9:51 오후 UTC

    Absolutely spot on. Thanks for your help.

    번역
    JoãoCésar17023019
    Community Expert
    Community Expert
    2월 24, 2022 10:20 오후 UTC

    You're welcome!

    번역