Skip to main content
Participant
April 19, 2021
Question

フレームが変わると変数が反映されません

  • April 19, 2021
  • 1 reply
  • 252 views

例えば、1フレームに

var score = 0;

と変数を設定します。

2フレームにボタンを作成し、

this.btn_seikai_1.addEventListener("click", btn_seikai_1.bind(this));

function btn_seikai_1()
{
score = score+10;
this.score_txt.text = score;
}

として、ボタンをクリックしたら、10プラスされるようにしましたが、うまくいきません。

animateでは、変数はフレームが変わると反映されないのでしょうか。

教えていただけるとありがたいです。

 

    This topic has been closed for replies.

    1 reply

    kglad
    Community Expert
    Community Expert
    April 19, 2021

    in thml5/canvas, variables prefixed by "var" are local to their frame.  if you want the variable to persist after it's defined, use:

     

    this.score = 0;

     

    if you do not want that variable reset when re-entering that frame, use:

     

    if(!this.alreadyExecuted){

    this.alreadyExecuted = true;

    this.score = 0;

    }

    勲15A7Author
    Participant
    April 20, 2021

    丁寧に教えていただき、

    ありがとうございました。

    うまくいきました。

    kglad
    Community Expert
    Community Expert
    April 20, 2021

    you're welcome.