Posted: 23rd Oct 2011 23:29
A challenge...

Can anyone enhance this sprite selection scroller, using only standard input commands, no RAW ?

+ Code Snippet
//
// AGK Application
// Scroll test
//

setvirtualresolution    (1280,800)
setclearcolor           (0,0,0)
setbordercolor          (0,0,0)
setorientationallowed   (0,0,1,1)
setresolutionmode       (1)
setsyncrate             (60,1)
setprintsize(24)

f=255
for s=601 to 700
    createsprite(s,0)
    setspritesize(s,256,256)
    setspritecolor(s,f,f,f,255)
    setspriteposition(s,-5000,-5000)
    dec f,16
    if f<64 then f=255
next x

offsetX#=640.0
movespeed#=0.0
startclick=0
stopclick=0
diff#=0.0
do
    // Draw sprites
    for x=1 to 50
        setspritepositionbyoffset(x+600,offsetX#+(x*288),300)
        setspritepositionbyoffset(x+650,offsetX#+(x*288),588)
    next x

    // Do calculations
    offsetX#=offsetX#+movespeed#
    if offsetX#>640.0 then offsetX#=640.0
    if offsetX#<-14250.0 then offsetX#=-14250.0
    if getpointerpressed()=1
        startclick=getpointerx()
        diff#=offsetX#
    endif
    if getpointerstate()=1 then offsetX#=diff#-(startclick-getpointerX())
    if getpointerreleased()=1 then stopclick=getpointerx()
    movespeed#=((startclick-stopclick)/50.0)*-1

    // Print Diag
    print(getpointerx())
    printc("Start click X: ")
    print(startclick)
    printc("Stop click X: ")
    print(stopclick)
    printc("Diff: ")
    print(diff#)
    printc("OffsetX: ")
    print(offsetX#)
    sync()
loop


I want it to work like when you scroll a webpage on a phone.
Posted: 24th Oct 2011 20:20
Improved it abit, but still not perfect.

Need to make a direction change detection, that does not go bananas when reading a finger on a touch device, it quickly jumps between the states, if I try to read direction change from last read, might need to set up an array of readings, then analyze them in realtime... So this code does not take into account direction change while not releasing the screen.

+ Code Snippet
//
// AGK Application
// Scroll test
//

setvirtualresolution    (1280,800)
setclearcolor           (0,0,0)
setbordercolor          (0,0,0)
setorientationallowed   (0,0,1,1)
setresolutionmode       (1)
setsyncrate             (60,1)
setprintsize(24)

f=255
for s=601 to 700
    createsprite(s,0)
    setspritesize(s,256,256)
    setspritecolor(s,f,f,f,255)
    setspriteposition(s,-5000,-5000)
    dec f,16
    if f<64 then f=255
next x

offsetX#=640.0
movespeed#=0.0
startclick=0
distance=100
diff#=0.0
starttime#=0.0
timediff#=1.0

do
    // Draw sprites
    for x=1 to 50
        setspritepositionbyoffset(x+600,offsetX#+(x*288),300)
        setspritepositionbyoffset(x+650,offsetX#+(x*288),588)
    next x

    // Do calculations of movement from inertia
    offsetX#=offsetX#+movespeed#

    // Force limitation of start and stop offset values
    if offsetX#>640.0 then offsetX#=640.0
    if offsetX#<-14250.0 then offsetX#=-14250.0

    // To do when if screen is first pressed
    if getpointerpressed()=1
        startclick=getpointerx()
        diff#=offsetX#
        starttime#=timer()
    endif

    // To do while screen is pressed
    if getpointerstate()=1
        offsetX#=diff#-(startclick-getpointerx())
    endif

    // To do when screen is released
    if getpointerreleased()=1
        distance=getpointerx()-startclick
        timediff#=timer()-starttime#
        movespeed#=(distance/timediff#)/80
    endif

    // Apply "friction"
    if movespeed#>0 then movespeed#=movespeed#-0.05
    if movespeed#<0 then movespeed#=movespeed#+0.05

    // Apply full stop at very low speed (drif prevention)
    if movespeed#>0 and movespeed#<0.05 then movespeed#=0.0
    if movespeed#<0 and movespeed#>-0.05 then movespeed#=0.0

    // Print diag data
    printc("OffsetX: ")
    print(offsetX#)
    printc("The mouse moved: ")
    print(distance)
    printc("In so many seconds: ")
    print(timediff#)
    printc("Speed in pixel/sec: ")
    print(distance/timediff#)
    printc("Movespeed: ")
    print(movespeed#)

    // Draw all
    sync()
loop
Posted: 25th Oct 2011 20:03
Even better, now I take into account the finger swiping in changing directions. Still abit jerky scroll, but I'm getting there...

+ Code Snippet
//
// AGK Application
// Scroll test
//

setvirtualresolution    (1280,800)
setclearcolor           (0,0,0)
setbordercolor          (0,0,0)
setorientationallowed   (0,0,1,1)
setresolutionmode       (1)
setsyncrate             (60,1)
setprintsize(24)

f=255
for s=601 to 700
    createsprite(s,0)
    setspritesize(s,256,256)
    setspritecolor(s,f,f,f,255)
    setspriteposition(s,-5000,-5000)
    dec f,16
    if f<64 then f=255
next x

offsetX#=640.0
movespeed#=0.0
diff#=0.0
timer1#=timer()
x1=0
dist=0
speed#=0.0

do
    // Draw sprites
    for x=1 to 50
        setspritepositionbyoffset(x+600,offsetX#+(x*288),300)
        setspritepositionbyoffset(x+650,offsetX#+(x*288),588)
    next x

    // Do calculations of movement from inertia
    offsetX#=offsetX#+movespeed#

    // Force limitation of start and stop offset values
    if offsetX#>640.0 then offsetX#=640.0
    if offsetX#<-14250.0 then offsetX#=-14250.0

    // To do when if screen is first pressed
    if getpointerpressed()=1
        startclick=getpointerx()
        diff#=offsetX#
    endif

    // To do while screen is pressed
    if getpointerstate()=1
        offsetX#=diff#-(startclick-getpointerx())

        // Calculate mouse speed over 50 millisec
        if timer()-timer1#>0.05
            timer1#=timer()
            gpx=getpointerx()
            dist=gpx-x1
            speed#=dist/4
            x1=gpx
        endif
    endif

    // To do when screen is released
    if getpointerreleased()=1
        movespeed#=speed#
    endif

    // Apply "friction"
    if movespeed#>0 then movespeed#=movespeed#-0.1
    if movespeed#<0 then movespeed#=movespeed#+0.1

    // Apply full stop at very low speed (drif prevention)
    if movespeed#>0 and movespeed#<0.1 then movespeed#=0.0
    if movespeed#<0 and movespeed#>-0.1 then movespeed#=0.0

    // Print diag data
    printc("OffsetX: ")
    print(offsetX#)
    printc("SpeedStep: ")
    print(movespeed#)
    printc("MouseSpeed: ")
    print(speed#)

    // Draw all
    sync()
loop
Posted: 25th Oct 2011 22:00
Hey, I'm glad I've tried your code. It is super cool - remembers me the new Windows phone interface.

Nice!