I’ve done like that, it’s not working if I am pressing key for a while. It works only when I stop pressing and my object is behind my bounds. And it works bad if i’m trying to press up and down fast by order. Please help me fix my input. I am new in game development
function on_input(self, action_id, action)
local pos = go.get_position()
if action_id == hash("up") then
if go.get_position().y < 750 then
if action.pressed then
speed = 5
elseif action.released then
speed = 0
end
end
elseif action_id == hash("down") then
if go.get_position().y > 150 then
if action.pressed then
speed = -5
elseif action.released then
speed = 0
end
end
end
end
I assume you are moving in update() according to the speed you set in on_input()? If that is the case then why don’t you move the bounds check to update() instead of in on_input()?
I’ve done in that way and it works, thanks! But another problem is still actual: when I stop pressing up or down and at once after that press down or up, my hero goes neither up nor down. I think it because of action.released. Can’t understand why. Is there any other way for action.released? Or some method which will tell if nothing is pressed?
function on_input(self, action_id, action) local pos = go.get_position() if action_id == hash("up") then if action.pressed then speed = 5 elseif action.released then speed = 0 end elseif action_id == hash("down") then if action.pressed then speed = -5 elseif action.released then speed = 0 end end end
function update(self, dt) local pos = go.get_position() if pos.y > 680 then if speed > 0 then speed = 0 end elseif pos.y < 170 then if speed < 0 then speed = 0 end end pos.y = pos.y + speed go.set_position(pos) end