LoginLogin

How do I use the touch screen/bottom screen?

Root / Programming Questions / [.]

RNGesusCreated:
Touch Screen/Bottom Screen Tutorial Hello everyone, I have decided to make a tutorial about using the bottom screen. Let's start. First copy all of this code into SmileBASIC:
ACLS:BGMSTOP:XSCREEN 2:SYSBEEP=0:OPTION STRICT

@TCHSCRNSET
DISPLAY 1
SPSET 0,0,0,0,0
VAR TM
VAR TX
VAR TY
DISPLAY 0

@LOOP
GOSUB @TCHSCRN
VSYNC 1
GOTO @LOOP

@TCHSCRN
DISPLAY 1
TOUCH OUT TM,TX,TY
SPOFS 0,TX,TY
IF TM<=0 THEN SPOFS 0,-1,-1
DISPLAY 0
RETURN
What this does is set up a 1*1 sprite on the bottom screen and moves it to where you are touching the bottom screen. And if you aren't touching the bottom screen, then the sprite is moved off of the screen. The 1*1 sprite is invisible, so you can't see it. You can use this by using SPHIT or SPHITSP. Let's break it down
ACLS:BGMSTOP:XSCREEN 2:SYSBEEP=0:OPTION STRICT
What this does is clear the screen, stops all music, turns off the keyboard, turns of system noises, and makes variables have to be set using VAR.
@TCHSCRNSET
DISPLAY 1
SPSET 0,0,0,0,0
VAR TM
VAR TX
VAR TY
DISPLAY 0
What this part does is that it makes a place in the code called @TCHSCRNSET. Then it tells SmileBASIC to use the bottom screen for all of the following commands. And then it sets up a 1*1 invisible sprite on the bottom screen. Then it sets the variables TM, TX, and TY as 0. Then it tells SmileBASIC to use the top screen for all of the following commands.
@LOOP
GOSUB @TCHSCRN
VSYNC 1
GOTO @LOOP
What this does is that is makes a place in the code called @LOOP. Then it tells SmileBASIC to go to the place called @TCHSCRN and return to the the this command once the command RETURN is given. VSYNC 1 tells SmileBASIC to wait 1/60 of a second. Then it tells SmileBasic to goto the place called @LOOP but won't return if the command RETURN is given.
@TCHSCRN
DISPLAY 1
TOUCH OUT TM,TX,TY
SPOFS 0,TX,TY
IF TM<=0 THEN SPOFS 0,-1,-1
DISPLAY 0
RETURN
What this does is that it makes a place in the code called @TCHSCRN. Then it tells SmileBASIC to use the bottom screen for all of the following commands. Then it makes TM equal the amount of time the screen is touch, TX equal the x-coordinate of where the screen is touched, and TY equal the y-coordinate of where the screen is touched. Then is moves the 1*1 sprite at the coordinates of where the screen is touched. Then it tells SmileBASIC if the screen isn't being touched then put the 1*1 sprite off of the screen. Then it tells SmileBASIC to use the top screen for all of the following commands. Then it tells SmileBASIC to go back to where the GOSUB command was used. I hope this tutorial was helpful! There is also a shorter way which does the same thing.
@TCHSCRN
DISPLAY 1
VAR TM
VAR TX
VAR TY
IF TM==0 AND TX==0 AND TY==0 THEN SPSET 0,0,0,1,1
TOUCH OUT TM,TX,TY
IF TM==0 THEN TX=-1:TY=-1
SPOFS 0,TX,TY
DISPLAY 0
RETURN 

You don't HAVE TO use a sprite for the touchscreen. It does look nice and is probably how you want to do it, but you can also use SPHITRC.
SPHITRC( BUTTON_ID, TX-5, TY-5, 10, 10 )
Will check if the user is touching a button with a 5 pixel snapping range.

Yes, but that is harder to deal with AND it's not as exact.

How so? It's in the same coordinate system. You pass all the same arguments to your invisible sprite DEF. This consolidates those commands in a sense. The math behind the scenes is the exact same. You can change the size of your box to make it snap more or less as well.

While I generally agree with kldck_hul, there is still one advantage to using a sprite - if you have 50 buttons, you'd only need to use one command and you will know which button was hit. If you use SPHITRC, you'd have to do it for each button separately.

@NeatNit Very good point. I've been doing a lot of non-sprite buttons, and that FOR loop has been easiest to implement there; this was not something I was thinking about.

While I generally agree with kldck_hul, there is still one advantage to using a sprite - if you have 50 buttons, you'd only need to use one command and you will know which button was hit. If you use SPHITRC, you'd have to do it for each button separately.
Not true, SPHITRC allows specifying a range of sprites, and/or a collision mask.

While I generally agree with kldck_hul, there is still one advantage to using a sprite - if you have 50 buttons, you'd only need to use one command and you will know which button was hit. If you use SPHITRC, you'd have to do it for each button separately.
Not true, SPHITRC allows specifying a range of sprites, and/or a collision mask.
Welp, you're right. I must have had it mixed up with the Petit Computer SPHITRC, whoops!

How do i make a sprite detect if its touching the touch screen sprite? SPCOL and SPHITSP is not working for me.