I am stuck with a tiny problem and need your help, please:
My app is going to be html, so I need to cater for possible mobile input, too.
I would like the mobile user to have a choice:
move when touching the left side of the screen, rotate when touching the right side.
Those who would like to have visual cues can switch on a virtual joystick.
To implement this, I have added two large nodes to the gui (they will be invisible in the game) one covering the right side, one the left side of the screen. I also added nodes for the joystick:
I would like to use @britzl’s onscreen.lua for this. Movement does work quite well, but the joystick buttons do not move, either one or both of them. I then moved the large nodes out of the way when the joystick is switched on, still the joystick buttons are stuck.
This is my code:
game.script init:
msg.post(/controls#controls, register)
-- register the joystick
msg.post(/controls#controls, register_analog, { id = move, radius = 52 })
msg.post(/controls#controls, register_analog, { id = rotate, radius = 52 })
game.script on_message:
if message_id == ONSCREEN_ANALOG and message.id == MOVING or message.id == MOVING_LARGE then
self.action_dx = 0
self.action_dy = 0
if message.released then
self.move.x = 0
self.move.y = 0
self.move.z = 0
self.speed = 0
else
self.move.x = message.x
self.move.z = -message.y
self.speed = speed_keys
end
elseif message_id == ONSCREEN_ANALOG and message.id == ROTATING or message.id == ROTATING_LARGE then
if message.released then
rotation_speed = 0.2
self.action_dx = 0
self.action_dy = 0
else
rotation_speed = 1
self.action_dx = message.x
self.action_dy = message.y
end
end
game.script on_input:
if action_id == MULTITOUCH then
-- register the invisible controls
-- radius set to 1, because these controls don't need to move like a joystick button
msg.post("/controls#controls", "register_analog", { id = "move_large", radius = 1 })
msg.post("/controls#controls", "register_analog", { id = "rotate_large", radius = 1 })
end
controls.gui_script on top:
local function on_analog(action_id, node, action)
post_to_listener(action_id, action)
end
controls.gui_script init:
self.analog = {}
post_to_listener = function(message_id, message)
if self.listener then
msg.post(self.listener, message_id, message or {})
end
end
controls.gui_script on_message:
if message_id == REGISTER then
self.listener = sender
elseif message_id == UNREGISTER then
self.listener = nil
elseif message_id == REGISTER_ANALOG then
onscreen.register_analog(gui.get_node(message.id), { radius = message.radius }, on_analog)
end
controls.gui_script on_input:
onscreen.on_input(action_id, action)
Sorry for the long post I did try lot’s of things, including my own script for screen splitting or using a separate on-screen script for the invisible nodes, I fiddled with the radii, too.