In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Lazy tower def

tower defence base
Get Adobe Flash player
by mutantleg 28 Aug 2012
/**
 * Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/xWbP
 */

package {
    import flash.text.TextField;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        
        public var deb:TextField;
        
        public function FlashTest() {
            // write as3 code here..
            
            deb = new TextField();
            addChild(deb);
            deb.text = "hello  world?";
            
                addChild(new myDef());
                
                
                  deb.text = "reached";
        }
    }
}
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.Sprite;




internal class myDef extends Sprite
{
  public var myWorld:tWorld;
  
  public var deb2:TextField;

  public function myDef()
  {
      deb2 = new TextField();
      addChild(deb2);
      deb2.text = "Hello2 ";
      deb2.y = 16;
      
      //deb2.text = "stage ";
      
      
      graphics.clear();
      graphics.lineStyle(1,0);
      graphics.drawRect(16,16,300,300);

    myWorld = new tWorld();
    addChild(myWorld);

        if (stage) init(null); 
        else 
     {addEventListener(Event.ADDED_TO_STAGE, init); }
    //addEventListener(MouseEvent.MOUSE_DOWN, mdown);
  //  stage.addEventListener(MouseEvent.MOUSE_UP, mup);
   // stage.addEventListener(Event.ENTER_FRAME, onEnter);
  }//ctor

    public function init(e:Event):void
    {
         removeEventListener(Event.ADDED_TO_STAGE, init);   
         stage.addEventListener(MouseEvent.MOUSE_DOWN, mdown);
         stage.addEventListener(Event.ENTER_FRAME, onEnter);
    }

public var frame:int = 0;
  public function mdown(e:MouseEvent):void
  {
    var a:tActor;
    a = new aTurret();
    a.x = mouseX;
    a.y = mouseY;
    a.team = 2;
    myWorld.addActor(a);


        
    //todo -- check if sector can be built in
    
  }//mdown

 // public function mup(e:MouseEvent):void  { }//mup

  public var mk:int = 0;

  public function onEnter(e:Event):void
  {

    mk += 1;
    if (mk >= 20) 
    {
      mk = 0;
      var a:tActor;
      a = new aMan();
      a.x = 0;
      a.y = 200;
      a.team = 1;
      myWorld.addActor(a);
      
    }//endif

    myWorld.update();

    frame += 1;
    deb2.text = ""+frame;


  }//onenter


}//classend



internal class tWorld extends Sprite
{
    
    public var deb:TextField;
  public function tWorld()
  {
    super();
    vecActor = new Array();
    
    deb = new TextField();
    addChild(deb);
    deb.y =32;
    deb.width = 400;
    deb.height = 200;
    deb.multiline = true;
  }

  public var vecActor:Array;

  public function addActor(a:tActor):void
  {
    vecActor.push(a);
    addChild(a);
    a.initActor(this);
  }

  public function clear():void
  {
    var i:int;
    var num:int;
    var a:tActor;

    num = vecActor.length;

    for (i = 0; i < num; i++)
    {
      a = vecActor[i];
      removeChild(a);
    }//nexti
    vecActor = new Array();
  }//clear

  public function update():void
  {
    var i:int;
    var num:int;
    var a:tActor;

    num = vecActor.length;

    for (i = 0; i < num; i++)
    {
      a = vecActor[i];
      if (a.dead) { continue; }
      a.update();
    }//nexti

num = vecActor.length;
    for (i = 0; i < num; i++)
    {
      a = vecActor[i];
      if (a.dead == false) { continue; }
     vecActor[i] = null;
       vecActor[i] = vecActor[num-1];
       vecActor.pop();
       
      removeChild(a);
      a = null;
      i -= 1;
      num = vecActor.length;
    }
    
    deb.text = ""+vecActor.length;
  }//update


}//classend


internal class tActor extends Sprite
{
  public var dead:Boolean = false;
  public var hp:Number = 1;
  public var rad:Number = 4;
  public var world:tWorld;
  public var vx:Number = 0;
  public var vy:Number = 0;
  public var team:int = 0;
  public var inv:int = 0; //invincible if not 0

  public function tActor():void
  {

  }//ctor

  public function initActor(w:tWorld):void
  {
    world = w;
    init();
  }

  public function kill():void
  {
    if (dead) { return; }
    dead = true;
  }//kill

  public function gotHit(d:Number,hx:Number,hy:Number):void
  {
    hp -= d;
    //vx += hx;
    //vy += hy;
    if (hp <= 0) { kill(); }
    
    x += Math.random() * 4 -2;
    y += Math.random() * 4 -2;
    //y+=8;
  }//gothit

  public function init():void
  {

  }//init

  public function update():void
  {}//update
}//classend


internal class aMan extends tActor
{
public var deb:TextField;    
    
  public function aMan()
  {
   super();
   graphics.clear();
   graphics.lineStyle(2,0);
   graphics.drawCircle(0,0,8);
   hp=10;
   
   deb = new TextField();
   addChild(deb);
  }//ctor

  override public function init():void
  {
    vx = 2;
  }//init  

  override public function update():void
  {
    x += 2;
  //  x += vx;
  //  y += vy;
        deb.text = ""+hp;

    if (x >= 400) { dead = true; }  
  }//update

}//classend

internal class aLaser extends tActor
{
    
    public var deb:TextField;
    
  public function aLaser(sx:Number, sy:Number, ex:Number, ey:Number)
  {
      //x = sx;
      //y = sy;
      
    inv = 99;
    graphics.clear();
    graphics.lineStyle(2,0);
    graphics.moveTo(sx, sy);
    graphics.lineTo(ex, ey);

    hp = 3;
    
    deb = new TextField();
    addChild(deb);
    deb.text ="laser";
    deb.x = sx+(ex-sx)/2;
    deb.y = sy+(ey-sy)/2;
  }//ctor

  override public function update():void
  {
    hp -= 1;
    deb.text = ""+hp;
    if (hp <= 0) { dead =true;}
  }//update


}//classend


internal class aTurret extends tActor
{
  public var w:int = 0;
  public var dmg:Number = 1;
  public var range:Number = 100;
  

    public var deb:TextField;

  public function aTurret()
  {
      inv = 99;
    graphics.clear();
    graphics.lineStyle(2,0);
    graphics.drawRect(-8,-8,16,16);
    
    deb = new TextField();
    addChild(deb);
  }//ctor


  //squared dist  
  public function getDist(kx:Number, ky:Number):Number
  {
    var dx:Number;
    var dy:Number;
    dx = kx - x;
    dy = ky - y;
    return (dx*dx + dy*dy);
  }//getdist

  public function getTarget(tdist:Number):tActor
  {
    var vec:Array;
    var i:int;
    var num:int;
    var a:tActor;
    var dd:Number; //dist squared
    dd = tdist * tdist;
    vec = world.vecActor;

    num = vec.length;
    for (i = 0; i < num; i++)
    {
      a = vec[i];
      if (a == this) {continue;}
      if (a == null || a.dead) { continue; }
      if (a.team == team) { continue; }
      if (a.inv > 0) { continue; }
      if (getDist(a.x, a.y) <= dd) { return a;}
    }//nexti
    return null;
  }//gettarget


  override public function update():void
  {
      deb.text = ""+w;
    w += 1;

    if (w >= 10)
    {
      w = 0;
      var a:tActor;
      a = getTarget(range);
      if (a != null)
      {
        a.gotHit(1,0,0);
      
      var b:tActor;
      b = new aLaser(a.x,a.y, x,y);
      world.addActor(b);
      }//endif2
    //  else { y -= 1;}
    }//endif

  }//update


}//classend