How to get touch on one side of screen? (SOLVED)

Hello I just started using Defold and am liking it so far however I have run into an issue. I am making a game in which if the user touches one half of the screen a block moves one way and if the user touches the other half the block goes the other way. I cannot find a way to do this in Defold (I’m very new). In Unity I would go about this by simply doing something like (pseudo code)

if touchcoord > Screen.width/2
{
right = true;
}

But am not aware of a code like screen.width in Defold. I was thinking about making two invisible objects on each half of the screen but am not quite sure on how to tell if they have been touched or not. I’m sure there’s a very simple fix to this.

Thanks.

Not an expert, learning as well but this is my experience.

create a gui or if you already have one, put your buttons.
create gui_script ,Then in on_input in gui_script you can define the area screen positions.

Screen positions
Mouse and touch input set fields in the action table for location (x and y) as well as movement (dx and dy). These inputs also set specific fields that are calculated against the real screen space. These fields are called:

screen_x and screen_y

  The real screen position
screen_dx and screen_dy 

  The real screen delta change since last update

These fields are useful on devices where the screen pixel position
differs from the game position—​for instance if you run a lower
resolution game on a retina screen that scales the game. For instance,
on the iPad you can have a screen that is 1024x768 but each “logical”
pixels consists of 4 real ones and maps to a physical resolution of
2048x1536.

You can do this from both script components and gui script components. First you need to acquire input so that the script gets calls to it’s on_input function:

function init(self)
    msg.pos(".", "acquire_input_focus")
end

Once you have acquired input focus you will start seeing calls to your on_input() function:

function on_input(self, action_id, action)
     print("I was called with action_id", action_id)
     pprint(action)
end

Any action you define in your input bindings will be passed along as the action_id to the input function when it’s triggered. Touch and mouse button one will be sent with action_id “touch”.

function on_input(self, action_id, action)
     if action_id == hash("touch") and action.pressed then
         print("You touched", action.x, action.y)
         -- action.screen_x and action.screen_y are translated screen coordinates
     end
end

You can read the screen dimensions in two ways:

  1. Get the value from game.project using sys.get_config("display.width") and sys.get_config("display.height"). This will not get updated values if the screen was resized.
  2. Use a custom render script and read render.get_window_width() and render.get_window_height()? to the get the current window dimensions. Do a msg.post() of these values or store them in a shared module for easy access from your script files.

So, to wrap this up I think it’s enough to use the first of the two ways to get screen size and compare that value with the x and y coordinate where the user touched/clicked:

function on_input(self, action_id, action)
     if action_id == hash("touch") and action.pressed then
         if action.x > tonumber(sys.get_config("display.width")) / 2 then
             -- right
         else
             -- left
         end
     end
end

Read more about how input is handled in Defold here.

5 Likes

Thanks a lot! How do I mark this as solved :stuck_out_tongue:

1 Like