LoginLogin
Nintendo shutting down 3DS + Wii U online services, see our post

Day 1 (Help plz)

Root / Programming Questions / [.]

Craftman780Created:
DAY 1 OF USING SMILE BASIC 4 (Switch) I've implimented movement.
'---------------------------------------------------------------------'
'                                 MOVEMENT'
CLS
DX=185,DY=115
SPSET 0,0:SPOFS 0,DX,DY
LOOP
IF BUTTON(1,#B_LLEFT)==1 THEN DX=DX-1
IF BUTTON(1,#B_LRIGHT)==1 THEN DX=DX+1
IF BUTTON(1,#B_LUP)==1 THEN DY=DY-1
IF BUTTON(1,#B_LDOWN)==1 THEN DY=DY+1
IF DX>360 THEN DX=360
IF DX<0 THEN DX=0
IF DY<0 THEN DY=0
IF DY>220 THEN DY=220
SPOFS 0,DX,DY
VSYNC
ENDLOOP
'---------------------------------------------------------------------'
It sets the sprite of a strawberry in the middle of the screen (roughly 185 (direction X) and 115 (direction Y)) and using the joy-con's right directional buttons, I can move the strawberry around the screen which will then stop if it reaches the certain threshold. (see DX,DY) However, If I say want to add right below it (I have it marked out to make it easier to see what's what) like a beep function for the A button like so:
'---------------------------------------------------------------------'
'                                 MOVEMENT'
CLS
DX=185,DY=115
SPSET 0,0:SPOFS 0,DX,DY
LOOP
IF BUTTON(1,#B_LLEFT)==1 THEN DX=DX-1
IF BUTTON(1,#B_LRIGHT)==1 THEN DX=DX+1
IF BUTTON(1,#B_LUP)==1 THEN DY=DY-1
IF BUTTON(1,#B_LDOWN)==1 THEN DY=DY+1
IF DX>360 THEN DX=360
IF DX<0 THEN DX=0
IF DY<0 THEN DY=0
IF DY>220 THEN DY=220
SPOFS 0,DX,DY
VSYNC
ENDLOOP
'---------------------------------------------------------------------'

LOOP
IF BUTTON(1,B_A,1) THEN BEEP 6
ENDLOOP
Nothing happens... All the code works just fine (no errors) it's just when I try to press the A button to get it to beep, nothing happens. I would really love some help.

The button code for A is #B_RRIGHT You don't want to put the beep code outside the main loop. Say your main loop is movement code. If your beep code is directly below it (outside the loop), and the program never exits the movement loop above, then the beep code would never run, and thus no sound occurs.
CLS
DX=185,DY=115
SPSET 0,0:SPOFS 0,DX,DY
LOOP'main loop
'movement
IF BUTTON(1,#B_LLEFT)==1 THEN DX=DX-1
IF BUTTON(1,#B_LRIGHT)==1 THEN DX=DX+1
IF BUTTON(1,#B_LUP)==1 THEN DY=DY-1
IF BUTTON(1,#B_LDOWN)==1 THEN DY=DY+1
IF DX>360 THEN DX=360
IF DX<0 THEN DX=0
IF DY<0 THEN DY=0
IF DY>220 THEN DY=220
SPOFS 0,DX,DY
'beep
IF BUTTON(1,#B_RRIGHT) THEN BEEP 6
VSYNC
ENDLOOP
I've only used SB4 for a bit, but isn't the first parameter of BUTTON the Controller ID? You want to make sure you're checking for input from the right controller as well, but I might be wrong about that. (I don't know what sort of XCTRLSTYLE you've got going on.)

Keep in mind that code is executed from top to bottom. So, your movement code runs, but then it hits the first ENDLOOP, making it go back up to LOOP. That first loop goes on forever, which means that SmileBASIC actually never reaches your second loop where you try to check the A Button. When it comes to interactive programs like this, you'll usually just have one big main loop. Adding another loop after it would mean you want that loop to happen after you're totally done with the first loop, which isn't what you were trying to do.

The button code for A is #B_RRIGHT
The alias #B_A was added in an update, so both will work.
I've only used SB4 for a bit, but isn't the first parameter of BUTTON the Controller ID? You want to make sure you're checking for input from the right controller as well, but I might be wrong about that. (I don't know what sort of XCTRLSTYLE you've got going on.)
The left and right Joy-Cons together count as one controller if you're using them as a pair.

Ah, I see. I guess there are now #B_X,#B_Y,#B_UP, etc. counterparts too? Neat. If you've got the control style set to single handheld mode (attached to console/attached to grip) (XCTRLSTYLE 0 I believe), then the controller ID is irrelevant? I mean, I guess then it wouldn't really make sense to ask about states of nonexistent controllers, unless I'm mistaken about what the actual functionality of controller ID is.

The button code for A is #B_RRIGHT
The alias #B_A was added in an update, so both will work.
I've only used SB4 for a bit, but isn't the first parameter of BUTTON the Controller ID? You want to make sure you're checking for input from the right controller as well, but I might be wrong about that. (I don't know what sort of XCTRLSTYLE you've got going on.)
The left and right Joy-Cons together count as one controller if you're using them as a pair.
Yes. It can work in two ways
IF BUTTON(1,#B_A)==1 THEN BEEP 6

IF BUTTON(1,#B_A,1) THEN BEEP 6
in the first command, the == bit means it will continue for as long as the button is pressed, and will repeat. the second, then {designation},1 bit means it will only happen once which is helpful if you're trying to have it only beep once.

Keep in mind that code is executed from top to bottom. So, your movement code runs, but then it hits the first ENDLOOP, making it go back up to LOOP. That first loop goes on forever, which means that SmileBASIC actually never reaches your second loop where you try to check the A Button. When it comes to interactive programs like this, you'll usually just have one big main loop. Adding another loop after it would mean you want that loop to happen after you're totally done with the first loop, which isn't what you were trying to do.
So basicall,y Id have to keep all my code in a loop?

So basicall,y Id have to keep all my code in a loop?
Not necessarily.
CLS
DX=185,DY=115
SPSET 0,0:SPOFS 0,DX,DY

LOOP
 MOVEMENT
 DRAW
 NOISE
 VSYNC
ENDLOOP

DEF MOVEMENT
 IF BUTTON(1,#B_LLEFT)==1 THEN DX=DX-1
 IF BUTTON(1,#B_LRIGHT)==1 THEN DX=DX+1
 IF BUTTON(1,#B_LUP)==1 THEN DY=DY-1
 IF BUTTON(1,#B_LDOWN)==1 THEN DY=DY+1
 IF DX>360 THEN DX=360
 IF DX<0 THEN DX=0
 IF DY<0 THEN DY=0
 IF DY>220 THEN DY=220
END
DEF DRAW
 SPOFS 0,DX,DY
END
DEF NOISE
 IF BUTTON(1,#B_RRIGHT) THEN BEEP 6
END
Defining routines as functions can be very helpful for organization. Among other things

So basicall,y Id have to keep all my code in a loop?
Not necessarily.
CLS
DX=185,DY=115
SPSET 0,0:SPOFS 0,DX,DY

LOOP
 MOVEMENT
 DRAW
 NOISE
 VSYNC
ENDLOOP

DEF MOVEMENT
 IF BUTTON(1,#B_LLEFT)==1 THEN DX=DX-1
 IF BUTTON(1,#B_LRIGHT)==1 THEN DX=DX+1
 IF BUTTON(1,#B_LUP)==1 THEN DY=DY-1
 IF BUTTON(1,#B_LDOWN)==1 THEN DY=DY+1
 IF DX>360 THEN DX=360
 IF DX<0 THEN DX=0
 IF DY<0 THEN DY=0
 IF DY>220 THEN DY=220
END
DEF DRAW
 SPOFS 0,DX,DY
END
DEF NOISE
 IF BUTTON(1,#B_RRIGHT) THEN BEEP 6
END
Defining routines as functions can be very helpful for organization.
My code now looks like this
'---------------------------------------------------------------------'
'                                 MOVEMENT'
CLS
DX=185,DY=115
SPSET 0,0:SPOFS 0,DX,DY
LOOP
IF BUTTON(1,#B_LLEFT)==1 THEN DX=DX-1
IF BUTTON(1,#B_LRIGHT)==1 THEN DX=DX+1
IF BUTTON(1,#B_LUP)==1 THEN DY=DY-1
IF BUTTON(1,#B_LDOWN)==1 THEN DY=DY+1
IF DX>360 THEN DX=360
IF DX<0 THEN DX=0
IF DY<0 THEN DY=0
IF DY>220 THEN DY=220
SPOFS 0,DX,DY
VSYNC
'---------------------------------------------------------------'

'---------------------------------------------------------------'
'                                BEEP'
IF BUTTON(1,#B_A,1) THEN BEEP 2
'---------------------------------------------------------------'
ENDLOOP
Thanks! I will also try the defining bit :D

So basicall,y Id have to keep all my code in a loop?
Not necessarily.
CLS
DX=185,DY=115
SPSET 0,0:SPOFS 0,DX,DY

LOOP
 MOVEMENT
 DRAW
 NOISE
 VSYNC
ENDLOOP

DEF MOVEMENT
 IF BUTTON(1,#B_LLEFT)==1 THEN DX=DX-1
 IF BUTTON(1,#B_LRIGHT)==1 THEN DX=DX+1
 IF BUTTON(1,#B_LUP)==1 THEN DY=DY-1
 IF BUTTON(1,#B_LDOWN)==1 THEN DY=DY+1
 IF DX>360 THEN DX=360
 IF DX<0 THEN DX=0
 IF DY<0 THEN DY=0
 IF DY>220 THEN DY=220
END
DEF DRAW
 SPOFS 0,DX,DY
END
DEF NOISE
 IF BUTTON(1,#B_RRIGHT) THEN BEEP 6
END
Defining routines as functions can be very helpful for organization. Among other things
My code now looks like this. It is indeed much cleaner. :D
CLS
DX=185,DY=115
SPSET 0,0:SPOFS 0,DX,DY

LOOP
MOVEMENT
DRAW
NOISE
ENDLOOP

                                    'DEFINITIONS'
'-----------------------------------------------------------------'
DEF MOVEMENT
   IF BUTTON(1,#B_LLEFT)==1 THEN DX=DX-1
   IF BUTTON(1,#B_LRIGHT)==1 THEN DX=DX+1
   IF BUTTON(1,#B_LUP)==1 THEN DY-DY-1
   IF BUTTON)1,#B_LDOWN)==1 THEN DY=DY+1
   IF DX>360 THEN DX=360
   IF DX<0 THEN DX=0
   IF DY<0 THEN DY=0
   IF DY<220 THEN DY=220
 END
'-----------------------------------------------------------------'
DEF DRAW
   SPOFS 0,DX,DY
 END
'-----------------------------------------------------------------'
DEF NOISE
   IF BUTTON(1,#B_A,2) THEN BEEP 2
 END
'-----------------------------------------------------------------'
Now, what if I wanted to add an inventory? Like a black square with a white outline, and a pointer, while still using the directional buttons for selections? And just items in general like a sword that can be picked up just be moving across it?

Well, a simple inventory system might use a 2d array to store things.
DIM INVNTRY[8,16]
for example. 8 slots tall, 16 slots wide. You could store an internal "inventory index" to keep track of which inventory slot the user is hovering over, and that inventory index could be used to interpret a position of the sprites and so forth that would draw the inventory representation on the screen.
DIM INV[8,16],INV_SLOT_X=0,INV_SLOT_Y=0
DIM INV_SLOT_SIZE=8'size in pixels
LOOP
 _INPUT
 DRAW
 VSYNC
ENDLOOP

DEF _INPUT
 IF BUTTON(0,#B_LUP) THEN DEC INV_SLOT_Y ENDIF
 IF BUTTON(0,#B_LDOWN) THEN INC INV_SLOT_Y ENDIF
 IF BUTTON(0,#B_LLEFT) THEN DEC INV_SLOT_X ENDIF
 IF BUTTON(0,#B_LRIGHT) THEN INC INV_SLOT_X ENDIF
 INV_SLOT_X=MIN(MAX(0,INV_SLOT_X),16-1)'keep cursor in boundaries
 INV_SLOT_Y=MIN(MAX(0,INV_SLOT_Y),8-1)'---
END

DEF DRAW
 GCLS 0
 DIM X,Y,ISS=INV_SLOT_SIZE
 FOR Y=0 TO 8-1
  FOR X=0 TO 16-1
   IF X==INV_SLOT_X && Y==INV_SLOT_Y THEN
    GFILL X*ISS,Y*ISS,X*ISS+ISS,Y*ISS+ISS
   ELSE
    GBOX X*ISS,Y*ISS,X*ISS+ISS,Y*ISS+ISS
   ENDIF
  NEXT X
 NEXT Y
END
INV_SLOT_X and INV_SLOT_Y point to an element of INV, which is a 2 dimensional array. So, if we treat objects in the inventory as numbers, say, 3 is a sword, you can set and get values of the inventory by accessing them. INV[INV_SLOT_Y,INV_SLOT_X]=3 X and Y in the DRAW routine correspond to elements of the inventory, so you can use that to draw certain images in the inventory slots. Say, 3 also corresponds to the sprite number of the object.
DEF DRAW
 GCLS 0
 DIM X,Y,ISS=INV_SLOT_SIZE
 FOR Y=0 TO 8-1
  FOR X=0 TO 16-1
   IF X==INV_SLOT_X && Y==INV_SLOT_Y THEN
    GFILL X*ISS,Y*ISS,X*ISS+ISS,Y*ISS+ISS
   ELSE
    GBOX X*ISS,Y*ISS,X*ISS+ISS,Y*ISS+ISS
   ENDIF
   SPSET ManagementNumber,INV[Y,X]'definition number
   SPOFS ManagementNumber,X*ISS,Y*ISS
  NEXT X
 NEXT Y
END
Also, I haven't tested any of this code, so I might've made a mistake lol

Well, a simple inventory system might use a 2d array to store things.
DIM INVNTRY[8,16]
for example. 8 slots tall, 16 slots wide. You could store an internal "inventory index" to keep track of which inventory slot the user is hovering over, and that inventory index could be used to interpret a position of the sprites and so forth that would draw the inventory representation on the screen.
DIM INV[8,16],INV_SLOT_X=0,INV_SLOT_Y=0
DIM INV_SLOT_SIZE=8'size in pixels
LOOP
 _INPUT
 DRAW
 VSYNC
ENDLOOP

DEF _INPUT
 IF BUTTON(0,#B_LUP) THEN DEC INV_SLOT_Y ENDIF
 IF BUTTON(0,#B_LDOWN) THEN INC INV_SLOT_Y ENDIF
 IF BUTTON(0,#B_LLEFT) THEN DEC INV_SLOT_X ENDIF
 IF BUTTON(0,#B_LRIGHT) THEN INC INV_SLOT_X ENDIF
 INV_SLOT_X=MIN(MAX(0,INV_SLOT_X),16-1)'keep cursor in boundaries
 INV_SLOT_Y=MIN(MAX(0,INV_SLOT_Y),8-1)'---
END

DEF DRAW
 GCLS 0
 DIM X,Y,ISS=INV_SLOT_SIZE
 FOR Y=0 TO 8-1
  FOR X=0 TO 16-1
   IF X==INV_SLOT_X && Y==INV_SLOT_Y THEN
    GFILL X*ISS,Y*ISS,X*ISS+ISS,Y*ISS+ISS
   ELSE
    GBOX X*ISS,Y*ISS,X*ISS+ISS,Y*ISS+ISS
   ENDIF
  NEXT X
 NEXT Y
END
INV_SLOT_X and INV_SLOT_Y point to an element of INV, which is a 2 dimensional array. So, if we treat objects in the inventory as numbers, say, 3 is a sword, you can set and get values of the inventory by accessing them. INV[INV_SLOT_Y,INV_SLOT_X]=3 X and Y in the DRAW routine correspond to elements of the inventory, so you can use that to draw certain images in the inventory slots. Say, 3 also corresponds to the sprite number of the object.
DEF DRAW
 GCLS 0
 DIM X,Y,ISS=INV_SLOT_SIZE
 FOR Y=0 TO 8-1
  FOR X=0 TO 16-1
   IF X==INV_SLOT_X && Y==INV_SLOT_Y THEN
    GFILL X*ISS,Y*ISS,X*ISS+ISS,Y*ISS+ISS
   ELSE
    GBOX X*ISS,Y*ISS,X*ISS+ISS,Y*ISS+ISS
   ENDIF
   SPSET ManagementNumber,INV[Y,X]'definition number
   SPOFS ManagementNumber,X*ISS,Y*ISS
  NEXT X
 NEXT Y
END
Also, I haven't tested any of this code, so I might've made a mistake lol
It works for the most part, but it doesn't necessarily stay within the confines of the box. The white pixel also gets longer the further it goes to the right. It also goes out of the box when it 'reaches' the threshold.

It works for the most part, but it doesn't necessarily stay within the confines of the box. The white pixel also gets longer the further it goes to the right. It also goes out of the box when it 'reaches' the threshold.
Are you sure you copied the code over right? I tested it on my 3DS (in this case only the button codes are different in SB3) and it seems to work fine. Edit: I would double check the DRAW function. I condensed it as much I could for readability, but it's totally possible to mistake an x for a y and so on, which would explain what you're seeing.

It works for the most part, but it doesn't necessarily stay within the confines of the box. The white pixel also gets longer the further it goes to the right. It also goes out of the box when it 'reaches' the threshold.
Are you sure you copied the code over right? I tested it on my 3DS (in this case only the button codes are different in SB3) and it seems to work fine. Edit: I would double check the DRAW function. I condensed it as much I could for readability, but it's totally possible to mistake an x for a y and so on, which would explain what you're seeing.
I think it's different on the 3DS. I think it uses v3 instead of the now current 4 Also, how would I make a background? Like the interior of a house, then the door would take you to like an overworld type deal?

I think it's different on the 3DS. I think it uses v3 instead of the now current 4 Also, how would I make a background? Like the interior of a house, then the door would take you to like an overworld type deal?
I'm familiar with both SB3 and SB4, they are built very similarly (SB4 is basically an advanced SB3; same instructions, same syntax, etc., new functionalities). The code should work the same on both platforms. That sounds like something artistically specific that you might need to figure out by experimentation. I suggest looking around forum sites like this and looking at others' code to figure out what makes them tick.

DAY 1 OF USING SMILE BASIC 4 (Switch) I've implimented movement.
'---------------------------------------------------------------------'
'                                 MOVEMENT'
CLS
DX=185,DY=115
SPSET 0,0:SPOFS 0,DX,DY
LOOP
IF BUTTON(1,#B_LLEFT)==1 THEN DX=DX-1
IF BUTTON(1,#B_LRIGHT)==1 THEN DX=DX+1
IF BUTTON(1,#B_LUP)==1 THEN DY=DY-1
IF BUTTON(1,#B_LDOWN)==1 THEN DY=DY+1
IF DX>360 THEN DX=360
IF DX<0 THEN DX=0
IF DY<0 THEN DY=0
IF DY>220 THEN DY=220
SPOFS 0,DX,DY
VSYNC
ENDLOOP
'---------------------------------------------------------------------'
It sets the sprite of a strawberry in the middle of the screen (roughly 185 (direction X) and 115 (direction Y)) and using the joy-con's right directional buttons, I can move the strawberry around the screen which will then stop if it reaches the certain threshold. (see DX,DY) However, If I say want to add right below it (I have it marked out to make it easier to see what's what) like a beep function for the A button like so:
'---------------------------------------------------------------------'
'                                 MOVEMENT'
CLS
DX=185,DY=115
SPSET 0,0:SPOFS 0,DX,DY
LOOP
IF BUTTON(1,#B_LLEFT)==1 THEN DX=DX-1
IF BUTTON(1,#B_LRIGHT)==1 THEN DX=DX+1
IF BUTTON(1,#B_LUP)==1 THEN DY=DY-1
IF BUTTON(1,#B_LDOWN)==1 THEN DY=DY+1
IF DX>360 THEN DX=360
IF DX<0 THEN DX=0
IF DY<0 THEN DY=0
IF DY>220 THEN DY=220
SPOFS 0,DX,DY
VSYNC
ENDLOOP
'---------------------------------------------------------------------'

LOOP
IF BUTTON(1,B_A,1) THEN BEEP 6
ENDLOOP
Nothing happens... All the code works just fine (no errors) it's just when I try to press the A button to get it to beep, nothing happens. I would really love some help.
you might have a problem because you put the
BEEP 6
on the same line, try it on the second line or, 2.check the volume 3. turn the
LOOP
to
@LOOP
4.remove the
END LOOP
and replace it with a
GOTO @LOOP

3. turn the
LOOP
to
@LOOP
4.remove the
END LOOP
and replace it with a
GOTO @LOOP
LOOP and ENDLOOP are valid commands in SB4. I think this was already answered. Please read the above posts before posting yourself.