In theory you can generate a block of random perlin noise by using a random number to define the 'random seed' argument of the noise constructor. You can then take a cross section (i.e. a row of pixels) from the block and, depending on how bright or dark that pixel is, you can position your shape or line according to the pixel’s value.
The possibilities of this technique are endless.. (animating noise, moving objects around depending on the pixels darkness etc) and I will definitely be playing with noise some more in the future. For now I’ve posted a quick example of how to use this process to create a random platform landscape, quickly and cleanly.
package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
/**
* @author Dan Bennett
* @email dan.bennett86@yahoo.com
*/
[SWF(width="900", height="700", backgroundColor="#ffffff")]
public class PerlinNoisePlatform extends Sprite
{
private static const BOX_WIDTH:int = 100;
private static const BOX_HEIGHT:int = 100;
private static const RANGE:int = 300;
private var xPos:Number = 1;
private var darkness:Number = 1;
public function PerlinNoisePlatform()
{
var noise:BitmapData = createNoise();
drawPlatform(noise);
}
private function createNoise():BitmapData
{
var randomSeed:int = Math.random() * 100;
var contents:BitmapData = new BitmapData(BOX_WIDTH, BOX_HEIGHT);
contents.perlinNoise(BOX_WIDTH, BOX_HEIGHT,6,randomSeed,false,false,7,true);
return contents;
}
private function drawPlatform(data:BitmapData):void
{
// Pick a starting point to get our row of pixels
var xStart:Number = Math.floor(Math.random() * BOX_WIDTH);
for(var i:int = 0;i<BOX_HEIGHT;i++)
{
var shape:Shape = new Shape();
shape.graphics.moveTo(xPos, darkness * RANGE);
if(i % 4 == 0)
{
this.darkness = data.getPixel( xStart , i) / 0xFFFFFF;
}
this.xPos+=10;
shape.graphics.lineStyle(1,0x000000);
shape.graphics.lineTo(xPos, darkness * RANGE);
this.addChild(shape);
}
}
}
}