Dropping Clips on Sprite Graphics V2

Follow-up to last post, here's a whole class that randomly drops MCs onto another using a MovieClip with blue graphics as base on where to throw.

You would initialize the class like this:
var airRaid:AirRaid = new AirRaid(new LandMass() as MovieClip, spots, "MSpot", 40, 40, 25);
airRaid.addEventListener(Event.COMPLETE, doSomething);

LandMass() would be your Sprite/MovieClip with the blue landmass. Notice the blue part of your land must have the hex code "cc".
spots is a MC in the main class onto which
"MSpot" named MCs from library get dropped.

drops parameter is the amount of MSpots to be dropped, 40 in example.
range would be the area to be left clear around each MSpot
interval defines the speed in which to drop each one, in milliseconds.


The AirRaid class looks like this:

package {
 import flash.display.MovieClip;
 import flash.display.BitmapData;
 import flash.events.Event;
 import flash.events.EventDispatcher;
 import flash.events.TimerEvent;
 import flash.geom.Point;
 import flash.utils.Timer;
 import flash.utils.getDefinitionByName;
 
 import gs.TweenLite;
 import gs.easing.*;
 public class AirRaid extends EventDispatcher {
  private var landpixels:BitmapData;
  private var landmass:MovieClip;
  // define bombDrops in order to keep track of space taken by previous dots
  // burp function checks random coordinate on map
  private var bombDrops:Array = new Array();
  private var bombName:String;
  private var tgt:MovieClip;
  //this means 60 pixels around point are to be kept free  
  private var pxrange:uint = 30;

  private var visitation:Timer;

  function AirRaid(map:MovieClip, area:MovieClip, bombMC:String, drops:uint = 50, range:uint = 20, interval:uint = 50){
   landmass = map;
   bombName = bombMC;
   tgt = area;
   pxrange = range;
   landpixels = new BitmapData(map.width, map.height);
   landpixels.draw(map);
   visitation = new Timer(interval, drops);
   visitation.addEventListener(TimerEvent.TIMER, dropBomb);
   visitation.addEventListener(TimerEvent.TIMER_COMPLETE, makePeace);
   visitation.start();
  }

  function dropBomb(e:TimerEvent):void {
   var point:Point = randPos();
   while(isWater(point))point=randPos();
   //trace("found land: " + point.x + "x" + point.y);
   var Reference:Class = getDefinitionByName(bombName) as Class;
   var cake:Object = new Reference();
   cake.x = point.x;
   cake.y = point.y;
   cake.scaleX=cake.scaleY=0.1;
   var tgtScale=(Math.random()/2)+0.5;
   TweenLite.to(cake, 1, {scaleX:tgtScale, scaleY:tgtScale, ease:Back.easeOut});
   
   tgt.addChild(MovieClip(cake));
   bombDrops.push(point);
  }
  
  function makePeace(e:TimerEvent):void {
   dispatchEvent(new Event(Event.COMPLETE));
  }
  
  
  function randPos():Point {
   return new Point(Math.floor(Math.random()*landmass.width), Math.floor(Math.random()*landmass.height));
  }

  function isWater(p:Point):Boolean {
   //first check if spot and surrounding area is already used
   for(var h:uint = 0; h < bombDrops.length; h++){
    var op:Point = bombDrops[h];
    if(op.x > p.x - pxrange && op.x < p.x + pxrange){
     if(op.y > p.y - pxrange && op.y < p.y + pxrange){
      //trace("found dot inside point range");
      return true;
     }
    }
   }
   var blue:uint = landpixels.getPixel32(p.x, p.y) & 0xFF;
   // in my sprite actually the land mass is blue so if
   // the pixel contains blue, this means no, it's not water
   if(blue.toString(16)=="cc"){
    return false;
   }else{
    //trace("found water");
    return true;
   }
  }
 }
}

Kommentare

Beliebte Posts aus diesem Blog

Angular is trash

Drawing a line after an arrow that follows a path in a Flash MovieClip