Is it possible to create a Spritemap from Bitmapdata?


(meganmorgangames) #1

Basically, what I am wanting to do is take a static image, place it at a semi-random position in a Bitmapdata object, then use that object for a frame in a Spritemap.

If it can’t be done, I can always create an Entity that will do the animation manually, but right now all of my effects are passed on through Spritemaps, so this would be incredibly convenient. Any ideas?


(JP Mortiboys) #2

Of course it’s possible - here’s a quick example

// This is a stand-in for your small bitmap you want to position
// using an 8x8 pixel yellow square for testing purposes
var smallBmp:BitmapData = new BitmapData(8, 8, true, 0xFFFF00FF);

// Define the size of the individual frames
var frameWidth:int = 64, frameHeight:int = 64;

// Number of frames
var numFrames:int = 10;

// Number of little objects per frame
var itemsPerFrame:int = 8;

// Create stage bitmap
var largeBmp:BitmapData = new BitmapData(numFrames * frameWidth, frameHeight, true, 0);

// Some variables
var iFrame:int; // current frame index
var iItem:int; // current item index
// For each frame...
for (iFrame = 0; iFrame < numFrames; iFrame++) {
  // For each item to place
  for (iItem = 0; iItem < numItems; iItem++) {
    // We're going to select the x and y positions randomly among the available space
    // but bias the y positions based on frame index
    FP.point.x = FP.rand(frameWidth - itemWidth) + frameWidth * iFrame;
    FP.point.y = (iFrame * itemHeight / (numFrames-1)) + FP.random * FP.random * FP.random * frameHeight;
    FP.point.y = FP.clamp(FP.point.y, 0, frameHeight - itemHeight);
    // Stick the small bitmap inside the frame
    largeBmp.copyPixels(smallBmp, smallBmp.rect, FP.point);
  }
}

// All the frames are generated, we can create a spritemap

var spritemap:Spritemap = new Spritemap(largeBmp, frameWidth, frameHeight);
// do something with the spritemap

(meganmorgangames) #3

Perfection. I had to do a little searching to figure out how to make my spritesheet into bitmapdata, but iwht that done it all looks great. Little more work in other classes, and I can test it :smile: Thank you.