Hi. I have a problem that has been bugging me. Help would be much appreciated. Thanks in advance!
I'm trying to make a 2d game where player 1 and player try to kill each other. They can each move left, right, up, down and they can each shoot bullets left, right, up, down. Unfortunately, when both of them are shooting, player 2 cannot move right anymore. I don't know why, this is quite a strange glitch.
Movement for class player1:
//---MOVEMENT---
int player1::movement()
{
//---UP---
if(dbKeyState(17) == 1)
{
dbSetSpriteFrame(plr1,3);
dbMoveSprite(plr1,4);
if(dbSpriteCollision(plr1,10))
{
dbMoveSprite(plr1,-4);
}
}
//---DOWN---
if(dbKeyState(31) == 1)
{
dbSetSpriteFrame(plr1,4);
dbMoveSprite(plr1,-4);
if(dbSpriteCollision(plr1,11))
{
dbMoveSprite(plr1,4);
}
}
//---RIGHT---
if(dbKeyState(32) == 1)
{
dbSetSpriteFrame(plr1,1);
dbRotateSprite(plr1,90);
dbMoveSprite(plr1,4);
dbRotateSprite(plr1,0);
if(dbSpriteCollision(plr1,13))
{
dbRotateSprite(plr1,270);
dbMoveSprite(plr1,4);
dbRotateSprite(plr1,0);
}
}
//---LEFT---
if(dbKeyState(30) == 1)
{
dbSetSpriteFrame(plr1,2);
dbRotateSprite(plr1,270);
dbMoveSprite(plr1,4);
dbRotateSprite(plr1,0);
if(dbSpriteCollision(plr1,12))
{
dbRotateSprite(plr1,90);
dbMoveSprite(plr1,4);
dbRotateSprite(plr1,0);
}
}
return(0);
}
Movement for class player2:
//---MOVEMENT---
int player2::movement()
{
//---UP---
if(dbUpKey() == 1)
{
dbSetSpriteFrame(plr2,3);
dbMoveSprite(plr2,4);
if(dbSpriteCollision(plr2,10))
{
dbMoveSprite(plr2,-4);
}
}
//---DOWN---
if(dbDownKey() == 1)
{
dbSetSpriteFrame(plr2,4);
dbMoveSprite(plr2,-4);
if(dbSpriteCollision(plr2,11))
{
dbMoveSprite(plr2,4);
}
}
//---RIGHT---
if(dbRightKey() == 1)
{
dbSetSpriteFrame(plr2,2);
dbRotateSprite(plr2,90);
dbMoveSprite(plr2,4);
dbRotateSprite(plr2,0);
if(dbSpriteCollision(plr2,13))
{
dbRotateSprite(plr2,270);
dbMoveSprite(plr2,4);
dbRotateSprite(plr2,0);
}
}
//---LEFT---
if(dbLeftKey() == 1)
{
dbSetSpriteFrame(plr2,1);
dbRotateSprite(plr2,270);
dbMoveSprite(plr2,4);
dbRotateSprite(plr2,0);
if(dbSpriteCollision(plr2,12))
{
dbRotateSprite(plr2,90);
dbMoveSprite(plr2,4);
dbRotateSprite(plr2,0);
}
}
return(0);
}
I actually think that the problem may lie in the shooting function. See, I tried making sprites shoot multiple bullets. I've thought, searched the internet, etc. I've learned that I have to make an array of bullets. But no one posted examples of arrays of bullets. So I finally found a way to shoot multiple bullets. I know it isn't the most efficient way, but it works.
Shooting for player1:
//---SHOOTING---
int player1::shooting()
{
//---GENERAL---
if(dbSpaceKey() == 1 && dbSpriteExist(plr1) == 1 && fireDelay != 0)
{
--fireDelay;
}
for(int i = 100; i < 150; i++)
{
for(int b = 10; b < 20; b++)
{
if(dbSpriteCollision(i,b))
{
dbDeleteSprite(i);
}
}
}
//--------GUN 1--------
//---RIGHTSHOT---
if(dbSpaceKey() == 1 && dbSpriteExist(plr1) == 1 && dbSpriteExist(100) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr1) == 1 && bullet == 0)
{
dbStopSound(1);
dbSprite(100,dbSpriteX(plr1) + 50, dbSpriteY(plr1),bulletID);
bullet = 1;
fireDelay = 15;
dbPlaySound(1);
}
if(dbSpaceKey() == 1 && dbSpriteExist(plr1) == 1 && dbSpriteExist(101) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr1) == 1 && bullet == 1)
{
dbStopSound(1);
dbSprite(101,dbSpriteX(plr1) + 50, dbSpriteY(plr1),bulletID);
bullet = 2;
fireDelay = 15;
dbPlaySound(1);
}
if(dbSpaceKey() == 1 && dbSpriteExist(plr1) == 1 && dbSpriteExist(102) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr1) == 1 && bullet == 2)
{
dbStopSound(1);
dbSprite(102,dbSpriteX(plr1) + 50, dbSpriteY(plr1),bulletID);
bullet = 0;
fireDelay = 15;
dbPlaySound(1);
}
for(int i = 100; i < 103; i++)
{
dbRotateSprite(i,90);
dbMoveSprite(i,8);
dbRotateSprite(i,0);
}
//---LEFTSHOT---
if(dbSpaceKey() == 1 && dbSpriteExist(plr1) == 1 && dbSpriteExist(103) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr1) == 2 && bullet == 0)
{
dbStopSound(1);
dbSprite(103,dbSpriteX(plr1), dbSpriteY(plr1),bulletID);
bullet = 1;
fireDelay = 15;
dbPlaySound(1);
}
if(dbSpaceKey() == 1 && dbSpriteExist(plr1) == 1 && dbSpriteExist(104) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr1) == 2 && bullet == 1)
{
dbStopSound(1);
dbSprite(104,dbSpriteX(plr1), dbSpriteY(plr1),bulletID);
bullet = 2;
fireDelay = 15;
dbPlaySound(1);
}
if(dbSpaceKey() == 1 && dbSpriteExist(plr1) == 1 && dbSpriteExist(105) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr1) == 2 && bullet == 2)
{
dbStopSound(1);
dbSprite(105,dbSpriteX(plr1), dbSpriteY(plr1),bulletID);
bullet = 0;
fireDelay = 15;
dbPlaySound(1);
}
for(int i = 103; i < 106; i++)
{
dbRotateSprite(i,270);
dbMoveSprite(i,8);
dbRotateSprite(i,0);
}
//---UPSHOT---
if(dbSpaceKey() == 1 && dbSpriteExist(plr1) == 1 && dbSpriteExist(106) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr1) == 3 && bullet == 0)
{
dbStopSound(1);
dbSprite(106,dbSpriteX(plr1), dbSpriteY(plr1) - 10,bulletID2);
bullet = 1;
fireDelay = 15;
dbPlaySound(1);
}
if(dbSpaceKey() == 1 && dbSpriteExist(plr1) == 1 && dbSpriteExist(107) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr1) == 3 && bullet == 1)
{
dbStopSound(1);
dbSprite(107,dbSpriteX(plr1), dbSpriteY(plr1) - 10,bulletID2);
bullet = 2;
fireDelay = 15;
dbPlaySound(1);
}
if(dbSpaceKey() == 1 && dbSpriteExist(plr1) == 1 && dbSpriteExist(108) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr1) == 3 && bullet == 2)
{
dbStopSound(1);
dbSprite(108,dbSpriteX(plr1), dbSpriteY(plr1) - 10,bulletID2);
bullet = 0;
fireDelay = 15;
dbPlaySound(1);
}
for(int i = 106; i < 109; i++)
{
dbMoveSprite(i,8);
}
//---DOWNSHOT---
if(dbSpaceKey() == 1 && dbSpriteExist(plr1) == 1 && dbSpriteExist(106) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr1) == 4 && bullet == 0)
{
dbStopSound(1);
dbSprite(109,dbSpriteX(plr1), dbSpriteY(plr1) + 50,bulletID2);
bullet = 1;
fireDelay = 15;
dbPlaySound(1);
}
if(dbSpaceKey() == 1 && dbSpriteExist(plr1) == 1 && dbSpriteExist(107) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr1) == 4 && bullet == 1)
{
dbStopSound(1);
dbSprite(110,dbSpriteX(plr1), dbSpriteY(plr1) + 50,bulletID2);
bullet = 2;
fireDelay = 15;
dbPlaySound(1);
}
if(dbSpaceKey() == 1 && dbSpriteExist(plr1) == 1 && dbSpriteExist(108) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr1) == 4 && bullet == 2)
{
dbStopSound(1);
dbSprite(111,dbSpriteX(plr1), dbSpriteY(plr1) + 50,bulletID2);
bullet = 0;
fireDelay = 15;
dbPlaySound(1);
}
for(int i = 109; i < 112; i++)
{
dbMoveSprite(i,-8);
}
return(0);
}
Shooting for player2:
//---SHOOTING---
int player2::shooting()
{
//---GENERAL---
if(dbKeyState(82) == 1 && dbSpriteExist(plr2) == 1 && fireDelay != 0)
{
--fireDelay;
}
for(int i = 150; i < 200; i++)
{
for(int b = 10; b < 20; b++)
{
if(dbSpriteCollision(i,b))
{
dbDeleteSprite(i);
}
}
}
//--------GUN 1--------
//---RIGHTSHOT---
if(dbKeyState(82) == 1 && dbSpriteExist(plr2) == 1 && dbSpriteExist(150) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr2) == 2 && bullet == 0)
{
dbStopSound(1);
dbSprite(150,dbSpriteX(plr2) + 50, dbSpriteY(plr2),bulletID);
bullet = 1;
fireDelay = 15;
dbPlaySound(1);
}
if(dbKeyState(82) == 1 && dbSpriteExist(plr2) == 1 && dbSpriteExist(151) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr2) == 2 && bullet == 1)
{
dbStopSound(1);
dbSprite(151,dbSpriteX(plr2) + 50, dbSpriteY(plr2),bulletID);
bullet = 2;
fireDelay = 15;
dbPlaySound(1);
}
if(dbKeyState(82) == 1 && dbSpriteExist(plr2) == 1 && dbSpriteExist(152) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr2) == 2 && bullet == 2)
{
dbStopSound(1);
dbSprite(152,dbSpriteX(plr2) + 50, dbSpriteY(plr2),bulletID);
bullet = 0;
fireDelay = 15;
dbPlaySound(1);
}
for(int i = 150; i < 153; i++)
{
dbRotateSprite(i,90);
dbMoveSprite(i,8);
dbRotateSprite(i,0);
}
//---LEFTSHOT---
if(dbKeyState(82) == 1 && dbSpriteExist(plr2) == 1 && dbSpriteExist(153) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr2) == 1 && bullet == 0)
{
dbStopSound(1);
dbSprite(153,dbSpriteX(plr2), dbSpriteY(plr2),bulletID);
bullet = 1;
fireDelay = 15;
dbPlaySound(1);
}
if(dbKeyState(82) == 1 && dbSpriteExist(plr2) == 1 && dbSpriteExist(154) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr2) == 1 && bullet == 1)
{
dbStopSound(1);
dbSprite(154,dbSpriteX(plr2), dbSpriteY(plr2),bulletID);
bullet = 2;
fireDelay = 15;
dbPlaySound(1);
}
if(dbKeyState(82) == 1 && dbSpriteExist(plr2) == 1 && dbSpriteExist(155) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr2) == 1 && bullet == 2)
{
dbStopSound(1);
dbSprite(155,dbSpriteX(plr2), dbSpriteY(plr2),bulletID);
bullet = 0;
fireDelay = 15;
dbPlaySound(1);
}
for(int i = 153; i < 156; i++)
{
dbRotateSprite(i,270);
dbMoveSprite(i,8);
dbRotateSprite(i,0);
}
//---UPSHOT---
if(dbKeyState(82) == 1 && dbSpriteExist(plr2) == 1 && dbSpriteExist(156) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr2) == 3 && bullet == 0)
{
dbStopSound(1);
dbSprite(156,dbSpriteX(plr2), dbSpriteY(plr2) - 10,bulletID2);
bullet = 1;
fireDelay = 15;
dbPlaySound(1);
}
if(dbKeyState(82) == 1 && dbSpriteExist(plr2) == 1 && dbSpriteExist(157) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr2) == 3 && bullet == 1)
{
dbStopSound(1);
dbSprite(157,dbSpriteX(plr2), dbSpriteY(plr2) - 10,bulletID2);
bullet = 2;
fireDelay = 15;
dbPlaySound(1);
}
if(dbKeyState(82) == 1 && dbSpriteExist(plr2) == 1 && dbSpriteExist(158) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr2) == 3 && bullet == 2)
{
dbStopSound(1);
dbSprite(158,dbSpriteX(plr2), dbSpriteY(plr2) - 10,bulletID2);
bullet = 0;
fireDelay = 15;
dbPlaySound(1);
}
for(int i = 156; i < 159; i++)
{
dbMoveSprite(i,8);
}
//---DOWNSHOT---
if(dbKeyState(82) == 1 && dbSpriteExist(plr2) == 1 && dbSpriteExist(159) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr2) == 4 && bullet == 0)
{
dbStopSound(1);
dbSprite(159,dbSpriteX(plr2), dbSpriteY(plr2) + 50,bulletID2);
bullet = 1;
fireDelay = 15;
dbPlaySound(1);
}
if(dbKeyState(82) == 1 && dbSpriteExist(plr2) == 1 && dbSpriteExist(160) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr2) == 4 && bullet == 1)
{
dbStopSound(1);
dbSprite(160,dbSpriteX(plr2), dbSpriteY(plr2) + 50,bulletID2);
bullet = 2;
fireDelay = 15;
dbPlaySound(1);
}
if(dbKeyState(82) == 1 && dbSpriteExist(plr2) == 1 && dbSpriteExist(161) == 0 && fireDelay == 0 && gunType == 0 && dbSpriteFrame(plr2) == 4 && bullet == 2)
{
dbStopSound(1);
dbSprite(161,dbSpriteX(plr2), dbSpriteY(plr2) + 50,bulletID2);
bullet = 0;
fireDelay = 15;
dbPlaySound(1);
}
for(int i = 159; i < 162; i++)
{
dbMoveSprite(i,-8);
}
return(0);
}
I'm sorry that my post is so long. I will post a picture of my game so far. Thanks!
I can has noobish skills.