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 > Coding fireballs

#9074 - KoaM - Sun Jul 27, 2003 11:47 pm

Hello,

I'm working on a simple shooter using C for the GBA. At the moment, I have a sprite for the spaceship and one for the fireblast. The fireblast is off the screen so it won't show. My question is, what would be the best way to code the fireblast when the player presses the "B" button for example?

#9075 - Daikath - Mon Jul 28, 2003 12:22 am

take a global variable and make it update once per screen by putting this into the wait for vblank function.

Code:

if (globelvar2 = 2)
{
globelvar++;
}


Take a second global variable , declare it as 0 and do this.
Code:
if (!(KEYS)&(KEY_B)
{
     func();
     globelvar2 = 1;
}


and then
Code:

void func(void)
{
if (globalvar2 = 1)
{   
      blastcoorx = shipcoorx;  /*these are better off as global variables too unless there in the same function block but this is just an example.*/   

   globalvar = shipcoory + 2; /*or more to make it appear to appear to be shot from the front of the plane instead of the middle/*
globalvar2 = 2;
}
blastcoory = globalvar;
}


untested code by a relative n00b but i dont see any reason why it shouldt work.
_________________
?There are no stupid questions but there are a LOT of inquisitive idiots.?

#9078 - KoaM - Mon Jul 28, 2003 1:00 am

Cool thanks, I'll test that out. Love you your sig btw.

#9089 - Daikath - Mon Jul 28, 2003 11:45 am

just foudn out it had to be == instead of = in the if parts, doh :).
_________________
?There are no stupid questions but there are a LOT of inquisitive idiots.?

#9276 - KoaM - Thu Jul 31, 2003 8:44 pm

Well, i got it to work, but when i try more than one fireball, i gets stuck in an endless loop. Here's what i have, notice anything wrong?

This is my code for when you shoot:

Code:

if(!(*KEYS & KEY_B))
{
   u16 loop;
   u16 shoot;
   shoot = 0;
      while (shoot != 1){
         for (loop=0; loop < 10; loop++)
         {
         if(blast[loop].active == 0) {
         blast[loop].active = 1;
         blast[loop].x = ship1.x;
         if (ship1.y > 16)
            blast[loop].y = ship1.y - 16;
         shoot == 1;   
         }
         }
      }   
   fireBlast();
}


Here's the fireBlast code:

Code:

void fireBlast()
{
   u16 z;
   for(z = 0; z < 10; z++) {
      if(blast[z].active == 1) {
         if ((blast[z].y - 8) > 0) {
            blast[z].y -=  8;
         }
         else {
            blast[z].y = 0;
            blast[z].x = 240;
            blast[z].active = 0;
         }
      MoveSprites(&sprites[z+1],blast[z].x,blast[z].y);
      }

   }

}



Here's the struct definition for blast
Code:

typedef struct
{
   u16 x;         //x and y position on screen
   u16 y;
   u16 spriteFrame[1];     //animation frame storage
   int activeFrame;        //which frame is active
   u16 OAMSpriteNum;       //which sprite referring to
   u8 active;
}Sprite;


And here's how it calls the feirBlast() function in my main()
Code:

while(1)
{
GetInput();
MoveSprites(&sprites[0],ship1.x,ship1.y);
fireBlast();
WaitForVsync();
CopyOAM();
}

#9278 - mtg101 - Thu Jul 31, 2003 9:04 pm

From a quick glance, I'm guessing the problem is with the first block of code - it's the only one with a while loop that might cause a problem.

A second glance made me wonder why there's a while loop there at all - why not use:

Code:

if(!(*KEYS & KEY_B))
{
   for (u16 loop=0; loop < 10; loop++)
   {
      if(blast[loop].active == 0)
      {
         blast[loop].active = 1;
         blast[loop].x = ship1.x;
         if (ship1.y > 16)
            blast[loop].y = ship1.y - 16;
         break;
      }
   }
   fireBlast();
}


This way you can't get stuck in an infinite loop if blast[loop].active is never equal to 0.
_________________
---
Speaker for the Dead

#9286 - KoaM - Fri Aug 01, 2003 12:19 am

I guess i should explain this a bit. The way it works is that i have 10 "blasts" that are set off screen. Everytime someone shoots, i want just one of those fireblast to move right in front of the ship and scroll up until they reach the end of screen.

The problem is, when one shot is fired, the player can shoot again. He can shoot up to 10 shots. The reason the while is there is because i only want one fire blast to go at a time. If i don't have the while, all 10 would go off right away.

Maybe i'm going at this the wrong way. Is there an easier way to code multiple fire blasts? Can i create instances of "blast" dynamically instead?

#9289 - tepples - Fri Aug 01, 2003 12:49 am

Here's what I'd do: Make bullet and blast subtypes of a general type. Then turn a bullet into a blast when it hits something, and destroy it when the blast animation is over.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#9297 - KoaM - Fri Aug 01, 2003 4:02 am

See my problem lies in the fact that my ship can blast more than one shot at a time in other words, you'll see something like this:


| > > > > >

Where | is the ship and the > are the blasts. There are more than one blast, i'm trying to figure out how to have more than one blast at a time.

#9299 - tepples - Fri Aug 01, 2003 5:04 am

ok, what you called "blasts" I called "bullets", and I assumed "blasts" were explosions when the bullets hit something. I'll refer to them as "projectiles" to ease confusion.

Make an array of 10 projectiles, each a struct with live/dead state, animation phase, position, and velocity. Then when searching for collisions, test each projectile against each enemy that it could hit.
_________________
-- Where is he?
-- Who?
-- You know, the human.
-- I think he moved to Tilwick.

#9305 - mtg101 - Fri Aug 01, 2003 10:04 am

KoaM wrote:
If i don't have the while, all 10 would go off right away.


But using a while loop could cause the lock-up you're seeing. If you just break from the loop, as my code above shows, you get the same effect but without the possibility of an infinite loop.
_________________
---
Speaker for the Dead

#9316 - KoaM - Fri Aug 01, 2003 1:00 pm

"ok, what you called "blasts" I called "bullets", and I assumed "blasts" were explosions when the bullets hit something. I'll refer to them as "projectiles" to ease confusion.

Make an array of 10 projectiles, each a struct with live/dead state, animation phase, position, and velocity. Then when searching for collisions, test each projectile against each enemy that it could hit"

Sorry for the confusion, yes my blasts means your bullets.

#9317 - KoaM - Fri Aug 01, 2003 1:03 pm

mtg101: You sir, are the greatest man ever!

Thank you all for your help and thanks for accepting my n00b skillz.

#9342 - KoaM - Sat Aug 02, 2003 1:50 am

[Images not permitted - Click here to view it]

:)