- Select manual -

Down duration

Listens to input trigger "touch" and count mouse down duration in update method.

input bindings



function init(self)
    msg.post(".", "acquire_input_focus") 
    self.message = "Duration: %f . Last duration: %f" 
    self.duration = 0
    self.last_duration = 0

local function update_text(self) 
    local msg = string.format(self.message, self.duration, self.last_duration) 
    label.set_text("#label", msg) 

function on_input(self, action_id, action)
    if action_id == hash("touch") then 
        if action.pressed then 
            self.is_start_timer = true 
        elseif action.released then 
            self.is_start_timer = false 
            self.last_duration = self.duration 
            self.duration = 0

function update(self, dt)
    if self.is_start_timer then 
        self.duration = self.duration + dt 
  1. Tell the engine that this object ("." is shorthand for the current game object) wants to receive input. The function on_input() will be called whenever input is received.
  2. Prepare format of output including two float placeholders.
  3. Create method for updating the text label.
  4. Create a formatted string from the format and duration and last_duration arguments.
  5. Set the label component to the stored text.
  6. Check if we receive an input action named “touch”.
  7. Check if it is pressed then run the following.
  8. Change flag for starting a timer.
  9. Check if it is released then run the following.
  10. Change flag for stopping a timer.
  11. Save duration as last_duration.
  12. Run method for updating text.
  13. Check if timer is started.
  14. Add dt (delta time from the last frame) to the duration variable.
  15. Run method for updating text.

If you want to play with these examples, you can get the project on Github.

Do you want to see more examples? Why not write a few yourself and submit a pull request? We love contributions.