Dragging an object on the screen...
BlitzMax Forums/BlitzMax Beginners Area/Dragging an object on the screen...
| ||
Hi everyone, I really didn't want help for this because I though it would be quite easy and it should but something's wrong somewhere and to me I dont see an error but there must be if it's not working... so yeah. Basicly, in my object type code I made a rectangle with lines and so I wana be able to drag it around. It should be as easy as checking if the left mouse is down then checking last point of origin and comparing it with the current MouseX and MouseY location and then you move it accordigly. here's the piece of code that should be doing this (notice the code to make it follow the cursor isnt made yet, I was testing for if it's right or left of the point of origin(lastx) then it moves left or right by 1 but it only goes right.) Local mx:Int=MouseX() Local my:Int=MouseY() If(mx>Xpos)And(mx<Xpos+Width)And(my>Ypos)And(my<Ypos+Height)Then ' DRAG WINDOW Local lastx:Int Local lasty:Int ' Get First X&Y Loc If MouseHit(1)Then lastx=mx mtest2=mx End If ' On Dragging If MouseDown(1) mtest="true" If(lastx<mx)Then Xpos:+1 EndIf If(lastx>mx)Then Xpos:-1 EndIf Else mtest="false" End If EndIf mtest is drawn in my draw method for testing purpose aswell as mtest2. Xpos and Ypos are the current top left position of the object. |
| ||
Hi. Your code seems, to me .... peculiar. Do you call this code in a loop? Are you trying to only drag horizontally? Why are you using a string to store "true" and "false" rather than using an Integer which =True or =False ? I don't entirely understand what your code is attempting to do, so maybe that's because it doesn't work. Here's how I would probably do it: Graphics 640,480,0 Local Xpos:Int=50 Local Ypos:Int=50 Local Width:Int=100 Local Height:Int=100 Local Dragging:Int=False Local Offsetx:Int Local Offsety:Int Repeat 'This is your main game loop Local mx:Int=MouseX() Local my:Int=MouseY() If MouseDown(1) If Dragging=True 'We already started dragging it Xpos=mx-OffsetX Ypos=my-OffsetY Else 'We need to start dragging it If (mx>=Xpos) And (mx<Xpos+Width) And (my>=Ypos) And (my<Ypos+Height) OffsetX=mx-Xpos OffsetY=my-Ypos Dragging=True EndIf EndIf Else If Dragging=True Then Dragging=False EndIf 'Draw graphics Cls DrawLine Xpos,Ypos,Xpos+Width-1,Ypos,False DrawLine Xpos+Width-1,Ypos,Xpos+Width-1,Ypos+Height-1,False DrawLine Xpos+Width-1,Ypos+Height-1,Xpos,Ypos+Height-1,False DrawLine Xpos,Ypos+Height-1,Xpos,Ypos,False 'Do other stuff here too Flip 1 Until KeyHit(KEY_ESCAPE) Or AppTerminate() There are other variations on how you'd do this, but this does work - albeit procedurally. It should be fairly easy to conver this to object oriented - add those locals as Fields in your custom type, then each time you want to update the position and do the dragging stuff, call the main routine, then whenever you get around to doing your graphics drawing or `draw()` method or whatever, just draw the box at its current position. |
| ||
Thanks this gave me a general idea on how to approche this. As for your questions, true and false isnt being stored in a string for that purpose as I've stated at the bottom of my code. And secondly this is a part of my code which is in an Update Method inside my object. Oh and I was starting with only dragging Hor and would have added Vertically after :P |
| ||
ok ok I've been trying to make my own code with your code using offx, but it never works it always puts the cube at the Xpos instead of Xpos-Offx... Can someone make me another example so I can see multiple ways? Or one very detailed and very structure, I love when things make sense :o ------------------- after a better look, I made my own example and it worked... so I'm currently checking in my obj where i went wrong.. |
| ||
my problem was because my offsetX was set to 0 in the loop because whenever I declare an int in a loop it's set to 0 i didnt know .. lol |