// forked from termat's starfield
/*
元ネタ=http://www.chromeexperiments.com/detail/starfield/
*/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
[SWF(width = "480", height = "480", backgroundColor = "0x000000", fps = "60")]
public class Practice70 extends Sprite{
private var stars:Array;
private var n:int = 512;
private var test:Boolean;
public function Practice70() {
stars = new Array();
stage.scaleMode = "showAll";
for (var i:int = 0; i < n; i++) {
var xx:Number = Star.width * Math.random()*2 - Star.cx*2;
var yy:Number = Star.height * Math.random()*2 - Star.cy*2;
var zz:Number = Math.round(Math.random() * Star.cz);
stars.push(new Star(xx, yy, zz));
}
addEventListener(Event.ENTER_FRAME, update);
}
private function update(e:Event):void {
var mx:Number = mouseX - Star.cx;
var my:Number = mouseY - Star.cy;
graphics.clear();
for (var i:int = 0; i < stars.length; i++) stars[i].update(graphics, mx, my);
}
}
}
import flash.display.Graphics;
class Star {
public static var speed:Number = 4;
public static var width:Number = 480;
public static var height:Number = 480;
public static var ratio:Number = 256;
public static var cx:Number = width / 2;
public static var cy:Number = height / 2;
public static var cz:Number = (width + height) / 2;
public static var color:Number = 1 / cz;
private var x:Number;
private var y:Number;
private var z:Number;
private var px:Number;
private var py:Number;
private var test:Boolean;
public function Star(_x:Number, _y:Number, _z:Number):void {
x = _x; y = _y; z = _z;
px = 0; py = 0;
}
public function update(g:Graphics, mx:Number, my:Number):void {
test = true;
var x_save:Number = px;
var y_save:Number = py;
x += mx / 16;
if (x > cx*2) { x -= width *2; test = false; }
if (x < -cx*2) { x += width *2; test = false; }
y += my /16;
if (y > cy *2) { y -= height *2; test = false; }
if (y < -cy *2) { y += height *2; test = false; }
z -= speed;
if (z > cz) { z -= cz; test = false; }
if (z < 0) { z += cz; test = false; }
px = cx + (x / z) * ratio;
py = cy + (y / z) * ratio;
if (x_save > 0 && x_save < width && y_save > 0 && y_save < height && test) {
g.lineStyle((1 - color * z) * 2, 0xffffff);
g.moveTo(x_save, y_save);
g.lineTo(px, py);
}
}
}