I’ve made a little modification to your code that creates a maximum scrollable area.
if the camera is moved outside of the maximum scrollable area, flinging doesn’t work, and the camera bounces back to just inside the maximum scrollable area. Works in all four directions. Maximum scrollable area is defined by 4 properties at the start of the script.
go.property("fling_enabled", true)
go.property("fling_distance_threshold", 2000) -- pixels per second
go.property("xmin", -120) -- these four attributes control the size of the scrollable part of the display
go.property("xmax", 230)
go.property("ymin", -300)
go.property("ymax", 250)
go.property("within", true)
go.property("sliding", false)
local function slidedone(self, url, property)
go.set("#script", "within", true)
go.set("#script", "sliding", false)
end
function init(self)
msg.post("camera", "acquire_camera_focus")
msg.post(".", "acquire_input_focus")
self.fling = vmath.vector3()
end
function final(self)
msg.post("camera", "release_camera_focus")
msg.post(".", "release_input_focus")
end
function update(self, dt)
if not self.drag and self.fling_enabled and self.within then
go.set_position(go.get_position() + self.fling)
self.fling = vmath.lerp(0.1, self.fling, vmath.vector3())
end
if not self.drag and not self.within and not self.sliding then
if go.get_position().y >= self.ymax then
go.animate(".", "position.y", go.PLAYBACK_ONCE_FORWARD, self.ymax-1, go.EASING_OUTEXPO, 0.2, 0, slidedone)
go.set("#script", "sliding", true)
go.set("#script", "fling_enabled", false)
end
if go.get_position().y <= self.ymin then
go.animate(".", "position.y", go.PLAYBACK_ONCE_FORWARD, self.ymin+1, go.EASING_OUTEXPO, 0.2, 0, slidedone)
go.set("#script", "sliding", true)
go.set("#script", "fling_enabled", false)
end
if go.get_position().x >= self.xmax then
go.animate(".", "position.x", go.PLAYBACK_ONCE_FORWARD, self.xmax-1, go.EASING_OUTEXPO, 0.2, 0, slidedone)
go.set("#script", "sliding", true)
go.set("#script", "fling_enabled", false)
end
if go.get_position().x <= self.xmin then
go.animate(".", "position.x", go.PLAYBACK_ONCE_FORWARD, self.xmin+1, go.EASING_OUTEXPO, 0.2, 0, slidedone)
go.set("#script", "sliding", true)
go.set("#script", "fling_enabled", false)
end
end
end
function on_input(self, action_id, action)
if action.pressed then
self.drag = true
self.pressed_pos = vmath.vector3(action.x, action.y, 0)
self.pressed_time = socket.gettime()
self.camera_pos = go.get_position()
elseif action.released then
self.drag = false
if self.fling_enabled and self.within then
local released_time = socket.gettime()
local released_pos = vmath.vector3(action.x, action.y, 0)
local delta_time = released_time - self.pressed_time
local distance = vmath.length(released_pos - self.pressed_pos)
local velocity = distance / delta_time
if velocity >= self.fling_distance_threshold then
local direction = vmath.normalize(self.pressed_pos - released_pos)
self.fling = direction * velocity * 0.02
end
end
if go.get_position().x >= self.xmin and go.get_position().x <= self.xmax and go.get_position().y >= self.ymin and go.get_position().y <= self.ymax then
go.set("#script", "within", true)
else
go.set("#script", "within", false)
end
end
if self.drag then
local mouse_pos = vmath.vector3(action.x, action.y, 0)
go.set_position(self.camera_pos + self.pressed_pos - vmath.vector3(action.x, action.y, 0))
if self.fling_enabled then
go.set("#script", "sliding", false)
end
end
end