Drag & Drop in Monkey

Monkey Forums/Monkey Programming/Drag & Drop in Monkey

trainer(Posted 2013) [#1]
Hi all,

I'm a beginner developer in Monkey Coder, and I was looking for some method or built-in function for (drag & drop) but unfortunately I couldn't find one.

In my game, the player should drag some objects, toys, from the ground and classify them, drop them, into their specific boxes:




Can anybody tell me how to do this in Monkey?
Is there any alternative code in Monkey coder that has the same functionality as (drag & drop)?

Your immediate reply is highly appreciated guys.
Thanks!


therevills(Posted 2013) [#2]
Really simply:
Each object has an x and y position
Each box has a region
When the user clicks/drags an object the object's x/y will be the same as the mouse/touch position
Once the drop happens check to see if the object's x/y is within a box's region


trainer(Posted 2013) [#3]

Really simply:
Each object has an x and y position
Each box has a region
When the user clicks/drags an object the object's x/y will be the same as the mouse/touch position
Once the drop happens check to see if the object's x/y is within a box's region



Thanks for reply,

Could you please give me a sample of code?
Or any source code has the same idea?


Paul - Taiphoz(Posted 2013) [#4]
Assuming that your using some sort of class for your items...

[monkeycode]

const held : int = 1
const dropped : int = 2


class toy
field _x:int
field _y:int
field _sprite:Image
field _status:int

method Held()
self._x = MouseX() ' or use self._x = ToushX()
self._y = MouseY() ' or touchy
end method

method draw....

method update()
'if mouse or touch is inside or over this object
'and touchdown then
self._status = 1

'else
self._status = 2
end method

[/monkeycode]


thats very rough code, essentially what you would do with your toy class, is use either radius or rects overlap code to see if the mouse of finger is over the item, if there is then a touch or left click detected set the field for that object so that its status is held, then in the update method for this toy class you would simply draw the toy at mouse/touch x,y

sorry if this is hard to follow, but hope it helps, not got time to actually code it in full.


trainer(Posted 2013) [#5]

Assuming that your using some sort of class for your items...

thats very rough code, essentially what you would do with your toy class, is use either radius or rects overlap code to see if the mouse of finger is over the item, if there is then a touch or left click detected set the field for that object so that its status is held, then in the update method for this toy class you would simply draw the toy at mouse/touch x,y

sorry if this is hard to follow, but hope it helps, not got time to actually code it in full.



Great! I got the idea,
but how can I know if the player released his finger, or the mouse, from the screen?
Is there any built-in function for touchdown?


ziggy(Posted 2013) [#6]
TouchDown(1) returns True if the first finger is touching the screen. Also TouchX(1) and TouchY(1) let's you get the X and Y position of the touch.
You can also use MouseDown() and MouseX() and MouseY()
You can also use touchHit and MouseHit, that return True if the screen has been touched or the mouse has been clicked.


trainer(Posted 2013) [#7]

TouchDown(1) returns True if the first finger is touching the screen. Also TouchX(1) and TouchY(1) let's you get the X and Y position of the touch.
You can also use MouseDown() and MouseX() and MouseY()
You can also use touchHit and MouseHit, that return True if the screen has been touched or the mouse has been clicked.



I appreciate your immediate reply, ziggy.
I'll try them out and see which one fits me more.

Thanks!


Gerry Quinn(Posted 2013) [#8]
Basically, have a variable that records any object that is being carried. It could be a reference to the object itself (null if nothing is being carried)- or it can be done in a number of ways.

While it is being carried, do not draw it in the normal way but draw it at the mouse position.

When it is dropped, update its position and start drawing it normally, and set your 'carriedObject' variable to null.