噴水っぽいの
2本目。今度のサンプルは噴水。
今度は、ボール、つうかオブジェクトが移動して、
範囲外に出たときに、削除するんじゃなくて、
再配置するっていうテクニック。
見た目は面白いかも。
package { import flash.display.Sprite; import flash.display.StageAlign; import flash.display.StageScaleMode; import flash.events.Event; public class Fountain extends Sprite { private var count:Number = 100; private var balls:Array; // 重力設定 private var gravity:Number = 0.5; public function Fountain() { init(); } private function init():void{ stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT; balls = new Array; for(var i:int = 0; i < count; i++){ var ball:Ball = new Ball(10, Math.random() * 0xFFFFFF); ball.x = stage.stageWidth / 2; ball.y = stage.stageHeight; ball.vx = Math.random() * 2 - 1; ball.vy = Math.random() * -10 - 10; addChild(ball); balls.push(ball); } addEventListener(Event.ENTER_FRAME, onEnterFrame); } private function onEnterFrame(evt:Event):void { for(var i:int; i < balls.length; i++){ var ball:Ball = balls[i]; ball.vy += gravity; ball.x += ball.vx; ball.y += ball.vy; // 範囲外に出たら if(ball.x > stage.stageWidth || ball.x < 0 || ball.y > stage.stageHeight || ball.y < 0){ // 再配置 ball.x = stage.stageWidth / 2; ball.y = stage.stageHeight; ball.vx = Math.random() * 2 - 1; ball.vy = Math.random() * -10 - 10; } } } } } import flash.display.Sprite; class Ball extends Sprite { private var radious:Number; private var color:uint; public var vx:Number; public var vy:Number; public function Ball(radius:Number = 40, color:uint = 0xff0000) { this.radious = radius; this.color = color; init(); } private function init():void{ graphics.beginFill(color); graphics.drawCircle(0, 0, radious); graphics.endFill(); } }
再配置してるとこはこんなかんじ。
// 範囲外に出たら if(ball.x > stage.stageWidth || ball.x < 0 || ball.y > stage.stageHeight || ball.y < 0){ // 再配置 ball.x = stage.stageWidth / 2; ball.y = stage.stageHeight; ball.vx = Math.random() * 2 - 1; ball.vy = Math.random() * -10 - 10; }
ボールのx, yプロパティを再設定してるだけ。
あと、vx, vyというスピードも再設定してる。
以上です。