Pour décrire mon problème, je vais reprendre ce sur quoi je planche en ce moment, à savoir box2D.
Il était une fois un monde que j'ai peuplé avec un cube.
Pour ce faire, j'ai déclaré dans ma classe Main
PhysicValues.world = new b2World(new b2Vec2(0, 9.8), true);
Pour ceux qui voudraient savoir, voici la classe PhysicValues qui a en charge les critères spécifiques au monde
public class PhysicValues
{
public static const SCALE:Number = 30;
public static const RADTODEG:Number = 180 / Math.PI;
public static const DEGTORAD:Number = Math.PI / 180;
private static var _world:b2World;
static public function get world():b2World
{
return _world;
}
static public function set world(value:b2World):void
{
_world = value;
}
}
public class Actors
{
private var _body:b2Body;
public function Actors(position:b2Vec2, isPolygon:Boolean, hx:Number, hy:Number)
{
var bodyDef:b2BodyDef = new b2BodyDef();
bodyDef.position.Set(position.x / PhysicValues.SCALE, position.y / PhysicValues.SCALE);
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.userData = this;
_body = PhysicValues.world.CreateBody(bodyDef);
(isPolygon) ? doPolygon(hx, hy) : doCircle();
}
private function doCircle():void
{
trace('todo');
}
private function doPolygon(hx,hy):void
{
var shape:b2PolygonShape = new b2PolygonShape();
shape.SetAsBox(hx / PhysicValues.SCALE, hy / PhysicValues.SCALE);
var fixtureDef:b2FixtureDef = new b2FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 1;
fixtureDef.friction = .3;
_body.CreateFixture(fixtureDef);
}
}
puis de retour dans la classe Main, je crée mes objets
_allBodies.push(new Actors(new b2Vec2(stage.stageWidth >> 1, 120), true, 30, 30));
_allBodies.push(new Actors(new b2Vec2((stage.stageWidth >> 1) + 70, 120), true, 30, 30));
jusque là, tout fonctionne bien.
Maintenant, mon problème, c'est que j'ai besoin d'utiliser mes objets Actors comme des véritables b2Body et là, pas moyen.
J'ai essayé plusieurs transtypages tels que
var bodyA:b2body = _allBodies[_allBodies.length - 1] as b2Body;
Bon j'ai réussi à obtenir ce que je voulais en créant les objets dans Main sans passer par Actors mais j'aurai aimé garder cette approche POO.
Quelques avis ?