forked from: 【AS100本ノック】9回目:雪
AS100本ノック
* 9回目のお題は「雪」
* あなたなりの「雪」を表現してください。
雪というと幻想的なのばっかり出るので、たまには現実に即してみようと思いました。
いろいろ間違ってるけど気にしない、というかskinがうまく動かなくて豆腐になっちゃった。
BGM: 夢いっぱいの箱
* フリー音楽素材 H/MIX GALLERY
* http://www.hmix.net/
* osamXさんからお借り
/**
* Copyright uwi ( http://wonderfl.net/user/uwi )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/qfXc
*/
// forked from mex's 【AS100本ノック】9回目:雪
/*
* AS100本ノック
* 9回目のお題は「雪」
* あなたなりの「雪」を表現してください。
*/
// 雪というと幻想的なのばっかり出るので、たまには現実に即してみようと思いました。
// いろいろ間違ってるけど気にしない、というかskinがうまく動かなくて豆腐になっちゃった。
/*
* BGM: 夢いっぱいの箱
* フリー音楽素材 H/MIX GALLERY
* http://www.hmix.net/
* osamXさんからお借り
*/
package {
import flash.display.MovieClip;
import flash.events.*;
import flash.media.*;
import flash.net.*;
import flash.system.Security;
import flash.text.TextField;
import flash.utils.Dictionary;
import com.actionsnippet.qbox.*;
import net.hires.debug.Stats;
[SWF(backgroundColor="#000000")]
public class FlashTest extends MovieClip {
private var LIM : Number = 465 / 30;
private var _qb : QuickBox2D;
private var _qos : BLE; // 双方向雪リスト
private var _tf : TextField;
private var _t : uint = 0; // ステップカウンタ
private var _qodic : Dictionary; // すべての物体への弱参照紐付け(削除用)
public function FlashTest() {
_qb = new QuickBox2D(this, {debug:false, iterations:2});
_qodic = new Dictionary(true);
init();
// addChild(new Stats());
_qb.addEventListener(QuickBox2D.STEP, onStep);
addEventListener(MouseEvent.CLICK, function(e : MouseEvent) : void { init(); });
_tf = new TextField();
// addChild(_tf);
_tf.textColor = 0xffffff;
_tf.y = 100;
loadSound();
}
private function init() : void
{
_qb.stop();
for(var k : * in _qodic){
QuickObject(k).destroy();
}
_qodic = new Dictionary(true);
_qodic[_qb.addBox({x:8, y:15, width:16, height:1, density:0})] = 3;
_qos = new BLE();
buildHouse();
_qb.start();
}
private function onStep(e : Event) : void
{
if(_t % 16 == 0)addSnow();
_t++;
removeSnow();
}
private function loadSound():void {
Security.loadPolicyFile("http://flash-scope.com/wonderfl/crossdomain.xml");
var s : Sound = new Sound();
s.addEventListener(Event.COMPLETE, function(e:Event):void{
s.play(0,9999);
});
s.load(new URLRequest("http://flash-scope.com/wonderfl/ChristmasSpectrum/o13.mp3"));
}
private function addSnow() : void
{
var obj : QuickObject = _qb.addBox({x:Math.random() * LIM, y:0, width : 1.0, height : 1.0, friction:2.5, restriction:0, fixedRotation:false, linearDamping:5.0, angularDamping:1.0, fillColor:0xffffff});
_qodic[obj] = 2;
var b : BLE = new BLE(obj);
b.insertAfter(_qos);
}
private function removeSnow() : void
{
// _tf.text = "" + _qb.w.GetBodyCount() + " " + _qos.length() + "\n";
for(var b : BLE = _qos.next;b != null;b = b.next){
// _tf.text += "" + b.content.x + "\n";
if(b.content.x < -0.7 || b.content.x > LIM + 0.7){
b.content.destroy();
b = b.remove().prev;
continue;
}
}
}
private function buildHouse() : void
{
var c : uint = 0x884444;
_qodic[_qb.addBox({x:1.5, y:12, width:1, height:5, fillColor:c})] = 1;
_qodic[_qb.addBox({x:6.5, y:12, width:1, height:5, fillColor:c})] = 1;
_qodic[_qb.addBox({x:4, y:9, width:6, height:1, fillColor:c})] = 1;
_qodic[_qb.addPoly({x:4, y:7.5, verts:[[0, -2, 3, 1, -3, 1]], fillColor:c})] = 1;
c = 0x444488;
_qodic[_qb.addBox({x:8.5, y:12, width:1, height:5, fillColor:c})] = 1;
_qodic[_qb.addBox({x:13.5, y:12, width:1, height:5, fillColor:c})] = 1;
_qodic[_qb.addBox({x:11, y:9, width:6, height:1, fillColor:c})] = 1;
_qodic[_qb.addPoly({x:11, y:7.5, verts:[[0, -2, 3, 1, -3, 1]], fillColor:c})] = 1;
}
}
}
// Bidirectional Linked Element
class BLE
{
public var content : *;
public var prev : BLE = null;
public var next : BLE = null;
public function BLE(c : * = null)
{
this.content = c;
}
public function insertAfter(t : BLE) : void
{
if(t != null){
var n : BLE = t.next;
t.next = this;
n.prev = this;
this.prev = t;
this.next = n;
}
}
public function remove() : BLE
{
var p : BLE = this.prev;
var n : BLE = this.next;
if(p != null)p.next = n;
if(n != null)n.prev = p;
this.prev = null;
this.next = null;
return n;
}
public function length() : uint
{
for(var i : uint = 0, s : BLE = this;s != null;i++, s = s.next);
return i;
}
}