Disable interactions on x-axis

Hi, I am trying to disable the movement of an object on x-axis. I try

Code: Select all

world.addEventListener(Event.ENTER_FRAME, loop);
function loop(evt:Event):void
{
   obj.x = 0;
   obj.syncTransform();
}


I need that the obj interact only on y-axis.
The problem is when I drag on the x-axis or try to rotate pressing a key, the object don't stay in x = 0.
When I release the obj, it returns to x = 0. How can I avoid this behavior?

Thanks.
I wonder if it makes a difference that you are using Event.ENTER_FRAME instead of the box2d/WCK StepEvent.STEP.

I haven't looked much into it but in one case instead of using syncTransform,
obj.b2body.SetTransform(new V2(new_x/world.scale, new_y/world.scale), 0);
seemed to work.

Update:
Another idea. Depending on your project, you could create a physical wall (and perhaps another one on the other side) that the object cannot go through. This should constrain the movement.

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