Get Graphics Data from world map sprite and add random spots to the land mass.
Task: Randomly drop dots onto the land mass of a world map sprite.
How to do it:
Transform Sprite into BitmapData and check color values of randomly selected pixel, also check surrounding area for dots too close.
How to do it:
Transform Sprite into BitmapData and check color values of randomly selected pixel, also check surrounding area for dots too close.
// get object from libraryprivate var worldland:WorldLand = new WorldLand();
private var landpixels:BitmapData;
// make bitmap out of sprite
function makeBitmapData():void {
landpixels = new BitmapData(worldland.width, worldland.height);
landpixels.draw(worldland);
}// define dotPoints in order to keep track of space taken by previous dots
private var dotPoints:Array = new Array();// burp function checks random coordinate on map
function burp():void {
var point:Point = randPos();
while(isWater(point))point=randPos();
//trace("found land: " + point.x + "x" + point.y);
var randomDot:Dot = new Dot();
randomDot.x = point.x;
randomDot.y = point.y;addChild(randomDot);
dotPoints.push(point);
}
function randPos():Point {
return new Point(Math.floor(Math.random()*worldland.width), Math.floor(Math.random()*worldland.height));
}
//this means 60 pixels around point are to be kept free
private var pxrange:uint = 30;function isWater(p:Point):Boolean {//first check if spot and surrounding area is already used
for(var h:uint = 0; h < dotPoints.length; h++){
var op:Point = dotPoints[h];
if(op.x > p.x-pxrange&&op.x 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 waterif(blue.toString(16)=="cc"){ return false; }else{ return true; } }
Kommentare
Kommentar veröffentlichen