flash on 2015-12-10
♥0 |
Line 94 |
Modified 2015-12-10 05:33:36 |
MIT License
archived:2017-03-30 11:40:59
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/8oMM
*/
package {
import flash.text.TextField;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public function FlashTest() {
deb = new TextField();
deb.width=320;deb.height=240;
deb.mouseEnabled=false;
addChild(deb);
vecAct = new Vector.<xActor>(0,false);
vecBull = new Vector.<xActor>(0,false);
var a:xActor;
a = new xActor(); a.cx = 230; a.cy = 230;
a.team = 1;
vecAct.push(a);
var i:int;
for (i=0;i<64;i+=1)
{
a = new xActor();
a.cx = Math.random()*435; a.cy = Math.random()*435;
a.team = 3;
vecAct.push(a);
}//nexti
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public static var vecAct:Vector.<xActor>;
public static var vecBull:Vector.<xActor>;
public var gt:int = 0;
public var deb:TextField;
public function onEnter(e:Event):void
{
gt+=1;
var a:xActor;
a = new xBull();
a.cx=230;a.cy=230;
a.ang = Math.random()*6.28; a.hp=64;
vecBull.push(a);
updateVec(vecAct); updateVec(vecBull);
graphics.clear(); graphics.lineStyle(2, 0);
renderVec(vecAct); renderVec(vecBull);
deb.text = ""+vecAct.length+"\n"+vecBull.length;
}//onenter
public function renderVec(vec:Vector.<xActor>):void
{ var i:int; var num:int; var a:xActor;
num = vec.length;
for(i=0;i<num;i+=1)
{ a = vec[i]; if (a==null || a.hp <=0) {continue;}
graphics.drawRect(a.cx-a.xrad,a.cy-a.yrad,a.xrad*2,a.yrad*2); }
}//render
public function updateVec(vec:Vector.<xActor>):void
{ var i:int; var num:int; var a:xActor; var k:int;
num = vec.length;
for(i=0;i<num;i+=1)
{ a = vec[i]; if (a ==null || a.hp <= 0)
{ for (k=i;k<num-1;k+=1) { vec[k]=vec[k+1]; }
vec.pop(); num-=1; i-=1; continue; } a.update();
}//nexti
}//update
}//classend
}
internal class xActor
{
public var cx:Number = 0; public var cy:Number = 0;
public var xrad:Number = 8; public var yrad:Number = 8;
public var hp:Number = 1;
public var ang:Number = 0;
public var vx:Number = 0; public var vy:Number = 0;
public var team:int = 0;
public function update():void {}
}//xactor
internal class xBull extends xActor
{
override public function update():void
{
var i:int; var num:int; var a:xActor;
var vec:Vector.<xActor>;
vec = FlashTest.vecAct;
num = vec.length;
for(i=0;i<num;i+=1)
{ a = vec[i];
if (a.team <= 0 || a.team == team) { continue; }
if (cx-xrad > a.cx+a.xrad) { continue; }
if (cx+xrad < a.cx-a.xrad) { continue; }
if (cy-yrad > a.cy+a.yrad) { continue; }
if (cy+yrad < a.cy-a.yrad) { continue; }
a.hp -= 1; hp = -1;
return;
}//nexti
hp -=1;
cx += Math.cos(ang)*4;
cy += Math.sin(ang)*4;
}//update
}//xbull