ActionScriptの練習


Flashアレルギーを克服しなきゃと思って作ってみた。


・レイヤ側


var id = 0;
// 雪生成
for (id=0; id<20; id++) {
this.snow0.duplicateMovieClip('snow'+id, id);
}

// 少しずつ増えていきます
var snowObserver = setInterval(function() {
if (Math.random() * 10 < 1) {
snow.duplicateMovieClip('snow'+(++id), id, {_y:0});
//trace('create new snow id : ' + id);
}
if (id >= 200)
clearInterval(snowObserver);
}, 500);


・雪オブジェクト側(snow0)


onClipEvent (load) {
// 落下角度
fallangle = -Math.PI + Math.random() * Math.PI ;
// 落下方向(左右)
falldirection = Math.random() * 1 - 1;
// 倍率
this._xscale = this._yscale = 50 + Math.random() * 70;
// 落下速度(倍率が高いほど速度も上昇)
fallspeed = 1 + Math.random() * 2 + this._xscale / 60;
// 初期位置
this._x = Math.random() * Stage.width;
this._y = Math.random() * Stage.height;
// 透過度
this._alpha = 60 + Math.random() * 40;
}

onClipEvent (enterFrame) {
// 横軸
rad = (fallangle / 180) * Math.PI ;
// trace(this + ' : ' + rad);
if (Math.random() * 1000 < 1)
falldirection = -falldirection;
this._x -= Math.cos(rad) * falldirection;
// 縦軸
if (Math.random() * 8)
this._y += fallspeed;
// 下まで行ったら上に戻る
if (this._y >= Stage.height || this._x >= Stage.width || this._x + this._width <= 0) {
this._x = Math.random() * Stage.width;
this._y = -10;
}
}


元が同じなだけに、JavaScriptと似てるけど、イベントハンドラの取得の仕方がちょっと違うんだなぁと感じた。


雪の落下のアルゴリズムは、下記のサイトを参考にさせていただきました。
http://www.kirupa.com/developer/mx/snow.htm