What are action.dx and action.dy actually? (DEF-2712)

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.

Storing previous action.x and doing like you do with action.x - self.prevx should result in exactly the same result as using action.dx. I did a quick test myself just to confirm this:

function on_input(self, action_id, action)
	print(action.dx, action.x - (self.prev_x or action.x)) -- will print identical values
	self.prev_x = action.x
end

I’m not really sure why you’re not seeing the same values.

It might be worth mentioning action.screen_dx and action.screen_x which represents the x and delta x if the window is scaled. Full list of values on the action table with explanations can be found here.

Hello, I am new to Defold, yet the mobile game I’m trying to build is relying strongly on action.dx and action.dy for core movement with the finger. However, while working as expected while testing the game with ALT+B, when I do a build HTML5 to also try it on an actual phone with real finger gestures, I get a really bad behavior (my player’s position changes directly to the position of the the finger) as if the first action.dx is equal to the action.x right when pressed triggers and then working as a delta. On top of that, I don’t know how to debug this, cuz my print statements don’t appear on the console when using HTML5 to test the game. The documentation doesn’t explain if there’s actually any difference for when it’s actual touch event or from the mouse’s pointer.
@britzl

They will end up in the browser dev console.

I’d probably track previous action.x and action.y and subtract from current action.x and action.y to get a delta x and y. I think dx and dy will be set to action.x and action.y for when the finger touches the screen,

1 Like

Actually you were right about action.dx on and action.dy on mobile, because I’ve just refactored my code to record each finger position based on the event, and now I get the same behavior on my phone too! Thank you very much, maybe this detail can be also added in the documentation too.

2 Likes

Yep, good point. Ping @sicher

1 Like

We have this marked as a bug in our backlog. DEF-2712

1 Like