PDA

View Full Version : [opinion needed] best way to create/render multiple movieclips?



ligaa
February 17th, 2010, 05:10 PM
Hey all :)

I'm starting work on a new game that has a lot of shapes dynamically generated on the stage. They're all the same shape, just different sizes. I figured the best way to do this would be an array of movieclips, but then I remembered hearing that things run much better when you clone things out of Bitmap Data. Finding good examples of either solution has been difficult, so I thought I'd try my own.

Here's what I have so far:

package {
import flash.display.MovieClip;
import flash.display.Shape;
import flash.display.Bitmap;
import flash.display.BitmapData;

public class Magnetron extends MovieClip {
private var circleSize:Number;
private var maxSize:Number;
private var numPullers:Number;
private var numPushers:Number;

public function Magnetron():void {
createPullers();
}

private function createPullers():void {
numPullers = 10;
maxSize = 50;

for(var i:Number = 0; i < numPullers; i++){
circleSize = Math.round(Math.random()*maxSize);
if(circleSize < 10){
circleSize = 10;
}

var circle:MovieClip = new MovieClip();
circle.name = "circle"+i;
circle.graphics.beginFill(0x000000);
circle.graphics.drawCircle(circleSize,circleSize,c ircleSize);
circle.graphics.endFill();

var pullerBMData:BitmapData = new BitmapData(circleSize*2, circleSize*2, true, 0xFFFFFF);
pullerBMData.draw(circle);
var puller:Bitmap = new Bitmap(pullerBMData);
puller.x = Math.round(Math.random()*stage.stageWidth);
puller.y = Math.round(Math.random()*stage.stageHeight);
stage.addChild(puller);
}
}

//================================================== ===================
}
}

Well, it works. It puts a bunch of black circles on the stage in random spots, and there's transparency around the circles. However, here are my concerns:


Is this the right way to copy things out of BitmapData?
If not, what is the right way? Or, what's a better way?
If so, will this actually save on loading/processing?
How will this method affect hit detection? And what's the best form of hit detection to use here?
Is it still a good idea to load the different shapes into an array to keep track of them? Or is there a better way to keep track?

A hundred questions, and I've just started. :P I thank anyone who can offer some insight into this!

rumblesushi
February 19th, 2010, 02:47 PM
This is 100% not the right way to do it :D

You shouldn't be using movieclips at all. Replace what you would be using movieclips for with your own custom class, with variables x/y/scale and anything else you need.

You could use BMF to draw these objects to the same graphics object every frame.

Collision detection would work the same way it does for anything else, you just use the coordinates of your object for the collision, then render the object using it's coordinates.

You don't need movieclips, and you don't need draw either.

Look into copyPixels. It's fast, but if your shapes are animated or scale dynamically, you'll of course need a prebuilt sprite sheet, or frame sheet.

mxrider108
February 23rd, 2010, 06:49 PM
This thread may be relevant to you if you decide to use bitmap rendering (very fast!): http://www.kirupa.com/forum/showthread.php?t=343909

ligaa
March 4th, 2010, 02:05 PM
Thank you rumblesushi and mxrider108! (And sorry for the late response!)

The information you both provided is very helpful. But I'm not sure it even needs to get as complicated as blitting and all. I'm essentially going to have three objects: a ship, a black sphere, and a red sphere. The spheres are rendered in different sizes but are all essentially the same thing.

So I'll go with BitmapData/Bitmap and copyPixels and see how that works. :)