Moving box with finger Swipes method (advice request)

Post Reply
User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

Moving box with finger Swipes method (advice request)

Post by Dav »

This code example works I guess, so I'll post it here, but I'd mostly like advice on how to make it better.

This is for a new puzzle game where you navigate a box through a maze. You swipe your finger up/down/left/right to move the box on the screen. The red box will stop it the blue box from moving (hitting a wall).

it works most of the time, but sometimes when swiping , it will mysteriously go the wrong way. I must not be getting x,y touch values properly. any Help will be most welcomed.

- Dav

Code: Select all

'move blue box by swiping finger up/down/left/right.
'red box will stop blue box (wall) and you have to
'navigate around it.
Code by Dav, JAN/2017


graphics

get screen size sw,sh

refresh off
'braw blue box
fill color 0,0,1
fill rect 0,0 to 50,50
sprite "s" scan 0,0,50,50
sprite "s" show
sprite "s" at 300,300

'draw red box
fill color 1,0,0
fill rect 0,0 to 50,200
sprite "r" scan 0,0,50,200
sprite "r" show
sprite "r" at 400,0
graphics clear 0,0,0
refresh on

draw text "move box by swiping finger " at 50,50
draw text "navigate around rex box"   at 50,100

SwipeDistance= sw/4 'how far to swipe


top:

'wait for no fingers down
while touch_x(0)>-1 ! end while

do
  
  'if first touch..
  get touch 0 as tx,ty

  'if screen touched
  if tx>-1 then

     'while finger down...
      while touch_x(0)>-1 

        'poll x & y locations
        movx=touch_x(0) ! newx= movx-tx
        movy=touch_y(0) ! newy= movy-ty

        'move sprite right
        if newx >SwipeDistance then 
           get sprite "s" pos sx,sy
           for x=sx to sw-50
              'if not hotting red box
              if not sprites_collide("s","r") then
                 sprite "s" at x,sy
                 pause .0001
              else 
                 sprite "s" at x-2,sy
                goto top
              end if
           next x
           goto top
        end if

        'left
        if newx <-SwipeDistance then 
           get sprite "s" pos sx,sy
           for x=sx to 0 step -1
              if not sprites_collide("s","r") then
                 sprite "s" at x,sy
                 pause  .0001
              else
                 sprite "s" at x+2,sy
                 goto top
              end if
           next x
           goto top
        end if

        'down
        if newy >SwipeDistance then 
           get sprite "s" pos sx,sy
           for y=sy to sh-50
              if not sprites_collide("s","r") then
                 sprite "s" at sx,y
                 pause .0001
              else
                 sprite "s" at sx,y-2
                 goto top
              end if
           next y
           goto top
        end if

        'up
        if newy <-SwipeDistance then 
           get sprite "s" pos sx,sy
           for y=sy to 0 step -1
              if not sprites_collide("s","r") then
                 sprite "s" at sx,y
                 pause .0001
              else
                 sprite "s" at sx,y+2
                 goto top
              end if
           next y
           goto top
        end if
      end while

  end if

until 0

end




User avatar
sarossell
Posts: 195
Joined: Sat Nov 05, 2016 6:31 pm
My devices: iPad Mini 2, iPhone 5, MacBook Air, MacBook Pro
Flag: United States of America
Contact:

Re: Moving box with finger Swipes method (advice request)

Post by sarossell »

It worked pretty good for me. I could only get it to go the wrong way twice out of about a hundred moves. And that was early on while I figured out you don't have to actually touch the blue box first. It did seem to behave more consistently when using the center of the screen and large, deliberate swiping movements. Frankly, I think it works better than other apps I've purchased.
smart BASIC Rocks!

- Scott : San Diego, California

Operator
Posts: 138
Joined: Mon May 06, 2013 5:52 am

Re: Moving box with finger Swipes method (advice request)

Post by Operator »

As sarossell stated, the wrong way will be in
the extremes of the screen.. if a swipe is started
close to the left then the block will "easily"
go up since touch_y may get -1...

In guess some microseconds the second
touch xy calls after the while touchx >-1
can get a different value...if this happens
while sweeping out of the screen then
touchx or touchy may get -1...

I used a dubbel check after your while
and it worked good (at least in my slow
iPhone 4)

Please check...

'poll x & y locations
movx = TOUCH_X(0)
IF TOUCH_X(0)=-1 THEN GOTO TOP
newx = movx-tx

movy = TOUCH_Y(0)
IF TOUCH_Y(0)=-1 THEN GOTO TOP
newy = movy-ty

User avatar
rbytes
Posts: 1338
Joined: Sun May 31, 2015 12:11 am
My devices: iPhone 11 Pro Max
iPad Pro 11
MacBook
Dell Inspiron laptop
CHUWI Plus 10 convertible Windows/Android tablet
Location: Calgary, Canada
Flag: Canada
Contact:

Re: Moving box with finger Swipes method (advice request)

Post by rbytes »

I posted some swipe code a year ago. It might be of some use. You can adjust the ratio to get very fine, slow movement or bull-in-a-China shop fast movement with finger swipes. Might be fun to ramp up the ratio for higher levels of play.

Code: Select all

/* NudgeNudge V1.0 by rbytes
   January 1, 2016
- allows very precise positioning of a crosshairs sprite on the screen.
- adjustable response. Set the variable "ratio" to 1 for 1:1 correspondence with your finger dragging. Set ratio to a decimal fraction to have the sprite move a smaller distance than your finger, or to a number higher than one to make the sprite move farther than you drag your finger. Currently set to .5 for very accurate positioning
- drag anywhere on the screen to move the sprite. You don't have to touch it to move it.
- drag repeatedly over the same section of the screen. The sprite will stop when you lift your finger but then move from the stop location when you drag again.
- adapt to your own programs if you wish. */

OPTION SPRITE POS CENTRAL
N$="sprite1"
xpos=100 ! ypos=100
ratio=.5
GRAPHICS
GRAPHICS CLEAR 1,1,1
SPRITE N$ BEGIN 53,53
DRAW COLOR 0,0,0
DRAW CIRCLE 27,27 SIZE 25
DRAW LINE 27,2 TO 27,52
DRAW LINE 2,27 TO 52,27
SPRITE N$ END
SPRITE N$ AT xpos,ypos
SPRITE N$ SHOW

DO
  nudge(x,y,xpos,ypos,ratio)
  GET TOUCH 0 AS x,y
  xpos=nudge.xpos!ypos=nudge.ypos
  SLOWDOWN
UNTIL 0
END

DEF nudge(x,y,xpos,ypos,ratio)
  WHILE x<>-1
    xset=xpos!yset=ypos
    xhold=x!yhold=y
    GET TOUCH 0 AS x,y
    xnew=x!ynew=y
    xpos+=(xnew-xhold)*ratio!ypos+=(ynew-yhold)*ratio
    xhold=xnew!yhold=ynew
    SPRITE .N$ AT xset,yset
    FILL RECT 370,390 TO 680,480    ' erase previous x AND y values
    DRAW TEXT "x = "&INT(xset)&"    y = "&INT(yset) AT 380,400
  ENDWHILE
    xpos=xset!ypos=yset
    SPRITE .N$ AT xpos,ypos
    GET TOUCH 0 AS x,y
END DEF
The only thing that gets me down is gravity...

Joel
Posts: 57
Joined: Fri Jan 15, 2016 1:36 pm
My devices: miniipad
Flag: Germany

Re: Moving box with finger Swipes method (advice request)

Post by Joel »

The problem is: if you release your finger too fast, the second value will be -1 ;-)
so i added some condition like : movx>0 ...see coloured marks

bye, joel

Code: Select all

'move blue box by swiping finger up/down/left/right.
'red box will stop blue box (wall) and you have to
'navigate around it.
'Code by Dav, JAN/2017


GRAPHICS

GET SCREEN SIZE sw,sh

REFRESH OFF
'braw blue box
FILL COLOR 0,0,1
FILL RECT 0,0 TO 50,50
SPRITE "s" SCAN 0,0,50,50
SPRITE "s" SHOW
SPRITE "s" AT 300,300

'draw red box
FILL COLOR 1,0,0
FILL RECT 0,0 TO 50,200
SPRITE "r" SCAN 0,0,50,200
SPRITE "r" SHOW
SPRITE "r" AT 400,0
GRAPHICS CLEAR 0,0,0
REFRESH ON

DRAW TEXT "move box by swiping finger " AT 50,50
DRAW TEXT "navigate around rex box"   AT 50,100

SwipeDistance= sw/4 'how far to swipe


TOP:

'wait for no fingers down
WHILE TOUCH_X(0)>-1 ! END WHILE

DO
  
  'if first touch..
  GET TOUCH 0 AS tx,ty

  'if screen touched
  IF tx>-1 THEN

     'while finger down...
      WHILE TOUCH_X(0)>-1 

        'poll x & y locations
        movx=TOUCH_X(0) ! newx= movx-tx
        movy=TOUCH_Y(0) ! newy= movy-ty

        'move sprite right
'b'
        IF newx >SwipeDistance AND movx>0 THEN 'and so on...
''
           GET SPRITE "s" POS sx,sy
           FOR x=sx TO sw-50
              'if not hotting red box
              IF NOT SPRITES_COLLIDE("s","r") THEN
                 SPRITE "s" AT x,sy
                 PAUSE .0001
              ELSE 
                 SPRITE "s" AT x-2,sy
                GOTO TOP
              END IF
           NEXT x
           GOTO TOP
        END IF

        'left
        IF newx <-SwipeDistance AND movx>0 THEN 
           GET SPRITE "s" POS sx,sy
           FOR x=sx TO 0 STEP -1
              IF NOT SPRITES_COLLIDE("s","r") THEN
                 SPRITE "s" AT x,sy
                 PAUSE  .0001
              ELSE
                 SPRITE "s" AT x+2,sy
                 GOTO TOP
              END IF
           NEXT x
           GOTO TOP
        END IF

        'down
        IF newy >SwipeDistance AND movy>0 THEN 
           GET SPRITE "s" POS sx,sy
           FOR y=sy TO sh-50
              IF NOT SPRITES_COLLIDE("s","r") THEN
                 SPRITE "s" AT sx,y
                 PAUSE .0001
              ELSE
                 SPRITE "s" AT sx,y-2
                 GOTO TOP
              END IF
           NEXT y
           GOTO TOP
        END IF

        'up
        IF newy <-SwipeDistance AND movy>0 THEN 
           GET SPRITE "s" POS sx,sy
           FOR y=sy TO 0 STEP -1
              IF NOT SPRITES_COLLIDE("s","r") THEN
                 SPRITE "s" AT sx,y
                 PAUSE .0001
              ELSE
                 SPRITE "s" AT sx,y+2
                 GOTO TOP
              END IF
           NEXT y
           GOTO TOP
        END IF
      END WHILE

  END IF

UNTIL 0

END




User avatar
Dav
Posts: 279
Joined: Tue Dec 30, 2014 5:12 pm
My devices: iPad Mini, iPod Touch.
Location: North Carolina, USA
Contact:

Re: Moving box with finger Swipes method (advice request)

Post by Dav »

Thank you all for you feedback and help. Much appreciated. Rbytes, I somehow missed that code of yours (or didn't save it). Glad to have it. Thanks for sharing.

Using Joel's addition cleared up the error. Thanks Joel.

Here is a simple prototype example of the puzzle game. You try to get the blue box to the yellow box, stopping on the red boxes. I'm going to have to use a different method than having all sprite walls (red). They will get to slow to check them all for collision every move. I will probably go to DIMming a data grid, and use that for boundry checking, bypassing sprites for walls, just drawing them on screen.

how does this play?

Code: Select all

'Blocked.txt
'move blue box by swiping finger up/down/left/right.
'red boxes will stop blue box (wall) and you have to
'navigate around it. Goal is to reach yellow box.
'Code by Dav, JAN/2017


graphics
option base 1
set orientation top
get screen size sw,sh

refresh off
'draw blue box
fill color 0,0,1
fill rect 0,0 to 100,100
sprite "b" scan 0,0,100,100
sprite "b" show
sprite "b" at 300,300

'draw yellow box
fill color 1,1,0
fill rect 0,0 to 100,100
sprite "y" scan 0,0,100,100
sprite "y" show
sprite "y" at 500,600

'draw red boxes
fill color 1,0,0
fill rect 0,0 to 100,400
sprite "r1" scan 0,0,100,400
sprite "r1" show
sprite "r1" at 400,0
sprite "r2" scan 0,0,100,100
sprite "r2" show
sprite "r2" at 500,500
sprite "r3" scan 0,0,100,100
sprite "r3" show
sprite "r3" at 100,400
sprite "r4" scan 0,0,100,100
sprite "r4" show
sprite "r4" at 200,700
graphics clear 0,0,0
refresh on

reds=4 'num of red boxes

draw text "Move blue box by swiping finger " at 10,10
draw text "Navigate around the red boxes" at 10,40
draw text "Try to reach the yellow box" at 10,70

SwipeDistance= sw/4 'how far to swipe


do
  
  'if first touch..
  get touch 0 as tx,ty

  'if screen touched
  if tx>-1 then

     'while finger down...
      while touch_x(0)>-1 

        'keep polling x & y locations
        get touch 0 as movx,movy
        newx= movx-tx ! newy= movy-ty

        'move sprite right
        if newx >SwipeDistance and movx>0 then 
           get sprite "b" pos sx,sy
           for x=sx to sw-100
              'if not hitting red box
              if not blocked then
                 sprite "b" at x,sy
                 pause .0001
              else 
                 sprite "b" at x-2,sy
                 break
              end if
              gosub checkwin
           next x
        end if

        'left
        if newx <-SwipeDistance and movx >0 then 
           get sprite "b" pos sx,sy
           for x=sx to 0 step -1
              if not blocked then
                 sprite "b" at x,sy
                 pause  .0001
              else
                 sprite "b" at x+2,sy
                 break
              end if
              gosub checkwin
           next x
        end if

        'down
        if newy >SwipeDistance and movy > 0 then 
           get sprite "b" pos sx,sy
           for y=sy to sh-100
              if not blocked then
                 sprite "b" at sx,y
                 pause .0001
              else
                 sprite "b" at sx,y-2
                 break
              end if
              gosub checkwin
           next y
        end if

        'up
        if newy <-SwipeDistance and movy > 0 then 
           get sprite "b" pos sx,sy
           for y=sy to 0 step -1
              if not blocked then
                 sprite "b" at sx,y
                 pause .0001
              else
                 sprite "b" at sx,y+2
                 break
              end if
              gosub checkwin
           next y
        end if

      end while
     
      'wait for finger up
      while touch_x(0)>-1 ! end while

  end if

until 0

end


checkwin:
if sprites_collide("b","y") then 
   sprite "y" order reds+2
   get sprite "y" pos _x,_y
   notes set "9:tc6e6f6g6" ! notes play
   a=255
   for g= 1 to 500 step 10
     sprite "y" alpha a/255!a=a-5
     sprite "y" at _x,_y scale g/50 angle g/100
     pause .02
   next g
   sprite "y" hide
   end
end if
return


def blocked()
for t= 1 to .reds
   if sprites_collide("b","r"&str$(t)) then 
     notes set "112:tc8"
     notes play
     return 1
   end if
next t
blocked=0
end def

User avatar
sarossell
Posts: 195
Joined: Sat Nov 05, 2016 6:31 pm
My devices: iPad Mini 2, iPhone 5, MacBook Air, MacBook Pro
Flag: United States of America
Contact:

Re: Moving box with finger Swipes method (advice request)

Post by sarossell »

Pretty cool Dav! For what it's worth, I found removing the PAUSEs and setting SwipeDistance=sw/10 felt much more fluid and natural. But that's just me. :D
smart BASIC Rocks!

- Scott : San Diego, California

Post Reply