かえる君の数珠繋ぎ (1)

かえる君が数珠繋ぎのように動くもの。 :boy:

youichさん :hakase: から教えてもらったものに、少し手を加えてみた。
1つ目は、ほとんどそのまま。


_root に、ムービークリップ(インスタンス名: frog1~frog5) を配置。
frog には、ムービークリップ(インスタンス名: base, shade) を配置。

This movie requires Flash Player 9.0.0

_root 第1フレームに
var frogMax:Number = 5;
var frogWidth:Number = 52;
var limitLeft:Number = 10;
var limitRight:Number = 590;
for (var n:Number = 1; n <= frogMax; n++) {
  var _mc:MovieClip = this["frog"+n];
  var left_mc:MovieClip = this["frog"+(n-1)];
  var right_mc:MovieClip = this["frog"+(n+1)];
  _mc.leftNeighbor = left_mc;
  _mc.rightNeighbor = right_mc;
  _mc.rangeLeft = limitLeft + frogWidth*(n-0.5);
  _mc.rangeRight = limitRight - frogWidth*(frogMax-n+0.5);

}
と記述。

frog 第1フレームに
var frogWidth:Number = 52;
var clickPos:Number;
base.onPress = function():Void {
  clickPos = this._parent._parent._xmouse;
  onMouseMove = chainSlide;
}
base.onRelease = function():Void {
  delete onMouseMove;
};
base.onReleaseOutside = base.onRelease;

function chainSlide():Void {
  this._x = _root._xmouse - clickPos;
  // 移動範囲の制限
  if (this._x < rangeLeft) {
    this._x = rangeLeft;
  }
  if (this._x > rangeRight) {
    this._x = rangeRight;
  }
  if (leftNeighbor) {
    leftNeighbor.chainRight(this)
  }
  if (rightNeighbor) {
    rightNeighbor.chainLeft(this)
  }

  updateAfterEvent();
}
// 右側から押される関数
function chainRight(rightTarget:MovieClip):Void {
  if (rightTarget._x - this._x < frogWidth) {
    this._x = rightTarget._x - frogWidth;
  }
  if (leftNeighbor) {
    leftNeighbor.chainRight(this);
  }

}
// 左側から押される関数
function chainLeft(leftTarget:MovieClip):Void {
  if (this._x - leftTarget._x < frogWidth) {
    this._x = leftTarget._x + frogWidth;
  }
  if (rightNeighbor) {
    rightNeighbor.chainLeft(this);
  }

}
と記述。

ポイントは…。横にいるかえる君を leftNeighbor, rightNeighbor というプロパティで指定。かえる君を動かした際、chainRight(), chainLeft() で、隣のかえる君に位置移動を数珠繋ぎのように伝達していく。


これを、1フレームにまとめてみる。

This movie requires Flash Player 9.0.0

_root 第1フレームに
var frogMax:Number = 5;
var frogWidth:Number = 52;
var limitLeft:Number = 10;
var limitRight:Number = 590;
for (var n:Number = 1; n <= frogMax; n++) {
  var _mc:MovieClip = this["frog"+n];
  var left_mc:MovieClip = this["frog"+(n-1)];
  var right_mc:MovieClip = this["frog"+(n+1)];
  _mc.leftNeighbor = left_mc;
  _mc.rightNeighbor = right_mc;
  _mc.rangeLeft = limitLeft + frogWidth*(n-0.5);
  _mc.rangeRight = limitRight - frogWidth*(frogMax-n+0.5);

  _mc.base.onPress = function():Void {
    var target:MovieClip = this._parent;
    target.clickPos = target._xmouse;
    target.onMouseMove = chainSlide;
  };
  _mc.base.onRelease = function():Void {
    var target:MovieClip = this._parent;
    delete target.onMouseMove;
  };
  _mc.base.onReleaseOutside = _mc.base.onRelease;
}

function chainSlide():Void {
  this._x = _root._xmouse - this.clickPos;
  // 移動範囲の制限
  if (this._x < this.rangeLeft) {
    this._x = this.rangeLeft;
  }
  if (this._x > this.rangeRight) {
    this._x = this.rangeRight;
  }
  if (this.leftNeighbor) {
    chainRight(this.leftNeighbor, this)
  }
  if (this.rightNeighbor) {
    chainLeft(this.rightNeighbor, this)
  }

  updateAfterEvent();
}
// 右側から押される関数
function chainRight(target:MovieClip, rightTarget:MovieClip):Void {
  if (rightTarget._x - target._x < frogWidth) {
    target._x = rightTarget._x - frogWidth;
  }
  if (target.leftNeighbor) {
    chainRight(target.leftNeighbor, target);
  }

}
// 左側から押される関数
function chainLeft(target:MovieClip, leftTarget:MovieClip):Void {
  if (target._x - leftTarget._x < frogWidth) {
    target._x = leftTarget._x + frogWidth;
  }
  if (target.rightNeighbor) {
    chainLeft(target.rightNeighbor, target);
  }

}
と記述。

でけたー。 :doki: