Is it possible to drag an object by its edge? (SOLVED)

Started by Iphigenia, Wed 08/07/2009 15:47:04

Previous topic - Next topic

Iphigenia

The objects in my game are draggable and use fairly large sprites. Currently when I click on them, they 'jump', centring on the cursor. Can some wise soul tell me if it is possible to prevent this behaviour, and have objects dragged from the exact point the mouse clicks on them?

GuyAwesome

#1
Yes, it's entirely possible. Are you still using KhrisMUC's code I linked you to in that other thread?

If so, or you've gone for something similar, you just need to change the lines that 'centre' the object on the mouse (the if (ob != null) condition at the bottom) to one that check the offset of the mouse from the Object's coords, and updates based on that. Something like (untested)
Code: ags

  if (ob != null) {
    int XOff  = mouse.x - ob.X;
    int YOff = mouse.y - ob.Y;
    ob.Baseline = game.room_height;
    ob.SetPosition(mouse.x-XOff, mouse.y+YOff);
  }


As I said, untested - in particular, I'm not sure I've got the + and - the right way round in the last line, and I don't think it'll work properly in scrolling rooms. Also, it might be better to declare X/YOff outside of repeatedly_execute and set them once, when the Object is first 'grabbed'. But hey, it's a start ... (And the question was 'is it possible', not 'how do you do it' :))

EDIT:
OK, so you WILL have to declare X/YOff outside of rep_ex and set them once, otherwise the object seems to jitter something awful (or not move at all). I'm assuming that's the reason, 'cause this way fixes it: Declare XOff and YOff, before room_RepExec. Add a line to the condition for 'mouse button not down' setting XOff to -1, and use
Code: ags

  if (ob != null) {
    if (XOff == -1) {
      XOff = mouse.x - ob.X;
      YOff = mouse.y - ob.Y;
    }
    ob.Baseline = game.room_height;
    ob.SetPosition(mouse.x-XOff, mouse.y-YOff);
  }


EDIT 2:
Heh, good timing with my edit I see. Glad it was too far wrong to start with...

Iphigenia

Brilliant. That works perfectly (as you suspected X/YOff must be declared outside of the repeatedly_execute).
Thanks again.

SMF spam blocked by CleanTalk