Skip to main content
Participating Frequently
January 8, 2024
Answered

ボタンアクションのif elseについて教えてください

  • January 8, 2024
  • 2 replies
  • 1238 views

再生ボタン「btn_play」の下に一時停止ボタン「btn_pause」を重ねた状態で

再生と一時停止を切り替えるアクションを以下のようにしたいと考えています。

 

1.「btn_play」をクリックすると指定したラベル位置から再生して再生ボタンは非表示

2.「btn_pause」をクリックすると一時停止して再生ボタンを表示

3.「btn_play」をクリックして2の停止位置から再生

以降、2と3を繰り返したいです。

 

1のアクションを作成してみましたが、2と3のアクションについてどのように記述すれば良いのかが分かりません。

動作として1~3のアクションの作成は可能なのでしょうか?ご教示いただけますと幸いです。

----------------------------------------------------

var _this = this;

var playing = true;

 

_this.btn_play.on('click', function(){

              if (playing) {

                            _this.gotoAndPlay('Label名');

                            _this.btn_play.visible = false;

                            playing = false;

              } else {

                            _this.stop();

                            _this.btn_play.visible = true;

                            playing = true;

              }

});

----------------------------------------------------

This topic has been closed for replies.
Correct answer momohanna

 

ご回答ありがとうございます。

現在、以下のように配置しているのですが、上記でご回答いただいた例は4フレーム目に追加すれば良いのでしょうか?

追加したり置き換えたりしてみたのですが動作しなくなってしまいます…。

アニメーション1, 2とも_this.gotoAndPlayを設定すると停止位置から再生できず、_this.playに変更しております。

--------------------------------------------------------------------------------

アニメーション1 (4フレーム目にアクションとボタン1を配置、ボタン2はイラストを配置)

var _this = this;

var playing = true;

 

_this.btn_play1.on('click', function(){

              if (playing) {

                            _this.play();

                            _this.btn_play1.visible = false;

                            playing = false;

              } else {

                            _this.stop();

                            _this.btn_play1.visible = true;

                            playing = true;

              }

});

 

_this.btn_pause1.on('click', function(){

              _this.stop();

              _this.btn_play1.visible = true;

              playing = true;

});

--------------------------------------------------------------------------------

アニメーション2 (62フレーム目にアクションとボタン2を配置、ボタン1はイラストを配置)

var _this = this;

var playing = true;

 

_this.btn_play2.on('click', function(){

              if (playing) {

                            _this.play();

                            _this.btn_play2.visible = false;

                            playing = false;

              } else {

                            _this.stop();

                            _this.btn_play2.visible = true;

                            playing = true;

              }

});

 

_this.btn_pause2.on('click', function(){

              _this.stop();

              _this.btn_play2.visible = true;

              playing = true;

});

--------------------------------------------------------------------------------

 


以下のコードで試してみてください。

--------------------------------------------------------------------------------

var _this = this;
var animation1Playing = false;
var animation2Playing = false;

_this.btn_play1.on('click', function(){
    if (!animation1Playing) {
        _this.play();
        animation1Playing = true;
        animation2Playing = false;
    }
});

_this.btn_pause1.on('click', function(){
    _this.stop();
    animation1Playing = false;
});

_this.btn_play2.on('click', function(){
    if (!animation2Playing) {
        _this.gotoAndPlay(62);  // アニメーション2の再生
        animation2Playing = true;
        animation1Playing = false;
    }
});

_this.btn_pause2.on('click', function(){
    _this.stop();
    animation2Playing = false;
});

--------------------------------------------------------------------------------

 

2 replies

conaconaAuthor
Participating Frequently
January 9, 2024

ご返信ありがとうございます。

ご回答いただいたコードを追加してみましたが動作が変わらず、2.の一時停止ボタンをクリックしても停止になりません。

解決方法はありますでしょうか?

conaconaAuthor
Participating Frequently
January 9, 2024

申し訳ございません、上記訂正します。

新規ページにて作成し直し、再度検証しました。

再生 → 一時停止 → 再生と切り替えはできましたが、

_this.gotoAndPlay('Label名'); の

Label名を設定すると一時停止後の再生がそのラベルの1フレーム目からの再生となってしまいます。

停止位置から再生する方法はありますでしょうか?

momohanna
Community Expert
Community Expert
January 9, 2024

ActionScriptに停止位置から再生するというMethodがあるのか不明なのですが、停止位置から再生するのであれば、以下 _this.gotoAndPlay(startFrame)のstartFrameに指定したフレームから再生開始はできると思います。

 

-----------------------------------------------------------------------

var _this = this;
var playing = true;
var startFrame = 1; // 開始フレームを指定

_this.btn_play.on('click', function(){
    if (playing) {
        _this.gotoAndPlay(startFrame);
        _this.btn_play.visible = false;
        playing = false;
    } else {
        _this.stop();
        _this.btn_play.visible = true;
        playing = true;
    }
});

_this.btn_pause.on('click', function(){
    _this.stop();
    _this.btn_play.visible = true;
    playing = true;
});

-----------------------------------------------------------------------

 

momohanna
momohanna
Community Expert
Community Expert
January 8, 2024

こちらでどうでしょう。

 

-----------------------------------------------------------------------

var _this = this;
var playing = true;

_this.btn_play.on('click', function(){
    if (playing) {
        _this.gotoAndPlay('Label名');
        _this.btn_play.visible = false;
        playing = false;
    } else {
        _this.stop();
        _this.btn_play.visible = true;
        playing = true;
    }
});

_this.btn_pause.on('click', function(){
    _this.stop();
    _this.btn_play.visible = true;
    playing = true;
});

-----------------------------------------------------------------------

momohanna