0 Replies - 1616 Views - Last Post: 12 November 2012 - 11:56 PM Rate Topic: -----

#1 aviateplus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 24-August 12

(DarkGDK) Locked Sprite Movement

Posted 12 November 2012 - 11:56 PM

Hi,

I'm still new to programming and realize my code could be optimized but aside from that I have a couple of issues with
a project that I am working on.

My objective is to make the MISSILE sprite launch upon pressing the spacebar.

When I was working on the code, the ship was able to move freely along the X axis as intended (Only left/right inputs accepted) but at some point the ship became "locked" on the X axis. I've tried removing pieces of the code and none of the if statements seem to be causing the problem.

After I press the spacebar, the missile appears and the ship sprite is instantly unlocked and free to move.

My second problem is that I have to hold down the spacebar to keep the missile moving.

I've tried adding a while loop to keep the missile moving until, for example, "missileY = 0"
but the program hangs when I do that and I've determined it's because of the loop being within the LoopGDK().

This is probably a simple fix, but after many attempts I cannot find a way around the locked sprite or missile shooting problem.

Thanks!

View of how the program looks when starting and ship is "locked":
Posted Image

After firing missile:
Posted Image



#include "DarkGDK.h"

const int SHIP = 1;
const int MISSILE = 2;
const int EXPLOSION = 3;
const int ASTEROID = 4;
const int REFRESH_RATE=60;
const int SHIP_IMAGE_NUMBER=1;
const int SHIP_SPRITE_NUMBER=1;
const int MISSILE_IMAGE_NUMBER=2;
const int MISSILE_SPRITE_NUMBER=2;
const int AST_IMAGE_NUMBER=4;
const int AST_SPRITE_NUMBER=4;
const int EXP_IMAGE_NUMBER=3;
const int EXP_SPRITE_NUMBER=3;
const int SHIP_STARTING_X=90;
const int SHIP_STARTING_Y=360;
const int AST_STARTING_X=280;
const int AST_STARTING_Y=70;
const int TEXT_X = 319;
const int TEXT_Y=20;

void setUp();
void getSHIPcoordinates (int &x);
void shootMISSILE (int &missileY);

void DarkGDK()
{
	int shipX = SHIP_STARTING_X;
	int shipY = SHIP_STARTING_Y;
	int missileY = SHIP_STARTING_Y;
	
	setUp();
	while (LoopGDK() )
	{
		dbPasteImage(5,0,0);
		dbCenterText(TEXT_X,TEXT_Y, "Use the arrow keys to position the ship and spacebar to fire.");
		getSHIPcoordinates (shipX);
		dbSprite(SHIP_SPRITE_NUMBER, shipX,SHIP_STARTING_Y,SHIP_IMAGE_NUMBER);

		if (dbSpaceKey())
			{
			shootMISSILE(missileY);
			dbSprite(MISSILE_SPRITE_NUMBER, (shipX+30), (missileY-50), MISSILE_IMAGE_NUMBER);
			}
	

		if (dbSpriteCollision(MISSILE_SPRITE_NUMBER,AST_SPRITE_NUMBER))
			{
			dbHideSprite(AST_SPRITE_NUMBER);
			dbSprite(EXP_SPRITE_NUMBER,AST_STARTING_X,AST_STARTING_Y,EXP_IMAGE_NUMBER);
			}

		if ( (dbSpriteCollision(MISSILE_SPRITE_NUMBER,AST_SPRITE_NUMBER)==0) && (missileY < 50) && (dbSpriteExist(EXP_SPRITE_NUMBER)==0))
			{
			dbText(TEXT_X,TEXT_Y+300, "Try again!");
			}
		
		if ( (missileY < 50) && (dbSpriteExist(EXP_SPRITE_NUMBER)==1))
			{
			dbText(200,TEXT_Y+300, "Press ESC to exit or Enter to play again.");
			}

	dbSync();
	}

}



void setUp()
{
	dbSetImageColorKey(0,255,0);
	dbLoadImage("asteroid.bmp",ASTEROID);
	dbLoadImage("ship.bmp",SHIP);
	dbLoadImage("explosion.bmp",EXPLOSION);
	dbLoadImage("missile.bmp",MISSILE);
	dbLoadImage("Backdrop.bmp",5);

	//dbSprite(SHIP_SPRITE_NUMBER, SHIP_STARTING_X, SHIP_STARTING_Y, SHIP_IMAGE_NUMBER);
	dbSprite(AST_SPRITE_NUMBER, AST_STARTING_X, AST_STARTING_Y, AST_IMAGE_NUMBER);
	

	dbSyncOn();
	dbSyncRate(REFRESH_RATE);
}
void getSHIPcoordinates (int &x)
{
	if (dbLeftKey())
		x--;
	if (dbRightKey())
		x++;
}
void shootMISSILE (int &missileY)
{
	missileY--;

}



Is This A Good Question/Topic? 0
  • +

Page 1 of 1