flash on 2015-3-3
followup to http://wonderfl.net/c/sTcp
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/vErx
*/
package {
import flash.geom.Rectangle;
import flash.geom.Matrix;
import flash.display.BitmapData;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
var i:int;
var a:xBall;
for (i = 0; i < 32; i++)
{
a = new xBall(); vec.push(a);
a.cx=20+(i%8)*70; a.cy = 100+(i/ 8)*70;
a.rad = 8 + Math.random()*32;
a.ang = Math.random()*6.28;
a.c = 0xf + Math.random()*0xffffff;
}//nexti
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var pic:BitmapData = new BitmapData(64,64,false,0);
public var mat:Matrix = new Matrix();
public var cw:Number = 8;
public var ch:Number = 8;
public var rect:Rectangle = new Rectangle();
public function setRect(ax:Number,ay:Number,aw:Number,ah:Number, c:uint):void
{
rect.x = Math.floor(ax/cw); rect.y = Math.floor(ay/ch);
rect.width = Math.floor(aw/cw); rect.height = Math.floor(ah/ch);
pic.fillRect(rect, c);
}//setrect
public function isWall(ax:Number, ay:Number):Boolean
{
if (ax < 0) { return true; }
if (ay < 0) { return true; }
if (ax >= 465) { return true; }
if (ay >= 465) { return true; }
return pic.getPixel(Math.floor(ax/cw),Math.floor(ay/ch)) > 0; // == 0xFF;
}//iswall
public var vec:Vector.<xBall> = new Vector.<xBall>(0,false);
public var gt:int = 0;
public function onEnter(e:Event):void
{
var mx:Number; var my:Number;
mx = stage.mouseX; my = stage.mouseY;
pic.unlock();
pic.lock();
graphics.clear();
graphics.lineStyle(2, 0);
mat.a = cw; mat.d = ch;
graphics.beginBitmapFill(pic, mat,false,false);
graphics.drawRect(0,0,465,465);
graphics.endFill();
graphics.lineStyle(2, 0xFFffFF);
var wh:int = 0;
var i:int; var num:int; var a:xBall;
num = vec.length;
for (i = 0; i < num; i++)
{
a = vec[i];
setRect(a.cx-a.rad,a.cy-a.rad,a.rad*2,a.rad*2, 0);
a.vx = Math.cos(a.ang) * a.vw;
a.vy = Math.sin(a.ang) * a.vw;
wh = 0;
if (a.vx > 0 && isWall(a.cx+a.vx+a.rad, a.cy)) { a.vx*=-1; wh = 1;}
if (a.vx < 0 && isWall(a.cx+a.vx-a.rad, a.cy)) { a.vx*=-1; wh = 1;}
if (a.vy > 0 && isWall(a.cx, a.cy+a.vy+a.rad)) { a.vy*=-1; wh = 1;}
if (a.vy < 0 && isWall(a.cx, a.cy+a.vy-a.rad)) { a.vy*=-1; wh = 1;}
if (wh > 0)
{ a.ang = Math.atan2(a.vy,a.vx); }
if ( (gt+i) % 30 == 0)
{ a.ang = Math.atan2(my-a.cy,mx-a.cx); }
a.cx += a.vx; a.cy+=a.vy;
setRect(a.cx-a.rad,a.cy-a.rad,a.rad*2,a.rad*2, a.c); //0xFF);
graphics.drawCircle(a.cx, a.cy, a.rad);
graphics.drawCircle(a.cx, a.cy, 4);
graphics.moveTo(a.cx,a.cy);
graphics.lineTo(a.cx+Math.cos(a.ang)*8,a.cy+Math.sin(a.ang)*8);
}//nexti
graphics.drawRect(mx-4,my-4,8,8);
gt += 1;
}//onenter
}//classend
}
internal class xBall
{
public var cx:Number = 0;
public var cy:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
public var ang:Number = 0;
public var vw:Number = 1;
public var rad:Number = 8;
public var c:uint = 0xff;
}//xball