gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Coding > displaying coins in a game

#18339 - jenswa - Wed Mar 24, 2004 2:58 pm

I want to use coins in my game,

so i thought of the following structure,
the coins will have an x and y position (integer) and a boolean to check wether it was taken by the player or not.
I display a maximum number of coins on the screen, 10 coins is the max currently.
So there could be a hundred coins in a level, but only 10 can be on screen.

So i wrote this code (see: zip-file)
to display a max of tens coins on the screen, i started out with less coins, like 4 as the max first, then 8, then 10.
And i had 20 coins in the level, this was working for me. So the code worked fine, i increased the number of coins in te level
to 100 and that didn't work. So i decreased it to 40, that works. So i then increased to 60 and that didn't work.
Now the maximum number of coins for a level is stuck at 40 (actually 45).

The problem which arises when i go above 45, first of all the file compiles fine, but when i run it on visualboyadvance,
nothing happens, the file gets loaded, but doesn't display the first coin, like it does when i set the maxcoins to 40.

So i tried to get around this problem by increasing by compiling the file with 40 and manually adding 1 in the game loop itself,
that gave me a strange artifact (see: zip-file). Som extra coins are added and move with a multiple speed of the original coins.

My earlier attempt was to use the OAMentries 20 ~ 80 for coins (since i am not using them anyway),
but that gave more or less the same problem, it also got stuck at a number of fourty.

Can anyone help me with this problem?
(a fix or a new solution would probably be best)

Oh i forgot to tell, all is compiled as multiboot, i also tried a normal binairy, but both have the same problem.

Thanks Jenswa


attachments:

http://www.geocities.com/gouwevrouwe/coins.zip [62 k]
(code included, working file and a not working file, all other sources need and the artifact)


PS:
I've an old version of devkitadvance
_________________
It seems this wasn't lost after all.

#18342 - DekuTree64 - Wed Mar 24, 2004 5:25 pm

Well, for one thing, here
Code:
   for(z=10; z >= n; z--){
      clones[n].x = 240;
      clones[n].y = 160;
   }

you're setting one past the end of the array the first time through the loop, which is bound to cause teouble eventually. Start at 9 and loop down. Or start at n and loop up while <10, that would be simpler.
...now that I look at it, you're NOT setting one past the end of the array, because you're setting clones[n] instead of z, so you'll probably want to change that. You might want to set them to be dead too, although it shouldn't really matter.
Also, in loop before that, make sure to check if n >= 10 and break if so, just incase.
Not sure what else could be wrong with it.
_________________
___________
The best optimization is to do nothing at all.
Therefore a fully optimized program doesn't exist.
-Deku

#18369 - LOst? - Thu Mar 25, 2004 1:19 am

DekuTree64 wrote:
Well, for one thing, here
Code:
   for(z=10; z >= n; z--){
      clones[n].x = 240;
      clones[n].y = 160;
   }

you're setting one past the end of the array the first time through the loop, which is bound to cause teouble eventually. Start at 9 and loop down. Or start at n and loop up while <10, that would be simpler.
...now that I look at it, you're NOT setting one past the end of the array, because you're setting clones[n] instead of z, so you'll probably want to change that. You might want to set them to be dead too, although it shouldn't really matter.
Also, in loop before that, make sure to check if n >= 10 and break if so, just incase.
Not sure what else could be wrong with it.


Yea don't confuse the actual loop steps with the array index. Then you will get into trouble.

A little C hint:
Code:

   int array_index = 10;
   int step = 0;
   
   while (--array_index >= 0, ++step <= 10)
   {
      printf("array_index == %d. Step == %d.\n", array_index, step);
   }


Quote:

output:
array_index == 9. Step == 1.
array_index == 8. Step == 2.
array_index == 7. Step == 3.
array_index == 6. Step == 4.
array_index == 5. Step == 5.
array_index == 4. Step == 6.
array_index == 3. Step == 7.
array_index == 2. Step == 8.
array_index == 1. Step == 9.
array_index == 0. Step == 10.

#18385 - jenswa - Thu Mar 25, 2004 11:38 am

Thanks for pointing out, i just copied and pasted that code from the apart above and forgot to change the n in z and 10 in 9 to start with.

However i still can't get past the limit.
_________________
It seems this wasn't lost after all.

#18454 - LOst? - Fri Mar 26, 2004 8:26 pm

jenswa wrote:

However i still can't get past the limit.


Sometimes it helps to revrite the whole code for the thing that's not working.
I'm afraid of using while and for loops in GBA games too much because I'm afraid it will take longer than the 60Hz of a frame.

Having 10 coins visible is decreasing that time so you can do other stuff, but still I don't like the idea of only showing 10 when there may be more on the screen. But then again I don't know what you're game design is so.

#18481 - jenswa - Sat Mar 27, 2004 12:49 pm

Thanks,

i think i'll do that again, when i've some time over.
So i may be using too many for loops?
Well i just find it weird that the bin file doesn't work at all.

back to the drawing board.
_________________
It seems this wasn't lost after all.

#18563 - LOst? - Sun Mar 28, 2004 11:17 pm

jenswa wrote:

So i may be using too many for loops?


No not at all.