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

dilerarc wrote:
Question: how can I set "equilibrium" distanceJoint.length in WCK?

Now distanceJoint.length is distance between j1 and j2 instances of Joint class. How can I change it?


I just tried this and was surprised to find it worked quite well (j is a WCK Joint):

Code:
j.b2joint as b2DistanceJoint).m_length += 0.1


Changing the length of a distance joint is not officially supported by Box2D though.
Its works! Thanks!
Hello again. One more probably stupid question:)

I'm trying to create a distance joint in my code (Rect is shapes.Box, Joint1 is wck.Joint, p is instance of my "actor"):

Code:
      private function mouseDownHandler(e:MouseEvent):void
      {
         if (e.target is Rect)
         {
            j1 = new Joint1;
            j2 = new Joint1;
            j1.x = p.x;
            j1.y = p.y;
            
            j2.x = mouseX;
            j2.y = mouseY;
            
            j1.target2Name = 'j2';
            j1.type = 'Distance';
            j1.frequencyHz = 1;
            j1.connectorClassName = 'ConnectorDots';
                 addChild(j2);
            addChild(j1);
         
         }
      }



Instance of Rect class was create by simple dragging in editor.

What am I doing wrong?
Maybe is there a better way to create and destroy a distance joint on mouse click (in WCK-way of course)?
Sorry for my english:)
Thanks
I've got two questions regarding WCK


1.

I made a class called Key witch extends BodyShape. Inside the Key instance i put one shapes.Box and one shapes.Circle instance.
This works, but the Key class do not registrate contact events sent from Key instance.

Key.as:
package shapes
{

import Box2DAS.*;
import Box2DAS.Collision.*;
import Box2DAS.Collision.Shapes.*;
import Box2DAS.Common.*;
import Box2DAS.Dynamics.*;
import Box2DAS.Dynamics.Contacts.*;
import Box2DAS.Dynamics.Joints.*;

import cmodule.Box2D.*;
import wck.*;

public class Key extends BodyShape {
public var contacts:ContactList;
public function Key ():void
{

// I can't set these commented properties here. It causes a runtime error
// reportBeginContact = true;
// reportEndContact = true;
// fixedRotation = true;
// super.create();
// contacts = new ContactList();
// contacts.listenTo(this);

}
}
}

Main.as:
public function handleContact(e:ContactEvent):void
{
var youCollision:MovingMan = e.fixture.m_userData as MovingMan;
var key:Key = e.other.m_userData as Key;
// Traces the bodyShapes inside the Key class, a Circle and a Box, but not the Key
trace(e.other.m_userData)

}

I hope I can use this approach, because the decompose polygon command in flash do not (seem) to work well on some shapes.


2:

I had to make a change to ScrollerChild.as to change focus object during runtime. Maybe there is a different way to do the same that I've missed?

public class ScrollerChild extends Entity {

/// Setting this to true will make the Scroller focus on this object. If you set this to true
/// for more than one ScrollerChild in a Scroller, only one will become the focus.
[Inspectable(defaultValue=false)]
public var focusOn:Boolean = false;

/**
* Tell the Scroller to focus on this object if focusOn == true.
*/
public override function create():void {
super.create();
if(focusOn) {
var s:Scroller = Util.findAncestorOfClass(this, Scroller) as Scroller;
if(s) {
s.focus = this;
}
}
}

//new method added

public function updateFocus(bool:Boolean):void {
focusOn = bool;
if(focusOn) {
var s:Scroller = Util.findAncestorOfClass(this, Scroller) as Scroller;
if(s) {
s.focus = this;
}
}
}

A big thank you to MayoButter for making WCK!
@brukernavn
i believe i have an answer for your question:

you need to override the create function in order to set any of the component inspector variables using a class.

and you start your listeners in a normal part of code, like your constructor as shown.

Code:
public function Key ():void
{
         this.listenWhileVisible(this, ContactEvent.BEGIN_CONTACT, onBeginContact);
         this.listenWhileVisible(this, ContactEvent.END_CONTACT, onEndContact);

//also available, PRE_SOLVE and POST_SOLVE

}

public override function create():void {

         reportBeginContact = true;
    reportEndContact = true;
    fixedRotation = true;
    super.create();   

}
brukernavn: What sketchbook said. Components, frustratingly, can't do anything with their inspectable properties in the constructor function. That's the point of the overriding the create() function. Plus, contact events are dispatched from the shape, not the body, so you'll have to set "reportBeginContact" on them. It is OK to listen on the body though because the events bubble up.

On setting the scroll focus: All you need to do is set the "focus" variable on the world. You could use startFocusTween() on the world if you want to ease it.
@sketchbookgames & mayobutter

Thanks for speedy reply!


I use a World subclass to process the events in my application. The solution you suggest will only dispatch contact events to the component itself right?
Below is the changed class

public class Key extends BodyShape {
public var contacts:ContactList;
public function Key ():void
{
this.listenWhileVisible(this, ContactEvent.BEGIN_CONTACT, onBeginContact);
this.listenWhileVisible(this, ContactEvent.END_CONTACT, onEndContact);

}
public override function create():void
{
reportBeginContact = true;
reportEndContact = true;
fixedRotation = true;

// I also tried to get this to dispatch events to the world class
contacts = new ContactList();
contacts.listenTo(this);

super.create();

}
private function onBeginContact(e:ContactEvent){
trace("key begin")
}
private function onEndContact(e:ContactEvent){
trace("key end")
}
}


Putting a ContactList instance in the overridden create method instead of the constructor will still only dispatch events from the components inside the Key component?
It could seem that the events are not bubbling up the displaylist? But you say they do.

Its also a little unclear why I also need to set "reportBeginContact" . The other components I use still dispatch contact events event though the reportBeginContact, reportEndContact, reportPostSolve and reportPreSolve are set to false?

I'm obviously missing something central here 🙂
@brukernavn
i've not yet tried to bubble it up to world, but i have a suggestion to get around what ever the bubble problem may be.
you could pass the event to world.

Code:
private function onBeginContact(e:ContactEvent){
   trace("key begin")   
   world.onBeginContact(e);
}

//then have a public function onBeginContact on your world extending class.

also im not sure if you need this bit
Code:
contacts = new ContactList();
contacts.listenTo(this);


also, for me, events have only ever dispatched when the appropriate boolean was set to true. (dispatching only what you need makes it run faster, since each event has to pass from C++ through Alchemy to AS3.)

@mayobutter, I think i will try to find a way to make the component variables change-able through the constructor. If i find something i will let you know for future builds.
sketchbookgames wrote:
@mayobutter, I think i will try to find a way to make the component variables change-able through the constructor. If i find something i will let you know for future builds.

When you use components Flash generates code in the background. The code does first creates the objects (the constructor is called), then sets all component properties. Essentially it does this:
Code:
var c = new MyComponent(); c.property1 = "value"; c.property2 = "other value";

Looking at that, it should be obvious why component constructors don't have access to properties. If you find a way around this, you have solved a very huge Flash problem.

I will look into contact event bubbling. But please guys... use code tags and formatting in your posts!
@mayobutter:
this is not elegant, but it is a work around for the constructor variables.
(coding standards aside, since i think very few people would choose to use a double _ )
(part of this code is to recreate the conditions the components cause)

the solution in this code revolves around using setters. when the body is not yet created, have set variables store in an Object. make sure the component is targeting the variables not the setters. then in the create function set any variables that exist in the Object.

this will require extra code for each variable, and un-doubt-ably be annoying to do and to look at. however it would resolve one piece of confusion of wck, and allow users to set variables in the constructor.


Code:
package{
   import flash.display.MovieClip;
   
   public class test extends MovieClip{
      private var __prop1:Number = 1;//default
      private var _created:Boolean = false;
      private var _propObj:Object = new Object();
      
      private function set _prop1(val:Number):void{
         if(_created){
            __prop1 = val;
         }else{
            _propObj.prop1=val;
         }
      }
      private function get _prop1():Number{
         return __prop1;
      }
      
      public function test(){
         _prop1 = 5;
         componentSetsValues();
         create();
         trace(_prop1);
      }
      private function componentSetsValues():void{
         __prop1=2;
      }
      protected function create():void{
         _created = true;
         if(_propObj.prop1){
            _prop1 = _propObj.prop1;
         }
      }
   }
}



looking forward to your opinion, either way.

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