If the solution provided didn't work as expected, it might be due to various factors such as the way your project is set up, how input is being handled, or differences in screen coordinate expectations between Defold and your project setup. Here is an alternative approach you can try, making sure to pay close attention to any project-specific details that might require adjustment.
Step 1: Confirm Your Input Setup
First, ensure your game.project file has the correct input settings and that you have an Input Binding file (input.gamepads, input.keys, input.mouse, input.text, input.touch) set up with the actions you want to listen for. For mouse position, you generally don't need to define a specific action since mouse position is available for any mouse action.
Step 2: Adjust the on_input Function
You'll want to capture the screen dimensions and adjust the input coordinates based on those dimensions directly within your on_input function. The critical step here is to ensure that you're getting the window's current dimensions correctly, considering the use case when the window might be resizable.
Let's revise the approach with attention to dynamic window sizing and simplifying the conversion:
function init(self)
-- Consider querying initial window size here if your application window is resizable
self.screen_width = tonumber(sys.get_config("display.width"))
self.screen_height = tonumber(sys.get_config("display.height"))
end
function update_window_size(self)
-- Update window size dynamically if needed
-- This might be necessary if your game window can be resized
local width, height = window.get_size()
self.screen_width = width
self.screen_height = height
end
function on_input(self, action_id, action)
-- Optional: Update window size each time in case of window resizing
-- update_window_size(self)
if action.x and action.y then -- Ensuring action.x and action.y are valid
-- Translate input position so (0,0) is in the middle of the screen
local pos_x = action.x - (self.screen_width / 2)
local pos_y = (self.screen_height / 2) - action.y -- Inverting Y due to Defold's origin being bottom-left
-- Now pos_x and pos_y are relative to the center of the screen
-- Debugging output
print("Translated Pos:", pos_x, pos_y)
-- Use pos_x and pos_y for your game logic
end
end
Please note a few critical points in this approach:
-
Window Size: There's an assumption that your window size might change. If it cannot change, this dynamic updating isn't necessary, but it's useful for resizable windows.
-
Y Coordinate Inversion: Remember, Defold's coordinate system starts from the bottom-left corner. Depending on your expectations (e.g., if you come from a system where the origin is typically at the top-left), you might need to invert the Y-axis.
-
Debugging: The print statement is there to help you see what the translated coordinates are. If you're not seeing these prints, ensure that the if action.x and action.y then condition is being met.
-
Correct Hooks and Lifecycle: Ensure that the functions init and on_input are correctly connected to your script's lifecycle, and that the script is attached to a game object that exists in your scene.
-
Input Binding: While action.x and action.y don't require explicit action bindings, make sure your project is correctly set up to receive input (i.e., the correct script is attached to a GameObject that's active and included in your main collection).
If you're still facing issues, consider whether there are more complexities in your specific project setup that might affect input handling, such as GUI nodes intercepting inputs or misconfigured input bindings.