flash on 2015-1-10
♥0 |
Line 95 |
Modified 2015-01-10 23:05:44 |
MIT License
archived:2017-03-30 11:50:23
ActionScript3 source code
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/jAIP
*/
package {
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
vecPart = new Vector.<xPart>(0, false);
var i:int; var a:xPart;
for (i = 0; i < 4; i++)
{
a = new xPart(); vecPart.push(a);
a.cx = cx + Math.cos(i*0.3)*32;
a.cy = cy + Math.sin(i*0.3)*32;
}
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
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 false;
}//iswall
public var cx:Number = 230;
public var cy:Number = 230;
public var vecPart:Vector.<xPart>;
public function onEnter(e:Event):void
{
graphics.clear();
graphics.lineStyle(2, 0);
var mx:Number; var my:Number;
mx = stage.mouseX; my = stage.mouseY;
cx = mx; cy = my;
graphics.drawCircle(cx,cy, 32);
var ang:Number; var d:Number;
var i:int; var num:int;
var a:xPart; var b:xPart;
num = vecPart.length;
var t:Number;
t = 0.2;
for (i = 0; i < num; i++)
{
a = vecPart[i];
if (i+1==num) { b=vecPart[0];} else { b = vecPart[i+1]; }
ang = Math.atan2(b.cy-a.cy, b.cx-a.cx);
d = getMag(a.cx-b.cx, a.cy-b.cy);
// t = (30-d)*0.01;
// if (t < -2) { t = -2; }
// else if (t >2) { t =2;}
a.vx += Math.cos(ang) *t;
a.vy += Math.sin(ang) *t;
graphics.moveTo(a.cx,a.cy);
graphics.lineTo(b.cx,b.cy);
}//nexti
t=0.5;
for (i = 0; i <num; i++)
{
a = vecPart[i];
a.vy += 0.1;
ang = Math.atan2(cy-a.cy, cx-a.cx);
d = getMag(a.cx-cx, a.cy-cy);
if (d > 80)
{
a.vx += Math.cos(ang) *t;
a.vy += Math.sin(ang) *t;
}else if (d < 70)
{
a.vx += Math.cos(ang) *-t;
a.vy += Math.sin(ang) *-t;
}
a.vx *=0.95; a.vy *= 0.95;
if (a.vx > 0 && isWall(a.cx+a.vx,a.cy)) {a.vx*=-0.5; }
if (a.vx < 0 && isWall(a.cx+a.vx,a.cy)) {a.vx*=-0.5; }
if (a.vy > 0 && isWall(a.cx,a.cy+a.vy)) {a.vy*=-0.5; }
if (a.vy < 0 && isWall(a.cx,a.cy+a.vy)) {a.vy*=-0.5; }
a.cx+=a.vx; a.cy+=a.vy;
graphics.moveTo(a.cx,a.cy);
graphics.lineTo(cx,cy);
graphics.drawCircle(a.cx, a.cy, 4);
}//nexti
graphics.beginFill(0xF33524,0.5);
a = vecPart[0]; graphics.moveTo(a.cx,a.cy);
for (i = 1; i < num; i++)
{ a = vecPart[i]; graphics.lineTo(a.cx,a.cy); }
a = vecPart[0]; graphics.lineTo(a.cx,a.cy);
graphics.endFill();
}//onenter
public function getMag(ax:Number, ay:Number):Number
{ return Math.sqrt(ax*ax+ay*ay); }
}//classend
}
internal class xPart
{
public var cx:Number = 0;
public var cy:Number = 0;
public var vx:Number = 0;
public var vy:Number = 0;
}//xpart