LoginLogin

Showing sprite hitboxes

Root / Submissions / [.]

PerskaCreated:
SmileBASIC does not provide a native method of drawing sprite hitboxes, but this is really easy to implement.
DEF SHOW_HITBOX ID%, COL%
 VAR X%,Y%,W%,H%,XX%,YY%,S%,SX%=1,SY%=1
 SPOFS ID% OUT XX%,YY%
 SPCOL ID% OUT X%,Y%,W%,H%,S%
 IF S% THEN SPSCALE ID% OUT SX%,SY%
 GBOX XX%+X%*SX%,YY%+Y%*SY%,XX%+(X%*SX%+W%*SX%-1),YY%+(Y%*SY%+H%*SY%-1)
END
Usage: SHOW_HITBOX Management number, drawing color Example:
SPSET 0,0
SPCOL 0,1
SHOW_HITBOX 0,#GREEN
SPOFS 0,200,120
SHOW_HITBOX 0,#RED
You should see a small green box at the top-left corner, and a big red box somewhat at the center of the screen. If you are going to run this in a loop, I suggest clearing the graphics screen every frame.

Nice function, but why you writing % after variable name? Smile Basic does'nt need it. You can type just variable name. And there are no need in VAR function either, you can just write SX=1:SY=1 in that line. Every variable creating when u use it, even if it not declared

Replying to:ran4erep
Nice function, but why you writing % after variable name? Smile Basic does'nt need it. You can type just variable name. And there are no need in VAR function either, you can just write SX=1:SY=1 in that line. Every variable creating when u use it, even if it not declared
some people like type restrictions

Replying to:ran4erep
Nice function, but why you writing % after variable name? Smile Basic does'nt need it. You can type just variable name. And there are no need in VAR function either, you can just write SX=1:SY=1 in that line. Every variable creating when u use it, even if it not declared
VAR explicitly declares variables in the local scope. This will prevent conflict with any globals sharing the same name. Using the % variable suffix is notation for explicit integer type. It's a little more efficient to use integers for integer operations rather than convert between them and the default (real number) type. For function parameters it also provides a hint as to what arguments are expected. If COL were a string (COL$) you might guess that it takes a color name instead of a packed RGB value. What SmileBASIC "does'nt need" [sic] and what it is good practice to do are different things. % suffix is usually messy and should be omitted in examples, yes, but this resource is 2 years old and that was the convention back then.

If you put a $ at the end of a variable it is a string (example: VAR s$ = "Hello World") If you put a # at the end of a variable it is a floating point number (example: VAR s# = 3.14159) If you put a % at the end of a variable it is an integer (example: VAR s% = 3) If you put OPTION STRICT at the top of your code SmileBasic will require that variables are declared before being used. Using the type specifiers might make the code somewhat ugly, but they can really save you when it comes time to debug things. You can spend hours tracing down something only to find that you misspelled something, or used the wrong data type. Then when you fix it you won't even feel triumphant, just dumb for not having done things right in the first place. In the absence of a better option I use them in my own code. On topic, it looks like a nice and useful code snippet, nice work.

Replying to:seggiepants
If you put a $ at the end of a variable it is a string (example: VAR s$ = "Hello World") If you put a # at the end of a variable it is a floating point number (example: VAR s# = 3.14159) If you put a % at the end of a variable it is an integer (example: VAR s% = 3) If you put OPTION STRICT at the top of your code SmileBasic will require that variables are declared before being used. Using the type specifiers might make the code somewhat ugly, but they can really save you when it comes time to debug things. You can spend hours tracing down something only to find that you misspelled something, or used the wrong data type. Then when you fix it you won't even feel triumphant, just dumb for not having done things right in the first place. In the absence of a better option I use them in my own code. On topic, it looks like a nice and useful code snippet, nice work.
Personally I writing only dollar sign to string variables. And don's using percent sign to indicate integers, integers and floating points in my code are without any signs.

You missed the COL% in GBOX instructions. It will use GCOLOR(), which is the default if the color argument is omitted.