ドラッグして投げる : ActionScript2.0

Pocket

ActionScriptでドラッグして投げるサンプル。


流れ

  1. 幅:20,高さ:20の円を描画(Shift+ドラッグで真丸の円)。
  2. ムービークリップシンボルへ変換(F8)。変換ダイアログのオプションを下記のように設定。
    名前: ball
    識別子: ball
    リンケージ:ActionScriptへ書き出し: 最初のフレームへ書き出し
  3. 円はActionScriptのattachMovie関数で動的に書き出すため、ステージから削除。
  4. 1フレーム目にActionScriptを設定。

function Ball() {
	var oldX;
	var oldY;
	var vx = 0;
	var vy = 0;
	this.init = function() {
		this.onEnterFrame = moving;
	};
	this.onPress = function() {
		oldX = this._x;
		oldY = this._y;
		this.startDrag(false);
		delete this.onEnterFrame;
		this.onEnterFrame = tracing;
	};
	this.onRelease = function() {
		this.stopDrag();
		delete this.onPress;
		delete this.onEnterFrame;
		this.onEnterFrame = moving;
	};
	this.onReleaseOutside = function() {
		this.stopDrag();
		delete this.onPress;
		delete this.onEnterFrame;
		this.onEnterFrame = moving;
	};
	var tracing = function () {
		vx = this._x-oldX;
		vy = this._y-oldY;
		oldX = this._x;
		oldY = this._y;
	};
	var moving = function () {
		this._x += vx;
		this._y += vy;
	};
}
var o = new Ball();
var mc = _root.attachMovie('ball', 'ball', _root.getNextHighestDepth(), o);
mc.init();

※onReleaseOutsideメソッドを記述しないと正常に動作しない。

コメント

No comments yet.

コメントの投稿

改行と段落タグは自動で挿入されます。
メールアドレスは表示されません。