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

Grab a coin in a side-scroller game and have it disappear without any errors

Root / Programming Questions / [.]

ssunlimitedCreated:
So I have been working on a side-scroller project. I manage to put coins on the screen and with bgofs for backgrounds and spofs for things on screen including coins the character scrolls through the screen going further. But when I get a coin it gets me an error due to me using spofs. I used a bgclr and even made the spofs of the coin's y to be 999 but both give me I think 2 different errors due to using spofs for the coin. How can I do that?

Hi ssunlimited,
But when I get a coin it gets me an error due to me using spofs
The context is a bit unclear. Do you mind posting some code and the errors associated. Are saying that when you collect the coin (detect a collision with the player), the game throws an error associated with SPOFS? Or that the offset needed to place the coin with a scrolling background is causing issues? What happens if you limit the coin and player to one screen (no scrolling)?

When I have the coin on screen without scrolling, no errors happen when I collect the coin( collision the coin, clear it from the screen and get a point). The issues is with SPOFS. The SPOFS X variable changes every time I press the right button. After I collect the coin if I go right I get an error at SPOFS.

That sounds strange. I think you are going to have to post some code and the error specifically. Are you accidentally accessing SPOFS on the coin sprite you cleared when you move right? I could speculate that when you get a button input and move your player, you are also testing for collision against the coin. If the sprite no longer is in memory, you might get errors. But that's pure guesswork, I need to see your code.

Are you accidentally accessing SPOFS on the coin sprite you cleared when you move right? I could speculate that when you get a button input and move your player, you are also testing for collision against the coin. If the sprite no longer is in memory, you might get errors. But that's pure guesswork, I need to see your code.
This is exactly what I was thinking when I read this problem.

Yeah I am accessing the cleared memory of the coin, the coin that was cleared. It's because I've made a script that when you press the right button every coin that was there goes left. How do I change it? I want it like to test that the there is a coin memory and so if there is, move the coin left when pressing the right button, if there isn't, do nothing. Here is the code part:
B=BUTTON()
IF B AND #RIGHT THEN COIN1X=COIN1X-10 COIN2X=COIN2X-10 X=X+6 X2=X2-10 X1=X1+6
SPOFS 1, COIN1X, COIN1Y
SPOFS 2, COIN2X, COIN2Y
FOR I=1 TO 2
IF SPHITSP(0,I)==TRUE THEN SPCLR I COINS=COINTS+I
NEXT

Hi ssunlimited, I'd suggest breaking some of the logic bits of your input out into functions that follow after you've captured input. For example:
DIM COINSP[2]  ' This will keep track of which coins you've collected
COINSP[0] = 1
COINSP[1] = 1

WHILE NOT DONE
  B=BUTTON()
  IF B AND #RIGHT THEN 
    COIN1X=COIN1X-10 
    COIN2X=COIN2X-10
  ENDIF
 
  COLLECT_COIN
WEND

' This is where the logic for collision will go.  But first make sure the coin isn't already collected
DEF COLLECT_COIN
  VAR I =0
  FOR I =0 TO 1
    IF COINSP[I] THEN
      SPOFS I, VAR("COIN"+STR$(I+1)+"X"), VAR("COIN"+STR$(I+1)+"Y")  ' This isn't a great way to do this, you should move your coin x,y variables into an array or something
      IF SPHITSP(O, I)==TRUE THEN
        SPCLR I
        COINS=COINTS+I
        COINSP[I]= 0 ' This makes sure that you don't evaluate this SP again next time
    ENDIF
  NEXT
END
I didn't test the above, just off the top of my head. But it should show you how to isolate testing the sprite by keeping track of whether you've already collected it. I'm typing this as I'm getting ready to head out the door. Sorry for the briefness, but let me know if you don't understand something, and I'll get back on later.

Can you explain to me the parts WHILE NOT DONE and STR$(I) meaning?

STR$() makes any value a string. That way, it could be concatenated with other strings. Combining a string and a value for printing won't work, so you need to make the value a string. Same goes for other things requiring strings. WHILE NOT DONE makes the stuff between that and WEND keep going until DONE does not equal 0, or false.

Why won't the loop finish? Why do those 2 SPOFS arguments take those arguments?

The loop won't finish because DONE needs to equal something other than 0. WHILE means the stuff between WHILE and WEND will keep going in a loop as long as the arguments after WHILE are being met. NOT means does not equal. DONE is the variable. Having a variable by itself like that for IFs or WHILEs is like saying "DONE==TRUE", true being boolean for 1 and false being boolean for 0. As for the sprites, I am not very experienced with them, so I am not the guy to ask for help about them. Sorry This explanation is probably terrible.

Sorry ssunlimited, the confusion is my fault. There should be a definition of DONE at the top of the program. like:
VAR DONE = 0
joelable's explanation about WHILE loops is great, I think the confusion is with the NOT DONE portion. Maybe I introduced a concept you weren't familiar with. I added the WHILE loop to differentiate where the main program loop is, in comparison to where you might define additional functions, like COLLECT_COIN. So the SPOFS function in your original examples used variable like COIN1X and COIN2X, and the only thing different about them was the numerical component, the 1 or 2. Using that knowledge I was just reconstructing the variable names based on the value of I in the FOR loop. So consider the following:
VAR I = 1
SPOFS I, VAR("COIN"+STR$(I)+"X"), VAR("COIN"+STR$(I)+"Y")
SPOFS 1, COIN1X, COIN1Y
Both SPOFS will do the same thing. The first one builds the variable name from a series of strings, the second one is explicit. I'll break down the string concatenations: STR$(I) changes 1 into "1". So if you substitute that in it becomes: "COIN"+"1"+"X" which equals "COIN1X". VAR() is used to evaluate variables by string name so: VAR("COIN1X") will return the value of COIN1X. All of that to say, this was probably a confusing example, and I should've kept it simpler. Did you understand the purpose of the COINSP[] array in my previous example? Maybe we should get back to the problem at hand, which is only running SPOFS on sprites that exist. You need a way to keep track of which coins have been collected. That's what the COINSP was trying to do.