Hi Eisher. I based my code on the drag and drop example. Input is on mouse, when it collide with sprite, it can drag and drop. What I would like to do is double-clik, to be able to use it on android.
So, I had up a line, just simple click for now, sprite does rotate and then can be dragged again, but won’t rotate again.
I tried to do it with right-click, no rotation, but works with wheel-up!! (but always only once).
I tried to car tutorial, I managed to “drive the car” and there is rotation of wheel, but it is keyboard driven, input is on car. With drad and drop example, input is on cursor.
I will try animation, and have rotation inside frame, as I need a third state, when the sprite becomes unmovable (the lego piece will be stuck to the board)
Here is the code of drag and drop, that I adapted so far: (did it in defold 1 and Defold 2, same problem)
function init(self)
msg.post(".", "acquire_input_focus")
self.collision_id = nil
end
function final(self)
msg.post(".", "release_input_focus")
end
function update(self)
self.collision_id = nil
end
function on_message(self, message_id, message, sender)
if message_id == hash("collision_response") then
if not self.collision_id or go.get_position(self.collision_id).z < message.other_position.z then
self.collision_id = message.other_id
end
end
end
function on_input(self, action_id, action)
-- update cursor position
go.set_position(vmath.vector3(action.x, action.y, 0))
if self.collision_id and action.pressed then
self.dragged_ts = socket.gettime()
self.dragged_id = self.collision_id
elseif action.released then
self.dragged_id = nil
end
-- we're dragging if we have a dragged_id and the time since action.pressed is larger than 0.5 seconds
local is_dragging = self.dragged_id and (socket.gettime() - self.dragged_ts >= 0.5) or false
-- if we're colliding with something and the user clicked we track the id and the time when it happened
if self.collision_id and action.pressed then
self.dragged_ts = socket.gettime()
self.dragged_id = self.collision_id
elseif action.released then
self.dragged_id = nil
end
if not action_id or action_id == hash("touch") and is_dragging then
local url = msg.url(nil, "picot", "sprite")
local widthpicot = go.get(url, "size.x")
local heightpicot = go.get(url, "size.y")
--snap to grid
go.set_position(vmath.vector3(math.floor((action.x)/16)*16+widthpicot/2, math.floor((action.y)/16)*16+1+heightpicot/2, 0), self.dragged_id)
end
--rotation of sprite..I tried many things, this is done with wheel-up, I would like double click instead. I had to desactivate multi-touch for mouse to work.
if action_id == hash("rotate") then
--I tried self. ; self.collision_id ; self.dragged_id, etc..it always work only once. collision shape is rectangle the same size as sprite
local url = msg.url(nil, "picot", nil)
local rot = vmath.quat_rotation_z(math.pi / 2)
go.set_rotation(rot ,url)
end
end
function on_reload(self)
-- Add reload-handling code here
-- Remove this function if not needed
end