Try it out. You can bump the blue block into the red block and hear the beep when it hits - from left, right, above and below.
Code: Select all
'dragsprite.txt
'Drag a sprite with finger example
'Code by Dav, JAN/2017
GET SCREEN SIZE sw,sh
OPTION BASE 1
OPTION SPRITE POS CENTRAL
GRAPHICS
REFRESH OFF
'draw blue box sprite
GRAPHICS CLEAR 0,0,1
DRAW RECT 2,2 TO 98,198
SPRITE "b" SCAN 0,0,100,200
SPRITE "b" SHOW
SPRITE "b" AT 0,0
'draw red box sprite
GRAPHICS CLEAR 1,0,0
DRAW RECT 2,2 TO 98,198
SPRITE "c" SCAN 0,0,100,200
SPRITE "c" SHOW
SPRITE "c" AT SCREEN_WIDTH()/2,SCREEN_HEIGHT()/2
GRAPHICS CLEAR 0,0,0
REFRESH ON
DRAW TEXT "Drag blue box with finger" AT 1,1
DRAW TEXT "Don't collide with red one" AT 1,30
DO
GET TOUCH 0 AS tx,ty 'get first touch
'if user presses on the blue sprite
IF SPRITE_HIT("b", tx,ty) THEN
'get sprite x,y location
GET SPRITE "b" POS sx,sy
DO
GET TOUCH 0 AS tx2,ty2 'get finger drag
IF tx2=-1 THEN BREAK 'break loop if up
'save current x,y pos of sprite
GET SPRITE "b" POS sx2,sy2
SPRITE "b" AT tx2-tx+sx,ty2-ty+sy
IF SPRITES_COLLIDE("b","c") THEN
' blue sprite is vertically coincident with red sprite
' blue sprite is left of red sprite
IF sy2 >sh/2-200 AND sy2<sh/2+200 AND sx2<=sw/2-100 THEN
SPRITE "b" AT sw/2-101,sy2
ENDIF
' blue sprite is vertically coincident with red sprite
' blue sprite is right of red sprite
IF sy2 >sh/2-200 AND sy2<sh/2+200 AND sx2>=sw/2+100 THEN
SPRITE "b" AT sw/2+101,sy2
ENDIF
' blue sprite is horizontally coincident with red sprite
' blue sprite is above red sprite
IF sx2 >sw/2-100 AND sx2<sw/2+100 AND sy2<=sh/2-200 THEN
SPRITE "b" AT sx2,sh/2-201
ENDIF
' blue sprite is horizontally coincident with red sprite
' blue sprite is below red sprite
IF sx2>sw/2-100 AND sx2<sw/2+100 AND sy2>=sh/2+200 THEN
SPRITE "b" AT sx2,sh/2+201
ENDIF
BEEP
BREAK
END IF
UNTIL 0
END IF
SLOWDOWN
UNTIL 0