Initializing a joint on bodies created in separate functions

Discuss issues specific to the Flash/AS3 port of Box2D
gramcracker
Posts: 21
Joined: Wed Feb 25, 2015 10:36 am

Initializing a joint on bodies created in separate functions

Postby gramcracker » Wed Mar 04, 2015 5:48 pm

I'm having a heck of a time with is. I looked through the manual. I've looked many other places, asked colleagues etc.
I create objects in my game like this:

*_object is a vector <Object>
*Object is a class (.as file)
*The object at the end is the texture.

_object(push.Object(createPlayer(x,y,etc,etc), PlayerImage()));
_object(push.Object(createEnemy(x,y,etc,etc), EnemyImage()));


Now I need to create parts of one character in separate functions and connect them with a joint.

_object(push.Object(createPlayerHead(x,y,etc,etc), PlayerHead()));
_object(push.Object(createPlayerBody(x,y,etc,etc), PlayerBody()));

No matter where the joint is initialized, it will not work. I tried in the functions, outside, in its own function, in the main class, in a separate file, everywhere.

It does work if you combine the functions into one. But I need them separate.

How can I Initialize a joint between two bodies when the bodies are created in separate functions in the method shown above?

Thanks for taking the time to read this.

gramcracker
Posts: 21
Joined: Wed Feb 25, 2015 10:36 am

Re: Initializing a joint on bodies created in separate funct

Postby gramcracker » Thu Mar 19, 2015 2:16 pm

I solved it with the help of Allan Bishop.
You access the items by referencing one index back and two indexes back.

Code: Select all


_balls.push(new Ball(createPlayerBody(), new PlayerBody()));
_balls.push(new Ball(createPlayer(), new PlayerImage()));
         
var ballA: Ball = _balls[_balls.length-1];
var ballB: Ball = _balls[_balls.length-2];

jd.Initialize(ballB.body, ballA.body, new b2Vec2(startX / PIXELS_TO_METRE, (startY + 10) / PIXELS_TO_METRE));
_world.CreateJoint(jd);   



Return to “Flash”



Who is online

Users browsing this forum: No registered users and 4 guests