You're just starting, right? Before worrying about how effective or efficient your method is, just worry about getting out something that works. SmileBASIC is fast enough that you don't have to worry too much about efficiency, and it'll just take up your time when you could be learning how to implement the other things in your game.
It works well, but you can't move diagonally. If you want to move diagonally, you'll either have to add more IF statements for things like B==(#UP OR #RIGHT), or switch to a different method of determining direction. The BUTTON() function returns a number which represents all of the buttons pressed down combined, so it can be both the #UP and #RIGHT values (kind of). This means that B will not simply be #UP or #RIGHT, it'll be both. I can go over why this is the case if you'd like, but basically what you want is to change B==#UP and the like to B AND #UP. This way, even if there are other directions being input, it will still catch that there is an #UP in there somewhere.
Basic Character Controller
Root / Programming Questions / [.]
GrahamCreated:
Hey guys! I picked up SmileBASIC about a week ago, and I've been combing the Internet for all kinds of tutorials. Today, I was able to make my very first 'character controller' that used the D-pad to move a sprite around the top screen.
It's a bit rough around the edges, but I was wondering if you guys could take a look at it and give me some tips. I wanna make sure I'm programming this using an effective method.
The code can be found at: 4K3N45ZM
B==(#UP AND #RIGHT)#UP AND #RIGHT is zero, so the expression above is the same as B==0. OR is a better choice for that expression.
I downloaded your code and saw your controller. Here are some observations:
-You should use IF B AND #DIRECTION THEN instead of ==. The B button obtain the button that are pressed, but each button is represented by a bit of the number. That's why each button constant can be obtained by a power of 2.
For example, the first 4 bits represent the direction of the variable. If the user input up and right then the variable will be like this:
1001
With the AND operation I evaluate if a bit is 1 on the button variable. For example, if I want to detect if the user pressed right(1000):
1001 AND 1000 = 1000.
Since the operation above isn't 0, it's considered as true.
-Call VSYNC at the end of the loop.
-You shouldn't hard code the speed. Store it on a variable in case you want to change it.(That way, you will change one line instead of four).
-Use indentation. With this I mean put space on blocks between if and endif, while and wend, for and next, def and end, and many others. With that, your code will be easier to read.
Here is the code with my tips applied:
VAR SPEED=1 WHILE PLAY==1 IF B AND #UP THEN CHRY=CHRY-SPEED IF B AND #DOWN THEN CHRY=CHRY+SPEED IF B AND #LEFT THEN CHRX=CHRX-SPEED IF B AND #RIGHT THEN CHRX=CHRX+SPEED SPOFS 0,CHRX,CHRY VSYNC 1 WEND