AS3 2.1 Port (Alchemy), WCK Update and Kinematic Demos

Hi there,

I'm trying to use thw WCK Input class, and for all that I know de lu() and kp() function ain't working. kd() is :/

Don't know why ^^.
helios: Just supply <0, 0> as the gravity to the world, then apply gravity before the timestep based on your own logic. If you are using WCK, you can override this method on wck.World:
Code:
      /**
       * Get gravity at a specific point, for a specific body & bodyshape (if a bodyshape exists for the body). The
       * body & bodyshape are passed so that this function can be overriden to provide different gravity for
       * different objects! This can also be overriden to implement circular gravity, capsule gravity, etc., or alter
       * each non-static non-sleeping gravity-enabled body in some other way.
       */
      public function getGravityFor(p:V2, b:b2Body = null, b2:BodyShape = null):V2 {


Xaren: Make sure you're calling kp() or ku() before Input.clear() is called each timestep. Depending on how your listeners are setup the keys might be cleared before you get a chance to check them. If you find it is genuinely broken please let me know but I've been using kp() myself a lot successfully.
Code:
// Called Each Frame
public function gameLoop(e:Event):void {   
   if (!GamePaused) {
      if (Input.ku("SPACE"))  trace("Special move");
      if (!RaceFinished && !playerDown) { checkKey(); }
      if (!playerDown) SetupVals.world.Step( 1.0 / SetupVals.FRAMERATE, SetupVals.ITERATION); // Step Method
      updateEvent(); // Update Method
      updateActor(); // Draw Method
   }
}


Here's the code ( the if (Input.ku("SPACE")) {... would actually be uin the checkKey method...)

But it's actually not working :/
Running "Test Movie" in the default demo.fla file (no modifications on my part) produces a .swf that just keeps looping through all the available worlds.

Is there something else that needs to be done in demo.fla in order for everything to work properly?

I'm using Flash CS4.

Suggestion: it would be nice if, rather than being completely dependent on the Flash IDE, there was a way to import graphical assets through an XML schema, or by embedding them directly into code like this: [Embed(source='../../data/MyPhysicsCrate.png')]

It would allow game developers to use a wide variety of tools to create their assets, and on multiple platforms (The Flex SDK is open source).

Also, taking a look at the flixel engine architecture would probably help; the flixel FlxSprite class represents a game object very nicely (imo), because it abstracts the lower level details, and provides a standard update function where the developer can write all his logic code in a relatively straightforward manner.

I don't know how difficult it would be to do something like that with your port, but ideally, there would be some-sort of a "GameObject" class with a method where one could write code like this:

Code:
if (hitObject(lava)){
    die();
}


PS: The demos are impressive.
Quote:
Running "Test Movie" in the default demo.fla file (no modifications on my part) produces a .swf that just keeps looping through all the available worlds.


You probably don't have the latest update to Flash CS4. It's free and fixes a bug with large projects.

Quote:
I don't know how difficult it would be to do something like that with your port, but ideally, there would be some-sort of a "GameObject" class with a method where one could write code like this


It's easy enough to add a game loop to a BodyShape. Just extend BodyShape and override the create function like so:

Code:
public override function create():void {
super.create();
listenWhileVisible(world, World.TIME_STEP, gameLoop);
}
I have been trying to get this thing up and running, but without luck so far. I'm using Flash Builder 4, beta 2 and have tried it in Flex Builder 3 as well.

I'm adding the Box2D.swc to my library path in Flash Builder, and then I go on to try and import it in my code using "import Box2DAS.*" but the definition could not be found. Autocompletion does not list it either - it only lists "cmodule.Box2D", but I'm not sure what that one does.

Could someone explain the steps needed to get Box2D - alchemy version - in a Flash Builder AS3-project?
Playing around with the demo I am trying to add joint on the fly to make BoxMan drag boxes...

I added following property to BoxMan class:
Code:
public var joint:Joint;


and modified handleContact method:
Code:
      public function handleContact(e:ContactEvent):void {
         var p:Pellet = e.other.m_userData as Pellet;
         if(p) {
            Util.addChildAtPosOf(world, new FX1(), p); 
            p.remove();
         }
         var box:Box20 = e.other.m_userData as Box20;
         if (box) {
            joint = new Joint();
            joint.bodyShape1=this;
            joint.bodyShape2=box;
            joint.type='Distance';
            joint.create();
         }
         
      }


But I'v got errors... I am new to box2d, so please anybody tell me where I am wrong...

EDIT: just changes the code, but still not wroking:
Code:
      public function handleContact(e:ContactEvent):void {
         var p:Pellet = e.other.m_userData as Pellet;
         if(p) {
            Util.addChildAtPosOf(world, new FX1(), p); 
            p.remove();
         }
         var box:Box20 = e.other.m_userData as Box20;
         if (box) {
            var craneJoint=new Joint2();
            Util.addChildAtPosOf(world, craneJoint, box);
            craneJoint.target2Object=this;
            craneJoint.type="Distance";
            craneJoint.create();
            
            trace(craneJoint);
         }
         
      }

Why the target2Object can not be associated?
ChronoDK wrote:
Could someone explain the steps needed to get Box2D - alchemy version - in a Flash Builder AS3-project?


Yeah i have to say im really interested in this as well. I have been trying to implement a sping and a box all day and just cant work it out. lol. Latest error is Main Thread (Suspended: ReferenceError: Error #1065: Variable ConnectorSpring is not defined.) even though it is available!
I've been playing around with Flash Builder 4. I just added the "wck" folder to my source path, linked to the Box2DAS.swc, and a simple project that just instantiates a b2World compiled fine.

On another note - Is it bad practice to use the "fl.*" packages? Are those not included in the open source Flex framework? I haven't been able to get the high level WCK stuff running yet because Flex is choking on that package.

evilsatan: "addChild" automatically calls "create()". Call " Util.addChildAtPosOf(world, craneJoint, box);" AFTER everything else.
Thanks! But I found anothr mistake in my code. Jonts cannot be created during callbacks, because the world is locked. I am not familiar with box2d, so I will post my solution for all box2d beginners. I don't know is it the best solution.

I'v added following properties on BoxMan class:
Code:
      public var boxDrag=false;      
      public var jointDrag=false;

added at the end of handleContact method following:
Code:
         var temp = e.other.m_userData;
         if (temp as Circle40 && !(jointDrag as Joint)) {
            boxDrag=temp;
         }         

and modified the parseInput method:
Code:
         var drop:Boolean = Input.kp('S','DOWN');
         if (drop && (jointDrag as Joint)) {
            jointDrag.destroy();
            jointDrag=false;
            boxDrag=false;
         }
         if (boxDrag as Circle40 && !(jointDrag as Joint)) {
            jointDrag = new Joint();
            jointDrag.bodyShape1=this;
            jointDrag.bodyShape2=boxDrag;
            jointDrag.type='Revolute';
            jointDrag.connectorClassName='ConnectorDots';
            
            Util.addChildAtPosOf(world, jointDrag, this);
            
         }

Original thread: https://box2d.org/forum/viewtopic.php?t=4105