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.

Beginners > Help coding shooting

#116975 - Sabr3wulf - Wed Jan 31, 2007 1:49 pm

Such a noob question I know, but I'm working on my first project making a GBA game and can't work out how to get the ship to shoot properly. At the moment I've got:
Code:

   if(keyDown(KEY_A) && ylaser < yMin)
   {
   ylaser = 121;
   xlaser = 240;
   }
   else if (keyDown(KEY_A))
   {
   sprites[1].attribute0 = COLOR_256 | SQUARE | ylaser;
   sprites[1].attribute1 = SIZE_8 | xship + 4;
   ylaser--;ylaser--;
   }


which is probably terrible code. Obviously, with the code above, the player has to hold down the A to 'shoot' and keep it held down for the shot to move, how would I go about making it move by it's own after a key press.

There's probably a really easy way and I'll kick myself, cheers.

#116981 - QuantumDoja - Wed Jan 31, 2007 3:18 pm

Hi...just so I can understand a bit more...

There is a weapon that shoots a laser at a ship right?

The user presses "A" to shoot a laser...

If this is the case, make it so you could spawn a "laser" object when the user presses the button, then in your game loop, you could move the laser object(sprite) - or make it carry on in the direction it was fired in....
_________________
Chris Davis

#116983 - Sabr3wulf - Wed Jan 31, 2007 3:25 pm

Sorry, you control a ship and press A to fire a laser.

#116992 - ScottLininger - Wed Jan 31, 2007 5:16 pm

So here's a bit that should work with your existing code:

Code:

if(keyDown(KEY_A) && ylaser <= yMin) {
  ylaser = 121;
  xlaser = xship+4;
}
   
if (ylaser > yMin) {

  if (laserHitsEnemy()) {
    xlaser = 240; // this moves the sprite off screen, hiding it
    ylaser = yMin;
  } else {
    ylaser--;ylaser--;
  }

} else { // ylaser <= ymin
  xlaser = 240; // this moves the sprite off screen, hiding it
}

sprites[1].attribute0 = COLOR_256 | SQUARE | ylaser;
sprites[1].attribute1 = SIZE_8 | xlaser;


See, the A press simply sets the yLaser to 121, then the second IF loop will move the laser to the top of the screen until it reaches the top or hits an enemy. (laserHitsEnemy() is a function that returns 0 or 1 after checking for collision and destroying bad guys.)

-Scott

#116994 - Sabr3wulf - Wed Jan 31, 2007 5:42 pm

That's perfect, thankyou very much.

#116999 - sgeos - Wed Jan 31, 2007 6:39 pm

My advice, your code should look something like this:
Code:
if (pressA())
   fireLaser();

You should make a laser or projectile module that handles such things. Enemies should be able to use it as well. Give each shot a team or owner. If the shot hits something other than its owner, it does damage.

-Brendan


Last edited by sgeos on Wed Jan 31, 2007 8:53 pm; edited 1 time in total

#117020 - Miked0801 - Wed Jan 31, 2007 8:25 pm

Lol - let him figure out why your version is better sgeos. Every programmer has to get burned by this a few times before they get why encapsulation and such is a 'good thing' :)

#117025 - sgeos - Wed Jan 31, 2007 8:53 pm

Miked0801 wrote:
Lol - let him figure out why your version is better sgeos.

In theory he might not know about refactoring and encapsulation yet.

Miked0801 wrote:
Every programmer has to get burned by this a few times before they get why encapsulation and such is a 'good thing' :)

Fair enough. =)

-Brendan

#117052 - Sabr3wulf - Wed Jan 31, 2007 11:26 pm

Oh dear, I'm scared now.

Thanks for the help guys!

#117074 - sgeos - Thu Feb 01, 2007 1:25 am

No problem. Good luck!

-Brendan