Card being detected as closed and opened at the same time

I am trying to detect play cards if they are opened or closed using the code below

local rendercam = require "rendercam.rendercam"

go.property("is_opened", false)

local function shake(self)
  go.cancel_animations(".", "scale.x")
  go.cancel_animations(".", "scale.y")
  go.set_scale(self.initial_scale)
  local scale = go.get_scale()
  go.set_scale(scale * 1.2)
  go.animate(".", "scale.x", go.PLAYBACK_ONCE_FORWARD, scale.x,
             go.EASING_OUTELASTIC, 0.8)
  go.animate(".", "scale.y", go.PLAYBACK_ONCE_FORWARD, scale.y,
             go.EASING_OUTELASTIC, 0.8, 0,
             function() go.set_scale(self.initial_scale) end)
end

function init(self)
  self.initial_scale = go.get_scale()
  msg.post(".", "acquire_input_focus")
end

function on_input(self, action_id, action)
  if self.is_opened == false then
    print("on_message: skip closed card")
    return
  else
    print("on_message: open card")
  end

  local pos = rendercam.screen_to_world_2d(action.x, action.y)
  action.x = pos.x
  action.y = pos.y
  msg.post("/cursor#cursor", "input", { action_id = action_id, action = action })
end

function on_message(self, message_id, message, sender)

  if message_id == cursor.OVER then
    self.over_pos = go.get_position()
    go.set_position(vmath.vector3(self.over_pos.x, self.over_pos.y, 1))
    go.animate("face#face_sprite", "tint.w", go.PLAYBACK_ONCE_FORWARD, 1.5,
               go.EASING_OUTQUAD, 0.1)
  elseif message_id == cursor.OUT then
    local pos = go.get_position()
    go.set_position(vmath.vector3(pos.x, pos.y, self.over_pos.z))
    go.animate("face#face_sprite", "tint.w", go.PLAYBACK_ONCE_FORWARD, 1,
               go.EASING_OUTQUAD, 0.1)
  elseif message_id == cursor.PRESSED or message_id == cursor.RELEASED then
    shake(self)
    print("shake")
  end
end

using this code block from the above code

  if self.is_opened == false then
    print("on_message: skip closed card")
    return
  else
    print("on_message: open card")
  end

when I hover over a card I am getting this debug script


It is not detecting the card properties correctly.
have a look at the project using this repo

1 Like

One thing I noticed is that each card acquires input focus in card.script and each card forwards converted coordinates to the cursor. This is not what I suggested in my previous answer. Having many game objects acquire input focus will fill up the input queue which can be seen by this error message:

WARNING:GAMEOBJECT: Input focus could not be acquired since the buffer is full (16).

The other problem with your approach is that each time you move the mouse or generate some other input event it will be sent to all instances of the card.script. This is the reason why you see multiple log entries each frame.

You should only update the cursor position once per frame, preferably from the controller.script.

2 Likes