Hey guys, I asked this on the flashcoders list, but immediately found these forums - and I'll probably get a much better answer here!
So, I like the engine after some playing, but I wanted to get things a little more encapsulated so I don't have to do lines and lines of creating bodies, and setting mass, etc.
What I'd like to do is:
var b:Block = new Block( width, height, etc etc etc), where Block extends b2Body.
It all worked pretty well when it extended b2BodyDef, but just tried to extend b2Body today, and my objects suddenly aren't moving (I'm looking in debug mode).
It kinda seems like creating a new b2Body isn't the same as creating it from the world outside the class? And then I tried super = world.CreateBody(_bodydef as b2BodyDef);, but that's not compiler friendly - I'm not sure how I'd do that!
So any suggestions on how to add my body to the world inside my class?
I'm making it extend b2Body and not b2BodyDef, because when I loop through and get m_bodylist, I'm getting an array of bodies, and want to call body[i].update on them.
thanks!
ben
package objects
{
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Collision.Shapes.b2ShapeDef;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2World;
import flash.display.Sprite;
public class Block extends b2Body implements IObject
{
public function Block( world:b2World, w:Number, h:Number, density:Number = 1.0, friction:Number = .5, restitution:Number = 0.2 ) {
_bodydef = new b2BodyDef();
_bodydef.userData = new Sprite();
_bodydef.userData.graphics.lineStyle(.5,0x000000);
_bodydef.userData.graphics.beginFill(0xFFFF00);
_bodydef.userData.graphics.drawRect(0,0,w,h);
_bodydef.userData.graphics.endFill();
_poly.SetAsBox(w/60, h/60);
_poly.density = density;
_poly.friction = friction;
_poly.restitution = restitution;
super( _bodydef, world );
//super = world.CreateBody(_bodydef as b2BodyDef);
super.CreateShape(this.shape);
super.SetMassFromShapes();
}
public function update():void {} // not implemented yet - doing stuff outside in the actual enterframe handler
public function get display():Sprite { return _bodydef.userData; }
public function get shape():b2ShapeDef { return _poly; }
public function get bodydef():b2BodyDef { return _bodydef; }
public function set x( val:int ):void { _bodydef.position.x = val/60; display.x = val; }
public function set y( val:int ):void { _bodydef.position.y = val/30; display.y = val; }
public function get x():int { return _bodydef.position.x*60; }
public function get y():int { return _bodydef.position.y*60; }
private var _poly:b2PolygonDef = new b2PolygonDef();
private var _bodydef:b2BodyDef;
}
}
So, I like the engine after some playing, but I wanted to get things a little more encapsulated so I don't have to do lines and lines of creating bodies, and setting mass, etc.
What I'd like to do is:
var b:Block = new Block( width, height, etc etc etc), where Block extends b2Body.
It all worked pretty well when it extended b2BodyDef, but just tried to extend b2Body today, and my objects suddenly aren't moving (I'm looking in debug mode).
It kinda seems like creating a new b2Body isn't the same as creating it from the world outside the class? And then I tried super = world.CreateBody(_bodydef as b2BodyDef);, but that's not compiler friendly - I'm not sure how I'd do that!
So any suggestions on how to add my body to the world inside my class?
I'm making it extend b2Body and not b2BodyDef, because when I loop through and get m_bodylist, I'm getting an array of bodies, and want to call body[i].update on them.
thanks!
ben
package objects
{
import Box2D.Collision.Shapes.b2PolygonDef;
import Box2D.Collision.Shapes.b2ShapeDef;
import Box2D.Dynamics.b2Body;
import Box2D.Dynamics.b2BodyDef;
import Box2D.Dynamics.b2World;
import flash.display.Sprite;
public class Block extends b2Body implements IObject
{
public function Block( world:b2World, w:Number, h:Number, density:Number = 1.0, friction:Number = .5, restitution:Number = 0.2 ) {
_bodydef = new b2BodyDef();
_bodydef.userData = new Sprite();
_bodydef.userData.graphics.lineStyle(.5,0x000000);
_bodydef.userData.graphics.beginFill(0xFFFF00);
_bodydef.userData.graphics.drawRect(0,0,w,h);
_bodydef.userData.graphics.endFill();
_poly.SetAsBox(w/60, h/60);
_poly.density = density;
_poly.friction = friction;
_poly.restitution = restitution;
super( _bodydef, world );
//super = world.CreateBody(_bodydef as b2BodyDef);
super.CreateShape(this.shape);
super.SetMassFromShapes();
}
public function update():void {} // not implemented yet - doing stuff outside in the actual enterframe handler
public function get display():Sprite { return _bodydef.userData; }
public function get shape():b2ShapeDef { return _poly; }
public function get bodydef():b2BodyDef { return _bodydef; }
public function set x( val:int ):void { _bodydef.position.x = val/60; display.x = val; }
public function set y( val:int ):void { _bodydef.position.y = val/30; display.y = val; }
public function get x():int { return _bodydef.position.x*60; }
public function get y():int { return _bodydef.position.y*60; }
private var _poly:b2PolygonDef = new b2PolygonDef();
private var _bodydef:b2BodyDef;
}
}