Dragging objects with Box2D Flash

This is ideally the next part after Understanding pixels and meters with Box2D and how to select an object with mouse – part 2, but it’s so important understanding how to drag objects with Box2D that I decided to change the title.

Anyway, in this uncommented example (a commented one will follow) you can select any crate with the mouse and drag it.

Changing at line 89 the mouse_joint.maxForce value will affect gameplay.

This is the source code:

And this is the result:

Enjoy dragging and dropping crates with your mouse.

Download the source code.

  • Very cool!

  • Terry

    Nice tutorial I was having an idea to create a game by dragging the boxes and it helps a lot. :)

  • Colin Diam

    Can you please make a turoial how to make game like mytheria

  • Peter

    COOOOOOOOOLLLLL!!!!!! :D

  • zap

    excelente

  • Pingback: Goodbye AS2… : Emanuele Feronato()

  • Won’t Work

    1046: Type was not found or was not a compile-time constant: b2Body. “public function GetBodyAtMouse(includeStatic:Boolean=false):b2Body {“

  • vijay

    hi,I’m very much interested to create games in flash can u help me

  • zap

    Im trying to put a top limit…

    //arriba
    boxDef.SetAsBox(8.5, 14.5);
    bodyDef.position.Set(8.5, -0.5);
    boxDef.friction = 1;
    boxDef.density = 0;
    bodyDef.userData = new floor();
    bodyDef.userData.width = 8.5 * 2 * pixels_in_a_meter;
    bodyDef.userData.height = 0.5 * 2 * pixels_in_a_meter;
    addChild(bodyDef.userData);
    body = m_world.CreateBody(bodyDef);
    body.CreateShape(boxDef);

    …the limit is in a correct position but my box have a bad movement, what´s the problem?

  • zap

    solucionado!!! Thanksssssss a lot

  • ben

    Fixed :
    worked well if addin my b2body a m_userData when created rather than addin userData to the body Def…
    now everything works fine….
    Is a empty b2Body always considered as null ???
    Strange ????

  • bigLama

    Please can you fully explain the code iside the function function GetBodyAtMouse.
    Thanks

  • Godfrey Cao

    thankssssss a lot !
    regarding this function (GetBodyAtMouse), i did not understand all of them .
    especially line 112. what is it used for?

  • Trevor

    Great!

    Now where is Box3D???

    I am SERIOUS!!!!

  • Elf

    What faster using GetBodyAtMouse() or
    private function on_mouse_down(e:MouseEvent):void
    {
    for(var body:b2Body=world.GetBodyList();body;body=body.m_next)
    {
    if(body.m_userData==e.target) break;
    }
    if (body)
    {
    …do something…
    }
    }
    ?
    but my method depend on object count.

  • Despecrew

    Great !!

    I would like to know how can I delineate the area of drag and drop ?

    PS: sorry for my bad benglish.. :/

  • Vlad

    Emanuele, might I ask why you always put everything in the same class ? is it for the simplicity of the tutorial ?

  • Emanuele Feronato

    Having a single script to show is easier to display and understand for newbies.

  • Jaleel

    Emanuele, great job. thanks for u r code its really helps us to move forward with box2d. keep it up.

  • I am trying to integrate this into my own Box2D project where i have about 20 objects that are just falling to the ground, but when I click on an object that is falling it will just STOP – it won’t let me drag it…do you know why that might be?

  • Lilx

    The Vector class doesn’t work with Flash CS3 and Flash player 9 :/

  • Alex

    i get this error:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at HelloBoxWorld/GetBodyAtMouse()

    the line that flash says is wrong is this one:

    var count:int = m_world.Query(aabb, shapes, k_maxCount);

    can someone help

  • MKEason

    I met a very strange problem!

    If I compile this source in Flex Builder with Flex SDK without any change except embed those necessary images with meta tag, then everything stops in the begining.

    I tried other sources too, they all failed! Have no idea why. Can everybody help me on this?

    Thanks in advanced.

  • Pingback: Volvo S60 Pinball game – Things I learnt « Tahir Ahmed's thoughts()

  • Ciao Emanuele,

    Grazie per questi tutorial! Sto cercando di aggiornare un file vecchio di 2 anni, e ho problemi con il metodo GetShapeList() e GetBody()


    mousePVec.Set(xMouse, yMouse);

    var starShape : b2Shape = star.GetShapeList();
    var inside : Boolean = starShape.TestPoint(starShape.GetBody().GetXForm(), mousePVec);

    Sai aiutmarmi perfavore? Grazie.

  • can anyone explain to me why i am getting this error:

    Line 100 1046: Type was not found or was not a compile-time constant: b2Body.

    I am kind of stuck, the source code does not work!!! help please

  • Crisdava
  • Hi,

    I was just wondering if you have an updated version(2.1a) of the mouseJoint.

  • marlo

    This does not work with cs5.5 can you please help or advise??