Hi,
Moving on from my 'PLEASE HELP - skinning a body' post where people were very helpful, I have finally found a way by adapting the HelloWorld.fla and have created a circle on a string there, and have succesfully managed to skin it using the code to skin in helloworld.as
But in the hello world.as there is no functions for dragging the items.mousejoints.
I have tried using the code from the test.as in the phystest.fla collection, but to no luck
this is my code and you can see the mouse functions I added at the end. The mousekill I had to quote (nor need it ) as it was causing and error in my new flash CS4 (yay!).
Can anyone see what I'm doing wrong. I just want o be able to drag the hanging circle thing around!
Thanks in advance!
Joe
Moving on from my 'PLEASE HELP - skinning a body' post where people were very helpful, I have finally found a way by adapting the HelloWorld.fla and have created a circle on a string there, and have succesfully managed to skin it using the code to skin in helloworld.as
But in the hello world.as there is no functions for dragging the items.mousejoints.
I have tried using the code from the test.as in the phystest.fla collection, but to no luck
Code:
package{
import flash.display.Sprite;
import flash.events.Event;
// Classes used in this example
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.Joints.*
import Box2D.Dynamics.Contacts.*
import flash.events.Event;
import flash.display.*;
import flash.text.*;
import General.*
public class HelloWorld extends Sprite{
public function HelloWorld(){
// Add event for main loop
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
// Creat world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(100.0, 100.0);
// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2(0.0, 25.0);
// Allow bodies to sleep
var doSleep:Boolean = true;
// Construct a world object
m_world = new b2World(worldAABB, gravity, doSleep);
/*// set debug draw
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
addChild(dbgSprite);
dbgDraw.m_sprite = dbgSprite;
dbgDraw.m_drawScale = 30.0;
dbgDraw.m_fillAlpha = 0.0;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = 0xFFFFFFFF;
m_world.SetDebugDraw(dbgDraw);*/
// Vars used to create bodies
var body:b2Body;
var bodyDef:b2BodyDef;
var boxDef:b2PolygonDef;
var circleDef:b2CircleDef;
// Add ground body
bodyDef = new b2BodyDef();
//bodyDef.position.Set(15, 19);
bodyDef.position.Set(10, 21);
//bodyDef.angle = 0.1;
boxDef = new b2PolygonDef();
boxDef.SetAsBox(30, 3);
boxDef.friction = 0.3;
boxDef.density = 0; // static bodies require zero density
/*circleDef = new b2CircleDef();
circleDef.radius = 10;
circleDef.restitution = 0.2*/
// Add sprite to body userData
bodyDef.userData = new PhysGround();
//bodyDef.userData = new PhysCircle();
bodyDef.userData.width = 30 * 2 * 30;
bodyDef.userData.height = 30 * 2 * 3;
addChild(bodyDef.userData);
body = m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
//body.CreateShape(circleDef);
body.SetMassFromShapes();
/// joint def ????
var jd:b2RevoluteJointDef = new b2RevoluteJointDef();
jd.lowerAngle = -15 / (180/Math.PI);
jd.upperAngle = 15 / (180/Math.PI);
jd.enableLimit = true;
var ground:b2Body = m_world.GetGroundBody();
var prevBody:b2Body = ground;
var anchor:b2Vec2 = new b2Vec2();
// Add some objects
for (var i:int = 1; i < 20; i++){
bodyDef = new b2BodyDef();
bodyDef.position.x = 10;//Math.random() * 15 + 5;
bodyDef.position.y = (i * 0.06 * 9);
var rX:Number = Math.random() + 0.5;
var rY:Number = Math.random() + 0.5;
// Box
if (i<=18){//Math.random() < 0.5
boxDef = new b2PolygonDef();
boxDef.SetAsBox(0.06, .3);
boxDef.density = 40.0;
boxDef.friction = 0.5;
boxDef.restitution = 1;
bodyDef.userData = new PhysBox();
bodyDef.userData.width = 0.06 * 2 * 30;
bodyDef.userData.height = .3 * 2 * 30;
body = m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
// add joint???
anchor.Set(10, (i * 0.06 * 9));
jd.Initialize(prevBody, body, anchor);
m_world.CreateJoint(jd);
prevBody = body;
} else {
bodyDef.position.y = bodyDef.position.y + 1.1 ;
circleDef = new b2CircleDef();
circleDef.radius = 1.5;
circleDef.density = 1.0;
circleDef.friction = 0.5;
circleDef.restitution = 0.2
bodyDef.userData = new PhysCircle();
bodyDef.userData.width = 1.5 * 2 * 30;
bodyDef.userData.height = 1.5 * 2 * 30;
body = m_world.CreateBody(bodyDef);
body.CreateShape(circleDef);
// add joint???
anchor.Set(10, (i * 0.065 * 9) + 1.1);
jd.Initialize(prevBody, body, anchor);
m_world.CreateJoint(jd);
prevBody = body;
}
body.SetMassFromShapes();
addChild(bodyDef.userData);
}
}
public function Update(e:Event):void{
m_world.Step(m_timeStep, m_iterations);
// Go through body list and update sprite positions/rotations
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is Sprite){
bb.m_userData.x = bb.GetPosition().x * 30;
bb.m_userData.y = bb.GetPosition().y * 30;
bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
// Update mouse joint
UpdateMouseWorld();
//MouseDestroy();
MouseDrag();
}
}
}
public var m_world:b2World;
public var m_iterations:int = 10;
public var m_timeStep:Number = 1.0/30.0;
//======================
// Member Data
//======================
//public var m_world:b2World;
public var m_bomb:b2Body;
public var m_mouseJoint:b2MouseJoint;
//public var m_iterations:int = 10;
//public var m_timeStep:Number = 1/30;
public var m_physScale:Number = 30;
// world mouse position
static public var mouseXWorldPhys:Number;
static public var mouseYWorldPhys:Number;
static public var mouseXWorld:Number;
static public var mouseYWorld:Number;
// Sprite to draw in to
//public var m_sprite:Sprite;
//======================
// Update mouseWorld
//======================
public function UpdateMouseWorld():void {
mouseXWorldPhys = (Input.mouseX)/m_physScale;
mouseYWorldPhys = (Input.mouseY)/m_physScale;
mouseXWorld = (Input.mouseX);
mouseYWorld = (Input.mouseY);
}
//======================
// Mouse Drag
//======================
public function MouseDrag():void {
// mouse press
if (Input.mouseDown && !m_mouseJoint) {
var body:b2Body = GetBodyAtMouse();
trace("mouse press");
if (body) {
var md:b2MouseJointDef = new b2MouseJointDef();
md.body1 = m_world.GetGroundBody();
md.body2 = body;
md.target.Set(mouseXWorldPhys, mouseYWorldPhys);
md.maxForce = 300.0 * body.GetMass();
md.timeStep = m_timeStep;
m_mouseJoint = m_world.CreateJoint(md) as b2MouseJoint;
body.WakeUp();
}
}
// mouse release
if (!Input.mouseDown) {
if (m_mouseJoint) {
m_world.DestroyJoint(m_mouseJoint);
m_mouseJoint = null;
}
}
// mouse move
if (m_mouseJoint) {
var p2:b2Vec2 = new b2Vec2(mouseXWorldPhys, mouseYWorldPhys);
m_mouseJoint.SetTarget(p2);
}
}
//======================
// Mouse Destroy
//======================
/*public function MouseDestroy():void {
// mouse press
if (!Input.mouseDown && Input.isKeyPressed(68)) {
var body:b2Body = GetBodyAtMouse(true);
if (body) {
m_world.DestroyBody(body);
return;
}
}
}*/
//======================
// GetBodyAtMouse
//======================
private var mousePVec:b2Vec2 = new b2Vec2();
public function GetBodyAtMouse(includeStatic:Boolean=false):b2Body {
// Make a small box.
mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
var aabb:b2AABB = new b2AABB();
aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);
// Query the world for overlapping shapes.
var k_maxCount:int = 10;
var shapes:Array = new Array();
var count:int = m_world.Query(aabb, shapes, k_maxCount);
var body:b2Body = null;
for (var i:int = 0; i < count; ++i) {
if (shapes[i].GetBody().IsStatic() == false || includeStatic) {
var tShape:b2Shape = shapes[i] as b2Shape;
var inside:Boolean = tShape.TestPoint(tShape.GetBody().GetXForm(), mousePVec);
if (inside) {
body = tShape.GetBody();
break;
}
}
}
return body;
}
}
}
import flash.display.Sprite;
import flash.events.Event;
// Classes used in this example
import Box2D.Dynamics.*;
import Box2D.Collision.*;
import Box2D.Collision.Shapes.*;
import Box2D.Common.Math.*;
import Box2D.Dynamics.Joints.*
import Box2D.Dynamics.Contacts.*
import flash.events.Event;
import flash.display.*;
import flash.text.*;
import General.*
public class HelloWorld extends Sprite{
public function HelloWorld(){
// Add event for main loop
addEventListener(Event.ENTER_FRAME, Update, false, 0, true);
// Creat world AABB
var worldAABB:b2AABB = new b2AABB();
worldAABB.lowerBound.Set(-100.0, -100.0);
worldAABB.upperBound.Set(100.0, 100.0);
// Define the gravity vector
var gravity:b2Vec2 = new b2Vec2(0.0, 25.0);
// Allow bodies to sleep
var doSleep:Boolean = true;
// Construct a world object
m_world = new b2World(worldAABB, gravity, doSleep);
/*// set debug draw
var dbgDraw:b2DebugDraw = new b2DebugDraw();
var dbgSprite:Sprite = new Sprite();
addChild(dbgSprite);
dbgDraw.m_sprite = dbgSprite;
dbgDraw.m_drawScale = 30.0;
dbgDraw.m_fillAlpha = 0.0;
dbgDraw.m_lineThickness = 1.0;
dbgDraw.m_drawFlags = 0xFFFFFFFF;
m_world.SetDebugDraw(dbgDraw);*/
// Vars used to create bodies
var body:b2Body;
var bodyDef:b2BodyDef;
var boxDef:b2PolygonDef;
var circleDef:b2CircleDef;
// Add ground body
bodyDef = new b2BodyDef();
//bodyDef.position.Set(15, 19);
bodyDef.position.Set(10, 21);
//bodyDef.angle = 0.1;
boxDef = new b2PolygonDef();
boxDef.SetAsBox(30, 3);
boxDef.friction = 0.3;
boxDef.density = 0; // static bodies require zero density
/*circleDef = new b2CircleDef();
circleDef.radius = 10;
circleDef.restitution = 0.2*/
// Add sprite to body userData
bodyDef.userData = new PhysGround();
//bodyDef.userData = new PhysCircle();
bodyDef.userData.width = 30 * 2 * 30;
bodyDef.userData.height = 30 * 2 * 3;
addChild(bodyDef.userData);
body = m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
//body.CreateShape(circleDef);
body.SetMassFromShapes();
/// joint def ????
var jd:b2RevoluteJointDef = new b2RevoluteJointDef();
jd.lowerAngle = -15 / (180/Math.PI);
jd.upperAngle = 15 / (180/Math.PI);
jd.enableLimit = true;
var ground:b2Body = m_world.GetGroundBody();
var prevBody:b2Body = ground;
var anchor:b2Vec2 = new b2Vec2();
// Add some objects
for (var i:int = 1; i < 20; i++){
bodyDef = new b2BodyDef();
bodyDef.position.x = 10;//Math.random() * 15 + 5;
bodyDef.position.y = (i * 0.06 * 9);
var rX:Number = Math.random() + 0.5;
var rY:Number = Math.random() + 0.5;
// Box
if (i<=18){//Math.random() < 0.5
boxDef = new b2PolygonDef();
boxDef.SetAsBox(0.06, .3);
boxDef.density = 40.0;
boxDef.friction = 0.5;
boxDef.restitution = 1;
bodyDef.userData = new PhysBox();
bodyDef.userData.width = 0.06 * 2 * 30;
bodyDef.userData.height = .3 * 2 * 30;
body = m_world.CreateBody(bodyDef);
body.CreateShape(boxDef);
// add joint???
anchor.Set(10, (i * 0.06 * 9));
jd.Initialize(prevBody, body, anchor);
m_world.CreateJoint(jd);
prevBody = body;
} else {
bodyDef.position.y = bodyDef.position.y + 1.1 ;
circleDef = new b2CircleDef();
circleDef.radius = 1.5;
circleDef.density = 1.0;
circleDef.friction = 0.5;
circleDef.restitution = 0.2
bodyDef.userData = new PhysCircle();
bodyDef.userData.width = 1.5 * 2 * 30;
bodyDef.userData.height = 1.5 * 2 * 30;
body = m_world.CreateBody(bodyDef);
body.CreateShape(circleDef);
// add joint???
anchor.Set(10, (i * 0.065 * 9) + 1.1);
jd.Initialize(prevBody, body, anchor);
m_world.CreateJoint(jd);
prevBody = body;
}
body.SetMassFromShapes();
addChild(bodyDef.userData);
}
}
public function Update(e:Event):void{
m_world.Step(m_timeStep, m_iterations);
// Go through body list and update sprite positions/rotations
for (var bb:b2Body = m_world.m_bodyList; bb; bb = bb.m_next){
if (bb.m_userData is Sprite){
bb.m_userData.x = bb.GetPosition().x * 30;
bb.m_userData.y = bb.GetPosition().y * 30;
bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
// Update mouse joint
UpdateMouseWorld();
//MouseDestroy();
MouseDrag();
}
}
}
public var m_world:b2World;
public var m_iterations:int = 10;
public var m_timeStep:Number = 1.0/30.0;
//======================
// Member Data
//======================
//public var m_world:b2World;
public var m_bomb:b2Body;
public var m_mouseJoint:b2MouseJoint;
//public var m_iterations:int = 10;
//public var m_timeStep:Number = 1/30;
public var m_physScale:Number = 30;
// world mouse position
static public var mouseXWorldPhys:Number;
static public var mouseYWorldPhys:Number;
static public var mouseXWorld:Number;
static public var mouseYWorld:Number;
// Sprite to draw in to
//public var m_sprite:Sprite;
//======================
// Update mouseWorld
//======================
public function UpdateMouseWorld():void {
mouseXWorldPhys = (Input.mouseX)/m_physScale;
mouseYWorldPhys = (Input.mouseY)/m_physScale;
mouseXWorld = (Input.mouseX);
mouseYWorld = (Input.mouseY);
}
//======================
// Mouse Drag
//======================
public function MouseDrag():void {
// mouse press
if (Input.mouseDown && !m_mouseJoint) {
var body:b2Body = GetBodyAtMouse();
trace("mouse press");
if (body) {
var md:b2MouseJointDef = new b2MouseJointDef();
md.body1 = m_world.GetGroundBody();
md.body2 = body;
md.target.Set(mouseXWorldPhys, mouseYWorldPhys);
md.maxForce = 300.0 * body.GetMass();
md.timeStep = m_timeStep;
m_mouseJoint = m_world.CreateJoint(md) as b2MouseJoint;
body.WakeUp();
}
}
// mouse release
if (!Input.mouseDown) {
if (m_mouseJoint) {
m_world.DestroyJoint(m_mouseJoint);
m_mouseJoint = null;
}
}
// mouse move
if (m_mouseJoint) {
var p2:b2Vec2 = new b2Vec2(mouseXWorldPhys, mouseYWorldPhys);
m_mouseJoint.SetTarget(p2);
}
}
//======================
// Mouse Destroy
//======================
/*public function MouseDestroy():void {
// mouse press
if (!Input.mouseDown && Input.isKeyPressed(68)) {
var body:b2Body = GetBodyAtMouse(true);
if (body) {
m_world.DestroyBody(body);
return;
}
}
}*/
//======================
// GetBodyAtMouse
//======================
private var mousePVec:b2Vec2 = new b2Vec2();
public function GetBodyAtMouse(includeStatic:Boolean=false):b2Body {
// Make a small box.
mousePVec.Set(mouseXWorldPhys, mouseYWorldPhys);
var aabb:b2AABB = new b2AABB();
aabb.lowerBound.Set(mouseXWorldPhys - 0.001, mouseYWorldPhys - 0.001);
aabb.upperBound.Set(mouseXWorldPhys + 0.001, mouseYWorldPhys + 0.001);
// Query the world for overlapping shapes.
var k_maxCount:int = 10;
var shapes:Array = new Array();
var count:int = m_world.Query(aabb, shapes, k_maxCount);
var body:b2Body = null;
for (var i:int = 0; i < count; ++i) {
if (shapes[i].GetBody().IsStatic() == false || includeStatic) {
var tShape:b2Shape = shapes[i] as b2Shape;
var inside:Boolean = tShape.TestPoint(tShape.GetBody().GetXForm(), mousePVec);
if (inside) {
body = tShape.GetBody();
break;
}
}
}
return body;
}
}
}
this is my code and you can see the mouse functions I added at the end. The mousekill I had to quote (nor need it ) as it was causing and error in my new flash CS4 (yay!).
Can anyone see what I'm doing wrong. I just want o be able to drag the hanging circle thing around!
Thanks in advance!
Joe