I’m writing an app that has multiple scenes (inventory, main etc.) created with composer and I want to add a swipe listener to navigate between them. For now I have a Runtime touch listener as it can be seen here –
local function swipe\_scene(event) if event.phase == "ended" then if (event.xStart \< event.x and (event.x - event.xStart) \>= 100 and current\_scene\_no \> 1) then local options = { effect = "slideRight", time = 500 }; current\_scene\_no = current\_scene\_no - 1; composer.gotoScene(scene\_list[current\_scene\_no], options); elseif (event.xStart \> event.x and (event.xStart - event.x) \>= 100 and current\_scene\_no \< scene\_list.count) then local options = { effect = "slideLeft", time = 500 }; current\_scene\_no = current\_scene\_no + 1; composer.gotoScene(scene\_list[current\_scene\_no], options); end end return true; end Runtime:addEventListener("touch", swipe\_scene);
but this does not look good since it doesn’t show the new scene while swiping, but switches to it only after swipe is completed (There is no need for it to be a swipe as well since it only checks the x and xStart coordinates) Is there any way to accomplish what I want with scenes or are scenes aren’t suitable for this?
>> Is there any way to accomplish what I want with scenes or are scenes aren’t suitable for this?
yes, BUT
if you have to ask, then you likely don’t yet know enough to fully accomplish it
it isn’t built in, and requires a good amount of “hackery” to make it fully functional (read: to make it work the way that you intuitively expect it would)…
you need to set up your current scene as an “overlay” above the next scene (so that next scene is present underneath)
you’ll need full began/moved/ended touch response because you’re making a “drag” listener
you’ll need to adjust the scene.view’s x coordinate (assuming it’s a left/right swipe you’re describing)
you’ll need to adjust composer’s slideLeft/Right effect’s xStart value (so that on touch ended the transition BEGINS where the DRAG ends) (i recommend cloning those effects before beginning as swipeLeft/swipeRight so that you don’t “break” them if used elsewhere for non-swipe slides)
you’ll need to adjust the transition time as a ratio of the scene’s now-offset x to the full-screen width (because you only want to use the full time if fully on-screen, ie if already 90% offscreen you only want 10% transition time, because you’ve already started at the 90% x-coordinate)
you also need an “oops” factor – if delta-x when touch ends is less than some tolerance, then DON’T transition, instead reposition scene.view at 0 as if swipe never occurred.
you also need to clamp delta-x so they don’t swipe it the “wrong direction” or further than fully offscreen in the “correct direction” (tho latter is unlikely to be a problem unless you fully support multitouch)
it’s actually far easier to accomplish something visually-equivalent WITHOUT composer, except that typically when this question is asked, composer is already in place and doing it via scene-transition/management is a requirement.
>> Is there any way to accomplish what I want with scenes or are scenes aren’t suitable for this?
yes, BUT
if you have to ask, then you likely don’t yet know enough to fully accomplish it
it isn’t built in, and requires a good amount of “hackery” to make it fully functional (read: to make it work the way that you intuitively expect it would)…
you need to set up your current scene as an “overlay” above the next scene (so that next scene is present underneath)
you’ll need full began/moved/ended touch response because you’re making a “drag” listener
you’ll need to adjust the scene.view’s x coordinate (assuming it’s a left/right swipe you’re describing)
you’ll need to adjust composer’s slideLeft/Right effect’s xStart value (so that on touch ended the transition BEGINS where the DRAG ends) (i recommend cloning those effects before beginning as swipeLeft/swipeRight so that you don’t “break” them if used elsewhere for non-swipe slides)
you’ll need to adjust the transition time as a ratio of the scene’s now-offset x to the full-screen width (because you only want to use the full time if fully on-screen, ie if already 90% offscreen you only want 10% transition time, because you’ve already started at the 90% x-coordinate)
you also need an “oops” factor – if delta-x when touch ends is less than some tolerance, then DON’T transition, instead reposition scene.view at 0 as if swipe never occurred.
you also need to clamp delta-x so they don’t swipe it the “wrong direction” or further than fully offscreen in the “correct direction” (tho latter is unlikely to be a problem unless you fully support multitouch)
it’s actually far easier to accomplish something visually-equivalent WITHOUT composer, except that typically when this question is asked, composer is already in place and doing it via scene-transition/management is a requirement.