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.

Graphics > gradius clone problems

#18914 - darknet - Thu Apr 08, 2004 5:08 am

Hey everyone, I am having a minor problem with my the beginnings of my gradius clone, I believe with the organization of my code.

To start things off, I wanted to just have my ship move around the screen and shoot its laser. The ship and the laser are separate sprites, and everything seems to be okay except that when I shoot the laser and move my ship at the same time, there is a notable slowdown with the ships movement.

Since there are no enemies yet, when I shoot a laser I simply want it to go from the nose of the ship to the end of the screen, which it does, and the code below is the only way I could get that to work....

Code:


//this is in the handleCollisions function....

  if(pressed(B_BUTTON) ) {

      //init laser to go right off of the front of the user ship
      ship_laser.xRight = (user.xRight+13);
      ship_laser.y = (user.yTop+1);
     
      //for now just go across the screen, later do collision
      //detection HERE for enemy ship intersections
      while( (ship_laser.xRight) < 240) {

         ship_laser.xRight = ship_laser.xRight + 1;
         moveSprite(&sprites[4], ship_laser.xRight, ship_laser.y);
         updateSpriteMemory();
         
      }
      ship_laser.xRight = (user.xRight+13);
   }


....the main loop then looks like so:

 while(gameRunning) {

      //the function above
      handleMovementsAndCollisions();
     
      //for now just move the user ship
      moveSprite(&sprites[0], user.xRight, user.yTop);
     
      updateSpriteMemory();
      waitForVSync();   
   }


I hope that code snippet explains what I am doing properly. Also included in that handleCollisions function is the up,down,left,right movements of the ship, which was omitted from my explanation above.

Thanks very much for any response, I truly appreciate it.
-Mike

#18919 - Cearn - Thu Apr 08, 2004 9:24 am

darknet wrote:

Code:
  if(pressed(B_BUTTON) ) {

      //init laser to go right off of the front of the user ship
      ship_laser.xRight = (user.xRight+13);
      ship_laser.y = (user.yTop+1);
     
      //for now just go across the screen, later do collision
      //detection HERE for enemy ship intersections
      while( (ship_laser.xRight) < 240) {

         ship_laser.xRight = ship_laser.xRight + 1;
         moveSprite(&sprites[4], ship_laser.xRight, ship_laser.y);
         updateSpriteMemory();
         
      }
      ship_laser.xRight = (user.xRight+13);
   }


The while loop here means that you'll be stuck in this function until the laser is off the screen (and it won't accept any input until then). Perhaps an if-statement is better. On the other hand, the whole loop should take very long since it has no wait for VBlank in it, so it shouldn't be noticeable. Hmmm.... On the third hand, that
Code:
ship_laser.xRight = (user.xRight+13);

probably doesn't help either.

(On a side note, C has special operators for x= x+y and such. Try x += y or, in the case of y==1, use x++. Look up these and related operators in your C-book or the help files. )

#18921 - darknet - Thu Apr 08, 2004 10:05 am

After posting I did realize the being stuck in the loop portion. However, if changed to an if (and if all of the init stuff is taken out as suggested), what happens is the following:

-Every time I press the b button it is incremented by my dx, which here is only 2.

Rather, at one button press I would like the laser to travel to the end of the screen all the while allowing the ship to move upon the user's request in a smooth manner.

How does one go about synchronizing sprite movement in that way? I.E. continuous movement of some entities, and others only by specific button requests? Or in my specific terms:

How do I shoot a laser from my ship and allow it to move across the screen (or eventually to an enemy) all the while allow my ship to move as I please simultaneously?

heh, and yeah, i was being very sloppy with my increments, drecrements, etc..since last post those have been fixed =)

Thanks,
-Mike

#18924 - Cearn - Thu Apr 08, 2004 10:31 am

What you probably want is some kind of active flag for your game's entities. (not objects, which are in OAM, but the actual things that are in your game-world). That way you have the full list of entities, but only the ones that are actually active will be considered in movement and collision detection and such. You could, for example have one laser-beam entity, that's not active by default, and so wouldn't do anything. When you shoot, you toggle the active flag and now the movement/collision functions will act on it. When you hit something (or it leaves the screen), you deactivate it again.
Of course, you should only activate something if it's not already active. An extra check for that will make sure that the laser's position isn't reset when you press B again when it's still visible.