Hey, I hadn't seen this post before. I've already made my own version of something very like this. It works fairly well. I hadn't thought to use scaleX and scaleY to determine the actual width and height of the movieclips; I use a complicated bit of code:
Code:
function findExtents(){
var box = this.getChildAt(0);
var pt1:Point = new Point(box.getChildByName("tLeft").x,box.getChildByName("tLeft").y); //Location of the top left corner
var pt2:Point = new Point(box.getChildByName("tRight").x,box.getChildByName("tRight").y); //Loc of top right corner
var pt3:Point = new Point(box.getChildByName("bRight").x,box.getChildByName("bRight").y); //Loc of bottom right corner
pt1 = box.localToGlobal(pt1); //Global coord transform
pt2 = box.localToGlobal(pt2);
pt3 = box.localToGlobal(pt3);
wid = dist(pt1,pt2); //Find the transformed width/height
hght = dist(pt2,pt3);
}
I have two classes, CollisionEngine and CollisionField. CollisionField just holds all the objects and initializes them like this:
Code:
public function CollisionField(){
for(var i=0;i<numChildren;i++){
child = getChildAt(i)
if(child is VisObject){ //All the objects extend VisObject; some children are joints and other things that don't, are handled separately, and are put in different arrays.
platforms.push(child);
}
}
CollisionEngine loops through the platforms array:
Code:
for (var j in platforms ){
bodyDef = new b2BodyDef();
bodyDef.position.x = platforms[j].x*scale;
bodyDef.position.y = platforms[j].y*scale;
bodyDef.angularDamping = 0.01;
bodyDef.AddShape(platforms[j].collideObj);
bodyDef.rotation = platforms[j].rotation*3.14159/180;
bodyDef.userData = platforms[j];
platforms[j].collideObj = m_world.CreateBody(bodyDef);
for(var n in forces){
if(platforms[j].hitTestObject(forces[n])){//forces[n].pt1.x,forces[n].pt1.y,true)){
trace("Force:"+platforms[j]);
platforms[j].collideObj.ApplyImpulse(forces[n].calcForce(),new b2Vec2(forces[n].x,forces[n].y))
}
}
// trace(platforms[j]+":"+platforms[j].dispObj);
}
Each object just creates a shape, not a bodydef, so that's done separately. I probably ought to put that into the VisObject class, to be honest. Overall I like your solution a lot better, I'll try and fiddle around with mine and rearrange it a bit. I still like having a separate "engine" class, though, so I can load in different CollisionFields (at least in theory) while having all the same logic running over it. I hadn't thought of using components to add editable properties like you have, that's really cool. I was wondering how I would be able to set properties of individual objects; my only idea was to use the movieclips' names to store info and then parse it in code. I would be rather ugly, and I never bothered.