The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.

ForumsProgramming ForumBox2d problem

2 3994
theshadow170
offline
theshadow170
2 posts
Nomad

Hello! I am new to programming using the Flash Box2D port. I am trying to develop a ragdoll fighter game and I have encountered a deadly problem on the way. I have the ragdoll clas which includes the bodies, fixtures and joint of the ragdoll(see below code), but I do not know how to fully delete an instance of the class. Please help me with this problem. Thanks in advance!
Code:
package
{
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.Joints.*;
import Box2D.Dynamics.Contacts.*;
import Box2D.Common.*;
import Box2D.Common.Math.*;
import Box2D.Common.Math.b2Vec2;
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.Stage;


public class Ragdoll extends MovieClip
{

public var jointDef:b2RevoluteJointDef=new b2RevoluteJointDef();
public var headBody:b2Body;
public var upperTorsoBody:b2Body;
public var headBodyDef:b2BodyDef;
public var midTorsoBody:b2Body;
public var lowerTorsoBody:b2Body;
public var upperArmLeftBody:b2Body;
public var upperArmRightBody:b2Body;
public var lowerArmLeftBody:b2Body;
public var lowerArmRightBody:b2Body;
public var upperLegLeftBody:b2Body;
public var upperLegRightBody:b2Body;
public var lowerLegLeftBody:b2Body;
public var lowerLegRightBody:b2Body;
public var life = 100;
public var canTakeDamage:Boolean = true;
public var counter:int = 0;
public var awakeIn:int = 240;
public var sleeps:Boolean = false;
public var canDealDamage:Boolean = true;

public var headFixture:b2Fixture;
public var upperTorsoFixture:b2Fixture;
public var midTorsoFixture:b2Fixture;
public var lowerTorsoFixture:b2Fixture;
public var upperArmLeftFixture:b2Fixture;
public var upperArmRightFixture:b2Fixture;
public var lowerArmLeftFixture:b2Fixture;
public var lowerArmRightFixture:b2Fixture;
public var upperLegLeftFixture:b2Fixture;
public var upperLegRightFixture:b2Fixture;
public var lowerLegLeftFixture:b2Fixture;
public var lowerLegRightFixture:b2Fixture;
public var AI = false;

public var upperTorsoJoint:b2Joint;
public var midTorsoJoint:b2Joint;
public var lowerTorsoJoint:b2Joint;
public var upperArmLeftJoint:b2Joint;
public var upperArmRightJoint:b2Joint;
public var lowerArmLeftJoint:b2Joint;
public var lowerArmRightJoint:b2Joint;
public var upperLegLeftJoint:b2Joint;
public var upperLegRightJoint:b2Joint;
public var lowerLegLeftJoint:b2Joint;
public var lowerLegRightJoint:b2Joint;

var force:b2Vec2 = new b2Vec2(0.0,0.0);


public function Ragdoll(posX:Number,posY:Number,isStatic:Boolean,isAI:Boolean)
{
// constructor code
if (isAI)
{
AI = true;
}

var density:Number = 1.0;
var restitution:Number = 0.0;
var friction:Number = 0.3;



/* Create the segments of the body
-----------------------------------
-----------------------------------
-----------------------------------
-----------------------------------
---------------------------------*/

//Create the head of the Ragdoll

var headShape:b2CircleShape = new b2CircleShape(12.5 / RagdollFighter.RATIO);
headBodyDef=new b2BodyDef();
var headFixtureDef:b2FixtureDef=new b2FixtureDef();
headFixtureDef.shape = headShape;
headFixtureDef.density = density;
headFixtureDef.friction = friction;
headFixtureDef.restitution = restitution;
headBodyDef.position.Set(posX/RagdollFighter.RATIO,posY/RagdollFighter.RATIO);
if (! isStatic)
{
headBodyDef.type = b2Body.b2_dynamicBody;
}
else
{
headBodyDef.type = b2Body.b2_staticBody;
}
headBody = RagdollFighter.world.CreateBody(headBodyDef);
headFixture = headBody.CreateFixture(headFixtureDef);


//Create the upper-torso

var upperTorsoShape:b2PolygonShape=new b2PolygonShape();
upperTorsoShape.SetAsBox(15/RagdollFighter.RATIO,10/RagdollFighter.RATIO);
var upperTorsoBodyDef:b2BodyDef=new b2BodyDef();
var upperTorsoFixtureDef:b2FixtureDef=new b2FixtureDef();
upperTorsoFixtureDef.shape = upperTorsoShape;
upperTorsoFixtureDef.density = density;
upperTorsoFixtureDef.friction = friction;
upperTorsoFixtureDef.restitution = restitution;
upperTorsoBodyDef.position.Set(posX/RagdollFighter.RATIO,(posY+28)/RagdollFighter.RATIO);
upperTorsoBodyDef.type = b2Body.b2_dynamicBody;
upperTorsoBody = RagdollFighter.world.CreateBody(upperTorsoBodyDef);
upperTorsoFixture = upperTorsoBody.CreateFixture(upperTorsoFixtureDef);


//Create the mid-torso

var midTorsoShape:b2PolygonShape=new b2PolygonShape();
midTorsoShape.SetAsBox(15/RagdollFighter.RATIO,10/RagdollFighter.RATIO);
var midTorsoBodyDef:b2BodyDef=new b2BodyDef();
var midTorsoFixtureDef:b2FixtureDef=new b2FixtureDef();
midTorsoFixtureDef.shape = midTorsoShape;
midTorsoFixtureDef.density = density;
midTorsoFixtureDef.friction = friction;
midTorsoFixtureDef.restitution = restitution;
midTorsoBodyDef.position.Set(posX/RagdollFighter.RATIO,(posY+43)/RagdollFighter.RATIO);
midTorsoBodyDef.type = b2Body.b2_dynamicBody;
midTorsoBody = RagdollFighter.world.CreateBody(midTorsoBodyDef);
midTorsoFixture = midTorsoBody.CreateFixture(midTorsoFixtureDef);


//Create the lower-torso

var lowerTorsoShape:b2PolygonShape=new b2PolygonShape();
lowerTorsoShape.SetAsBox(15/RagdollFighter.RATIO,10/RagdollFighter.RATIO);
var lowerTorsoBodyDef:b2BodyDef=new b2BodyDef();
var lowerTorsoFixtureDef:b2FixtureDef=new b2FixtureDef();
lowerTorsoFixtureDef.shape = lowerTorsoShape;
lowerTorsoFixtureDef.density = density;
lowerTorsoFixtureDef.friction = friction;
lowerTorsoFixtureDef.restitution = restitution;
lowerTorsoBodyDef.position.Set(posX/RagdollFighter.RATIO,(posY+58)/RagdollFighter.RATIO);
lowerTorsoBodyDef.type = b2Body.b2_dynamicBody;
lowerTorsoBody = RagdollFighter.world.CreateBody(lowerTorsoBodyDef);
lowerTorsoFixture = lowerTorsoBody.CreateFixture(lowerTorsoFixtureDef);


//Create the left upper-arm

var upperArmLeftShape:b2PolygonShape=new b2PolygonShape();
upperArmLeftShape.SetAsBox(18/RagdollFighter.RATIO,6.5/RagdollFighter.RATIO);
var upperArmLeftBodyDef:b2BodyDef=new b2BodyDef();
var upperArmLeftFixtureDef:b2FixtureDef=new b2FixtureDef();
upperArmLeftFixtureDef.shape = upperArmLeftShape;
upperArmLeftFixtureDef.density = density;
upperArmLeftFixtureDef.friction = friction;
upperArmLeftFixtureDef.restitution = restitution;
upperArmLeftBodyDef.position.Set((posX-30)/RagdollFighter.RATIO,(posY+20)/RagdollFighter.RATIO);
upperArmLeftBodyDef.type = b2Body.b2_dynamicBody;
upperArmLeftBody = RagdollFighter.world.CreateBody(upperArmLeftBodyDef);
upperArmLeftFixture = upperArmLeftBody.CreateFixture(upperArmLeftFixtureDef);


//Create the right upper-arm

var upperArmRightShape:b2PolygonShape=new b2PolygonShape();
upperArmRightShape.SetAsBox(18/RagdollFighter.RATIO,6.5/RagdollFighter.RATIO);
var upperArmRightBodyDef:b2BodyDef=new b2BodyDef();
var upperArmRightFixtureDef:b2FixtureDef=new b2FixtureDef();
upperArmRightFixtureDef.shape = upperArmRightShape;
upperArmRightFixtureDef.density = density;
upperArmRightFixtureDef.friction = friction;
upperArmRightFixtureDef.restitution = restitution;
upperArmRightBodyDef.position.Set((posX+30)/RagdollFighter.RATIO,(posY+20)/RagdollFighter.RATIO);
upperArmRightBodyDef.type = b2Body.b2_dynamicBody;
upperArmRightBody = RagdollFighter.world.CreateBody(upperArmRightBodyDef);
upperArmRightFixture = upperArmRightBody.CreateFixture(upperArmRightFixtureDef);


//Create the left lower-arm

var lowerArmLeftShape:b2PolygonShape=new b2PolygonShape();
lowerArmLeftShape.SetAsBox(17/RagdollFighter.RATIO,6/RagdollFighter.RATIO);
var lowerArmLeftBodyDef:b2BodyDef=new b2BodyDef();
var lowerArmLeftFixtureDef:b2FixtureDef=new b2FixtureDef();
lowerArmLeftFixtureDef.shape = lowerArmLeftShape;
lowerArmLeftFixtureDef.density = density;
lowerArmLeftFixtureDef.friction = friction;
lowerArmLeftFixtureDef.restitution = restitution;
lowerArmLeftBodyDef.position.Set((posX-57)/RagdollFighter.RATIO,(posY+20)/RagdollFighter.RATIO);
lowerArmLeftBodyDef.type = b2Body.b2_dynamicBody;
lowerArmLeftBody = RagdollFighter.world.CreateBody(lowerArmLeftBodyDef);
lowerArmLeftFixture = lowerArmLeftBody.CreateFixture(lowerArmLeftFixtureDef);


//Create the right lower-arm

var lowerArmRightShape:b2PolygonShape=new b2PolygonShape();
lowerArmRightShape.SetAsBox(17/RagdollFighter.RATIO,6/RagdollFighter.RATIO);
var lowerArmRightBodyDef:b2BodyDef=new b2BodyDef();
var lowerArmRightFixtureDef:b2FixtureDef=new b2FixtureDef();
lowerArmRightFixtureDef.shape = lowerArmRightShape;
lowerArmRightFixtureDef.density = density;
lowerArmRightFixtureDef.friction = friction;
lowerArmRightFixtureDef.restitution = restitution;
lowerArmRightBodyDef.position.Set((posX+57)/RagdollFighter.RATIO,(posY+20)/RagdollFighter.RATIO);
lowerArmRightBodyDef.type = b2Body.b2_dynamicBody;
lowerArmRightBody = RagdollFighter.world.CreateBody(lowerArmRightBodyDef);
lowerArmRightFixture = lowerArmRightBody.CreateFixture(lowerArmRightFixtureDef);


//Create the left upper-leg

var upperLegLeftShape:b2PolygonShape=new b2PolygonShape();
upperLegLeftShape.SetAsBox(7.5/RagdollFighter.RATIO,22/RagdollFighter.RATIO);
var upperLegLeftBodyDef:b2BodyDef=new b2BodyDef();
var upperLegLeftFixtureDef:b2FixtureDef=new b2FixtureDef();
upperLegLeftFixtureDef.shape = upperLegLeftShape;
upperLegLeftFixtureDef.density = density;
upperLegLeftFixtureDef.friction = friction;
upperLegLeftFixtureDef.restitution = restitution;
upperLegLeftBodyDef.position.Set((posX-8)/RagdollFighter.RATIO,(posY+85)/RagdollFighter.RATIO);
upperLegLeftBodyDef.type = b2Body.b2_dynamicBody;
upperLegLeftBody = RagdollFighter.world.CreateBody(upperLegLeftBodyDef);
upperLegLeftFixture = upperLegLeftBody.CreateFixture(upperLegLeftFixtureDef);


//Create the right upper-leg

var upperLegRightShape:b2PolygonShape=new b2PolygonShape();
upperLegRightShape.SetAsBox(7.5/RagdollFighter.RATIO,22/RagdollFighter.RATIO);
var upperLegRightBodyDef:b2BodyDef=new b2BodyDef();
var upperLegRightFixtureDef:b2FixtureDef=new b2FixtureDef();
upperLegRightFixtureDef.shape = upperLegRightShape;
upperLegRightFixtureDef.density = density;
upperLegRightFixtureDef.friction = friction;
upperLegRightFixtureDef.restitution = restitution;
upperLegRightBodyDef.position.Set((posX+8)/RagdollFighter.RATIO,(posY+85)/RagdollFighter.RATIO);
upperLegRightBodyDef.type = b2Body.b2_dynamicBody;
upperLegRightBody = RagdollFighter.world.CreateBody(upperLegRightBodyDef);
upperLegRightFixture = upperLegRightBody.CreateFixture(upperLegRightFixtureDef);


//Create the left lower-leg

var lowerLegLeftShape:b2PolygonShape=new b2PolygonShape();
lowerLegLeftShape.SetAsBox(6/RagdollFighter.RATIO,20/RagdollFighter.RATIO);
var lowerLegLeftBodyDef:b2BodyDef=new b2BodyDef();
var lowerLegLeftFixtureDef:b2FixtureDef=new b2FixtureDef();
lowerLegLeftFixtureDef.shape = lowerLegLeftShape;
lowerLegLeftFixtureDef.density = density;
lowerLegLeftFixtureDef.friction = friction;
lowerLegLeftFixtureDef.restitution = restitution;
lowerLegLeftBodyDef.position.Set((posX-8)/RagdollFighter.RATIO,(posY+120)/RagdollFighter.RATIO);
lowerLegLeftBodyDef.type = b2Body.b2_dynamicBody;
lowerLegLeftBody = RagdollFighter.world.CreateBody(lowerLegLeftBodyDef);
lowerLegLeftFixture = lowerLegLeftBody.CreateFixture(lowerLegLeftFixtureDef);


//Create the right lower-leg

var lowerLegRightShape:b2PolygonShape=new b2PolygonShape();
lowerLegRightShape.SetAsBox(6/RagdollFighter.RATIO,20/RagdollFighter.RATIO);
var lowerLegRightBodyDef:b2BodyDef=new b2BodyDef();
var lowerLegRightFixtureDef:b2FixtureDef=new b2FixtureDef();
lowerLegRightFixtureDef.shape = lowerLegRightShape;
lowerLegRightFixtureDef.density = density;
lowerLegRightFixtureDef.friction = friction;
lowerLegRightFixtureDef.restitution = restitution;
lowerLegRightBodyDef.position.Set((posX+8)/RagdollFighter.RATIO,(posY+120)/RagdollFighter.RATIO);
lowerLegRightBodyDef.type = b2Body.b2_dynamicBody;
lowerLegRightBody = RagdollFighter.world.CreateBody(lowerLegRightBodyDef);
lowerLegRightFixture = lowerLegRightBody.CreateFixture(lowerLegRightFixtureDef);


/* Create the joints of the body
--------------------------------
--------------------------------
--------------------------------
--------------------------------
------------------------------*/
jointDef.enableLimit = true;

//Create the head->upper-torso

jointDef.lowerAngle=-40/(180/Math.PI);
jointDef.upperAngle=40/(180/Math.PI);
jointDef.Initialize(upperTorsoBody,headBody,new b2Vec2(posX/RagdollFighter.RATIO,(posY+15)/RagdollFighter.RATIO));
upperTorsoJoint=RagdollFighter.world.CreateJoint(jointDef);

//left upper-arm->upper-torso;

jointDef.lowerAngle=-85/(180/Math.PI);
jointDef.upperAngle=130/(180/Math.PI);
jointDef.Initialize(upperTorsoBody,upperArmLeftBody,new b2Vec2((posX-18)/RagdollFighter.RATIO,(posY+20)/RagdollFighter.RATIO));
upperArmLeftJoint=RagdollFighter.world.CreateJoint(jointDef);


//right upper-arm->upper-torso;

jointDef.lowerAngle=-110/(180/Math.PI);
jointDef.upperAngle=85/(180/Math.PI);
jointDef.Initialize(upperTorsoBody,upperArmRightBody,new b2Vec2((posX+18)/RagdollFighter.RATIO,(posY+20)/RagdollFighter.RATIO));
upperArmRightJoint=RagdollFighter.world.CreateJoint(jointDef);

//left lower-arm->upper-arm;

jointDef.lowerAngle=-110/(180/Math.PI);
jointDef.upperAngle=10/(180/Math.PI);
jointDef.Initialize(upperArmLeftBody,lowerArmLeftBody,new b2Vec2((posX-45)/RagdollFighter.RATIO,(posY+20)/RagdollFighter.RATIO));
lowerArmLeftJoint=RagdollFighter.world.CreateJoint(jointDef);

//right lower-arm->upper-arm;

jointDef.lowerAngle=-10/(180/Math.PI);
jointDef.upperAngle=110/(180/Math.PI);
jointDef.Initialize(upperArmRightBody,lowerArmRightBody,new b2Vec2((posX+45)/RagdollFighter.RATIO,(posY+20)/RagdollFighter.RATIO));
lowerArmRightJoint=RagdollFighter.world.CreateJoint(jointDef);

//mid-torso->upper-torso;

jointDef.lowerAngle=-15/(180/Math.PI);
jointDef.upperAngle=15/(180/Math.PI);
jointDef.Initialize(upperTorsoBody,midTorsoBody,new b2Vec2((posX)/RagdollFighter.RATIO,(posY+35)/RagdollFighter.RATIO));
midTorsoJoint=RagdollFighter.world.CreateJoint(jointDef);

//lower-torso->midd-torso;

jointDef.Initialize(midTorsoBody,lowerTorsoBody,new b2Vec2((posX)/RagdollFighter.RATIO,(posY+50)/RagdollFighter.RATIO));
lowerTorsoJoint=RagdollFighter.world.CreateJoint(jointDef);

//left upper-leg->lower-torso;

jointDef.lowerAngle=-25/(180/Math.PI);
jointDef.upperAngle=70/(180/Math.PI);
jointDef.Initialize(lowerTorsoBody,upperLegLeftBody,new b2Vec2((posX-8)/RagdollFighter.RATIO,(posY+72)/RagdollFighter.RATIO));
upperLegLeftJoint=RagdollFighter.world.CreateJoint(jointDef);

//right upper-leg->lower-torso;

jointDef.lowerAngle=-70/(180/Math.PI);
jointDef.upperAngle=25/(180/Math.PI);
jointDef.Initialize(lowerTorsoBody,upperLegRightBody,new b2Vec2((posX+8)/RagdollFighter.RATIO,(posY+72)/RagdollFighter.RATIO));
upperLegRightJoint=RagdollFighter.world.CreateJoint(jointDef);

//left lower-leg->upper-leg;

jointDef.lowerAngle=-25/(180/Math.PI);
jointDef.upperAngle=115/(180/Math.PI);
jointDef.Initialize(upperLegLeftBody,lowerLegLeftBody,new b2Vec2((posX-8)/RagdollFighter.RATIO,(posY+105)/RagdollFighter.RATIO));
lowerLegLeftJoint=RagdollFighter.world.CreateJoint(jointDef);

//right lower-leg->upper-leg;

jointDef.lowerAngle=-115/(180/Math.PI);
jointDef.upperAngle=25/(180/Math.PI);
jointDef.Initialize(upperLegRightBody,lowerLegRightBody,new b2Vec2((posX+8)/RagdollFighter.RATIO,(posY+105)/RagdollFighter.RATIO));
lowerLegRightJoint=RagdollFighter.world.CreateJoint(jointDef);

headBody.SetUserData(2);
upperTorsoBody.SetUserData(1);
midTorsoBody.SetUserData(1);
lowerTorsoBody.SetUserData(1);
upperArmLeftBody.SetUserData(1);
upperArmRightBody.SetUserData(1);
upperLegLeftBody.SetUserData(1);
upperLegRightBody.SetUserData(1);
lowerLegLeftBody.SetUserData(0);
lowerLegRightBody.SetUserData(0);
lowerArmLeftBody.SetUserData(0);
lowerArmRightBody.SetUserData(0);

headFixture.SetUserData(this);
upperTorsoFixture.SetUserData(this);
midTorsoFixture.SetUserData(this);
lowerTorsoFixture.SetUserData(this);
upperArmLeftFixture.SetUserData(this);
upperArmRightFixture.SetUserData(this);
upperLegLeftFixture.SetUserData(this);
upperLegRightFixture.SetUserData(this);
lowerArmLeftFixture.SetUserData(this);
lowerArmRightFixture.SetUserData(this);
lowerLegLeftFixture.SetUserData(this);
lowerLegRightFixture.SetUserData(this);





}

  • 2 Replies
driejen
offline
driejen
486 posts
Nomad

There wasnt really any reason for you to post the contents of that class since you won't be deleting an instance from within the same class.

To delete an instance, first go to its parent and use removeChild() and then find every reference to the instance and set the value to null. For example, if you've created a ragdoll like this;
var raggy:Ragdoll = new Ragdoll;
addChild(raggy);
Then you would remove the instance like this;
removeChild(raggy);
raggy=null;

If you store a reference elsewhere, like this for example;
var raggy:Ragdoll = new Ragdoll;
addChild(raggy);
box.ragdollHolder = raggy;
Then you would have to set those references to null also;
removeChild(raggy);
raggy=null;
box.ragdollHolder = null;

If you have event listeners attached to the instance, you would want to remove those also with removeEventListener().

It may be that Box2D is stores a reference to your ragdolls, unfortunately I don't know how Box2D works so you would have to go read the documentation if so and find out how the makers of it want you to handle the deletion. Manually removing instances when you are using something like Box2D might be a bad idea if there are functions in Box2D that are designed to handle that for you.

theshadow170
offline
theshadow170
2 posts
Nomad

There are for bodies and fixtures, but I have to also clear the references. Thanks for the help, I will try this later when I come back from school. I appreciate your help!

Showing 1-2 of 2