Webデザインの勉強と製作 | あかとんぼ

フェリカテクニカルアカデミーの学習をベースに、Webについての勉強と製作の過程をまとめています。

[Flash]⑧MovieClipの再生/逆再生、他との接触

  • フレームの順再生と逆再生
    • onEnterFrame
    • this.nextFrame()/prevFrame()
  • フレームの接触を感知

フレームの順再生と逆再生

this.stop();

/* 傘を開く */
function openUmbrella():Void
{
     this.onEnterFrame = playForward;
}
/* 傘を閉じる */
function closeUmbrella():Void
{
     this.onEnterFrame = playBackward;
}

/* 順再生する関数 */
function playForward():Void
{
     this.nextFrame();
     if( this._currentframe == this._totalflames )
          delete this.onEnterFrame;
}

/* 逆再生する関数 */
function playBackward():Void
{
     this.prevFrame();
     if( this._currentframe == 1 )
          delete this.onEnterFrame;
}
  • openUmbrella関数が実行されたら、フレームごとに(onEnterFrame)、playForward関数が呼ばれる。
  • this.nextFrame();をフレーム毎に呼び出すことで順再生。
  • this.prevFrame();をフレーム毎で逆再生。
  • if( this.currentframe == this.totalflames )
  • 現在のフレーム番号が全体のフレーム数に達したら…
  • delete this.onEnterFrame;
  • onEnterFrameイベントに割り当てた関数(playForward)を解除。以後playForwardが呼び出されなくなる。
フレームの接触を感知
var human: MovieClip;
/*雨雲を動かす関数*/
human.onMouseMove = moveRainCloud;

function moveRainCloud():Void
{
     rainCloud._x = this._parent._xmouse;
     wetGround._x = this._parent._xmouse;
    
     if( this.hitTest(wetGround) )
     {
          this.openUmbrella();
     }else
     {
          this.closeUmbrella();
     }
}
  • human.onMouseMove▶humanのonMouseMoveイベント(マウスが動くごとに呼ばれるイベント)にmoveRainCloudという関数を割り当て。
  • rainCloud.x / wetGround.x▶雨雲、濡れた地面を横方向に移動。
  • this.parent.xmouse▶_rootでのマウスポインタのX座標を調べてその位置に合わせる。
  • if( this.hitTest(wetGround) )▶this(human)がwetGroundと接触しているか?
  • this.openUmbrella()▶this(human)の関数openUmbrellaを実行

f:id:akatonbo_web:20150525001700p:plain

hitTestメソッド
  • 他のムービークリップと接触しているか調べることができる
  • 接触していたらtrue,いなければfalseを返す