I followed Todd's tutorials, and now I updated to the new box2d I'm getting several errors since some classess are changed or even removed like b2PolygonShapeDef.
I have spent quite some time on trying to convert it myself, but I haven't managed to fix it. And the outdated documentation didn't really help either.
The class creates box2d shapes based on given points.
Here's the code that made the magic happen in box 2.0:
The errors I'm getting are:
Could someone please help me update this class? I would rather not stick with an outdated Box2D to avoid these errors.
I have spent quite some time on trying to convert it myself, but I haven't managed to fix it. And the outdated documentation didn't really help either.
The class creates box2d shapes based on given points.
Here's the code that made the magic happen in box 2.0:
Code:
package Actors
{
//Import Libraries
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.*;
import Box2D.Common.Math.b2Vec2;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.geom.Point;
/**
* ...
* @author Jason Gidding
*/
//Arbitrary Static Actor Class
public class ArbiStaticActor extends Actor
{
//Constructor
public function ArbiStaticActor(parent:DisplayObjectContainer, location:Point, arrayOfCoords:Array, costumeVisible:Boolean) {
//Link my body with parameters
var myBody:b2Body = CreateBodyFromCoords(arrayOfCoords, location);
//Create sprite
if (costumeVisible) {
var myCostume:Sprite = CreateSpriteFromCoords(arrayOfCoords, location, parent);
} else {
myCostume = new Sprite();
}
//Costume's visibility is set to passed on property
myCostume.visible = costumeVisible;
//Pass on my body and costume to my parent
super(myBody, myCostume);
}
//Create Sprite From Coordinates
private function CreateSpriteFromCoords(arrayOfCoords:Array, location:Point, parent:DisplayObjectContainer):Sprite {
//Create new Sprite
var newSprite:Sprite = new Sprite();
//Create lineStyle
newSprite.graphics.lineStyle(2, 0x00C60F);
//For each listOfPoints in arrayOfCoords
for each (var listOfPoints:Array in arrayOfCoords) {
//Set firstPoint
var firstPoint:Point = listOfPoints[0];
//Draw line to firstPoint x, y
newSprite.graphics.moveTo(firstPoint.x, firstPoint.y);
//With green color
newSprite.graphics.beginFill(0x00C60F);
//For each newPoint in listOfPoints
for each (var newPoint:Point in listOfPoints)
{
//Set line to newPoint x, y
newSprite.graphics.lineTo(newPoint.x, newPoint.y);
}
//Draw lineTo firstPoint x, y
newSprite.graphics.lineTo(firstPoint.x, firstPoint.y);
newSprite.graphics.endFill();
}
//Place newSprite to location x, y
newSprite.x = location.x;
newSprite.y = location.y;
//Add newSprite to the parent
parent.addChild(newSprite);
//Return new sprite
return newSprite;
}
//Create Body From Coordinates
private function CreateBodyFromCoords(arrayofCoords:Array, location:Point):b2Body {
//Define Shapes
var allShapeDefs:Array = [];
//Go through each list of points in arrayofCoords
for each (var listOfPoints:Array in arrayofCoords) {
//Create new b2PolyGondDef shapes
var newShapeDef:b2PolygonDef = new b2PolygonDef();
//Set vertex count to the listofPoints length
newShapeDef.vertexCount = listOfPoints.length;
//Go through the listofPoints length
for (var i:int = 0; i < listOfPoints.length; i++)
{
//Set nextPoint to the listofPoints index
var nextPoint:Point = listOfPoints[i];
//Set vertexes to each point
b2Vec2(newShapeDef.vertices[i].Set(nextPoint.x / gameData.RATIO, nextPoint.y / gameData.RATIO));
}
//Set bounce, slide, and material
newShapeDef.density = 0;
newShapeDef.friction = 0.0;
newShapeDef.restitution = 0;
//Push newShapeDef into the allShapeDefs array
allShapeDefs.push(newShapeDef);
}
//Define a body
var arbiBodyDef:b2BodyDef = new b2BodyDef();
arbiBodyDef.position.Set(location.x / gameData.RATIO, location.y / gameData.RATIO);
//Create the body
var arbiBody:b2Body = gameData.world.CreateBody(arbiBodyDef);
//Go through each new ShapeDef in allShapeDefs
for each (var newShapeDefToAdd:b2ShapeDef in allShapeDefs)
{
//Create the shape
arbiBody.CreateShape(newShapeDefToAdd);
}
//Set Mass from Shapes
arbiBody.SetMassFromShapes();
//Return Body
return arbiBody;
}
}
}
{
//Import Libraries
import Box2D.Collision.Shapes.*;
import Box2D.Dynamics.*;
import Box2D.Common.Math.b2Vec2;
import flash.display.DisplayObjectContainer;
import flash.display.Sprite;
import flash.geom.Point;
/**
* ...
* @author Jason Gidding
*/
//Arbitrary Static Actor Class
public class ArbiStaticActor extends Actor
{
//Constructor
public function ArbiStaticActor(parent:DisplayObjectContainer, location:Point, arrayOfCoords:Array, costumeVisible:Boolean) {
//Link my body with parameters
var myBody:b2Body = CreateBodyFromCoords(arrayOfCoords, location);
//Create sprite
if (costumeVisible) {
var myCostume:Sprite = CreateSpriteFromCoords(arrayOfCoords, location, parent);
} else {
myCostume = new Sprite();
}
//Costume's visibility is set to passed on property
myCostume.visible = costumeVisible;
//Pass on my body and costume to my parent
super(myBody, myCostume);
}
//Create Sprite From Coordinates
private function CreateSpriteFromCoords(arrayOfCoords:Array, location:Point, parent:DisplayObjectContainer):Sprite {
//Create new Sprite
var newSprite:Sprite = new Sprite();
//Create lineStyle
newSprite.graphics.lineStyle(2, 0x00C60F);
//For each listOfPoints in arrayOfCoords
for each (var listOfPoints:Array in arrayOfCoords) {
//Set firstPoint
var firstPoint:Point = listOfPoints[0];
//Draw line to firstPoint x, y
newSprite.graphics.moveTo(firstPoint.x, firstPoint.y);
//With green color
newSprite.graphics.beginFill(0x00C60F);
//For each newPoint in listOfPoints
for each (var newPoint:Point in listOfPoints)
{
//Set line to newPoint x, y
newSprite.graphics.lineTo(newPoint.x, newPoint.y);
}
//Draw lineTo firstPoint x, y
newSprite.graphics.lineTo(firstPoint.x, firstPoint.y);
newSprite.graphics.endFill();
}
//Place newSprite to location x, y
newSprite.x = location.x;
newSprite.y = location.y;
//Add newSprite to the parent
parent.addChild(newSprite);
//Return new sprite
return newSprite;
}
//Create Body From Coordinates
private function CreateBodyFromCoords(arrayofCoords:Array, location:Point):b2Body {
//Define Shapes
var allShapeDefs:Array = [];
//Go through each list of points in arrayofCoords
for each (var listOfPoints:Array in arrayofCoords) {
//Create new b2PolyGondDef shapes
var newShapeDef:b2PolygonDef = new b2PolygonDef();
//Set vertex count to the listofPoints length
newShapeDef.vertexCount = listOfPoints.length;
//Go through the listofPoints length
for (var i:int = 0; i < listOfPoints.length; i++)
{
//Set nextPoint to the listofPoints index
var nextPoint:Point = listOfPoints[i];
//Set vertexes to each point
b2Vec2(newShapeDef.vertices[i].Set(nextPoint.x / gameData.RATIO, nextPoint.y / gameData.RATIO));
}
//Set bounce, slide, and material
newShapeDef.density = 0;
newShapeDef.friction = 0.0;
newShapeDef.restitution = 0;
//Push newShapeDef into the allShapeDefs array
allShapeDefs.push(newShapeDef);
}
//Define a body
var arbiBodyDef:b2BodyDef = new b2BodyDef();
arbiBodyDef.position.Set(location.x / gameData.RATIO, location.y / gameData.RATIO);
//Create the body
var arbiBody:b2Body = gameData.world.CreateBody(arbiBodyDef);
//Go through each new ShapeDef in allShapeDefs
for each (var newShapeDefToAdd:b2ShapeDef in allShapeDefs)
{
//Create the shape
arbiBody.CreateShape(newShapeDefToAdd);
}
//Set Mass from Shapes
arbiBody.SetMassFromShapes();
//Return Body
return arbiBody;
}
}
}
The errors I'm getting are:
Quote:
ArbiStaticActor.as, Line 120 1046: Type was not found or was not a compile-time constant: b2ShapeDef.
ArbiStaticActor.as, Line 88 1046: Type was not found or was not a compile-time constant: b2PolygonDef.
ArbiStaticActor.as, Line 88 1180: Call to a possibly undefined method b2PolygonDef.
ArbiStaticActor.as, Line 123 1061: Call to a possibly undefined method CreateShape through a reference with static type Box2D.Dynamics:b2Body.
ArbiStaticActor.as, Line 127 1061: Call to a possibly undefined method SetMassFromShapes through a reference with static type Box2D.Dynamics:b2Body.
ArbiStaticActor.as, Line 88 1046: Type was not found or was not a compile-time constant: b2PolygonDef.
ArbiStaticActor.as, Line 88 1180: Call to a possibly undefined method b2PolygonDef.
ArbiStaticActor.as, Line 123 1061: Call to a possibly undefined method CreateShape through a reference with static type Box2D.Dynamics:b2Body.
ArbiStaticActor.as, Line 127 1061: Call to a possibly undefined method SetMassFromShapes through a reference with static type Box2D.Dynamics:b2Body.
Could someone please help me update this class? I would rather not stick with an outdated Box2D to avoid these errors.