Alright, I'm using Borland C++ Builder, and mostly its working aside from this small problem...
I have a Gameloop, where everything is handled(drawing, mouvment, etc) and FormKeyUp/Down functions to grab the key presses.
Whenever I move, it raised exceptions. This coded worked fine before I added the mouvment. To be able to move right or left, i need to wait until there has been a collision between a bullet and an enemy.(at the moment it just auto-shoots...) And mouvming up or down just doesn't work unless I hold the key down, and as soon as I let go, it raises the exception.
Here is the game loop:
Code:
void __fastcall TForm1::GameLoop(TObject*, bool &done)
{
// fires a regular shot
if(laser->y1 <= 0 || laser->Obsolete == true)
{
laser->y1=User->y1;
laser->Obsolete = false;
laser->x1 = User->x1 + User->Ship_Sprite->Width / 2;
// PlaySound(MAKEINTRESOURCE(ID_LASER), HInstance, SND_RESOURCE | SND_ASYNC );
}
laser->regShot();
//check for collision
laser->Collision(Baddy,laser);
// fires a regular missile
if(missile->y1 <= 0 || missile->Obsolete == true)
{
missile->y1=User->y1;
missile->Obsolete = false;
missile->x1 = User->x1 + User->Ship_Sprite->Width / 2;
}
missile->regShot();
missile->Collision(Baddy, missile);
//move baddies
for(int k=1;k<5;k++)
{
for(int z = 1;z<9;z++)
{
if(Baddy[k][z]->Dead == false)
{
Baddy[k][z]->MoveLeft();
Baddy[k][z]->Baddy_Collision.top = Baddy[k][z]->x1;
Baddy[k][z]->Baddy_Collision.left = Baddy[k][z]->y1 + Baddy[k][z]->Enemy_Sprite->Height;
Baddy[k][z]->Baddy_Collision.Bottom = Baddy[k][z]->Baddy_Collision.left + Baddy[k][z]->Enemy_Sprite->Width;
Baddy[k][z]->Baddy_Collision.right = Baddy[k][z]->x1 + Baddy[k][z]->Enemy_Sprite->Width ;
}
}
}
/*for(int i=1;i<5;i++)
{
for(int k=1;k<9;k++)
{
// BLaser->
}
} */
//Key presses are acted upon here
if (keys.right == true)
{
User->MoveRight();
}
if (keys.left == true)
{
User->MoveLeft();
}
if (keys.up == true)
{
User->MoveUp();
}
if (keys.down == true)
{
User->MoveDown();
}
Draw(BackBuffer, User, Baddy, ClientRect, BackGround, laser, missile, Health);
done = false;
}
and the key up and down
Code:
oid __fastcall TForm1::FormKeyUp(TObject *Sender, WORD &Key, TShiftState Shift)
{
if (Key == VK_RIGHT) //move piece right
{
keys.right = false;
}
if (Key == VK_LEFT)
{
keys.left = false;
}
if (Key==VK_UP)
{
keys.up = false;
}
if (Key == VK_DOWN)
{
keys.down = false;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormKeyDown(TObject *Sender, WORD &Key, TShiftState Shift)
{
if (Key == VK_ESCAPE) Application->Terminate();
if (Key == VK_RIGHT) //move piece right
{
keys.right = true;
}
if (Key == VK_LEFT)
{
keys.left = true;
}
if (Key==VK_UP)
{
keys.up = true;
}
if (Key == VK_DOWN)
{
keys.down = true;
}
}
Any help would be great!
Cheers,
DW