My aim is to get an object can be dragged around by mouse. I have tried id by utilize action.dx and action.dy, but I don’t get the proper behavior. Finally I get it working by manually calculate dx and dy as can be seen in the folowing code:
`function on_input(self, action_id, action)
if action_id == hash(“touch”) and action.pressed then
local pos = go.get_world_position("#sprite")
local size = go.get("#sprite", “size”)
local bound = {
top = pos.y + size.y/2,
right = pos.x + size.x/2,
bottom = pos.y - size.y/2,
left = pos.x - size.x/2
}
if action.x >= bound.left and action.x <= bound.right and action.y >= bound.bottom and action.y <= bound.top then
self.selected = true
self.prevx = action.x
self.prevy = action.y
end
elseif action_id == hash("touch") and action.released and self.selected then
self.selected = false
self.prevx = 0
self.prevy = 0
end
if self.selected and not action.pressed then
local dx = action.x - self.prevx
local dy = action.y - self.prevy
local pos = go.get_position()
pos.x = pos.x + dx
pos.y = pos.y + dy
go.set_position(pos)
self.prevx = action.x
self.prevy = action.y
end
end`
I realized that action.dx and action.dy have different value than my dx and dy. Is there any good explanation about action.dx and action.dy? How are they being calculated?
–
Edit: Somehow my code can’t appear properly.