• Advertisement
Sign in to follow this  

Java shooting game problem please help

This topic is 4219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Making a space shooting game in java almost complete. Infact if I can get my stupid ship, and AI ships to shoot it will be complete. I'm having trouble on how to move the bullets on the screen like the rest of the objects on the screen which is already moving across the screen like I like them to do. bull_x, and bull_y are my ships bullet x&y positions and there are 10 of them. each which is drawn off the screen as soon as the game is loaded by for(int i=0; i<Bull_Num; i++) {bull_x = -10; bull_y = -10;} //this work just great. However, when I am trying to fire the bullets they do go as I plan. if(e.getSource() == '32') { for(int i=0; i<Bull_Num; i++) { bull_x = ship_x; bull_y - ship_y; bull_x += speed; } } I know that all means let all bullets 10 equal the ships x&y for a second, and then let the bullets on the x coordinate increase by speed which is 5 However, it only moves every 5 increments evertime you press spacebar, and I need it to go either through the screen, or until it hits a spaceship, and evertime I throw I while loop wile(bull_x <= 500) do this, or something in that range my game just freezes, and I have to force quit it. Please help.

Share this post


Link to post
Share on other sites
Advertisement
I think in your bullets should have a boolean named active. And when you fire a bullet you set it to true. When it leaves the screen or hits a ship it becomes false. And in your drawing routine you only draw it if it's inactive and in your firing code.


if(e.getSource() == '32')
{
for(int i=0; i<Bull_Num; i++)
{
if (!bull_active)
{
bull_x = ship_x;
bull_y - ship_y;
bull_x plus= speed; //the plus symbol wont work for me
bull_active = true;
}
}
}




And to check to see if it goes off screen screen, don't use a while use and if



for(int i=0; i<Bull_Num; i++)
{
if (bull_y > 500)
{
bull_active = false;
}
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement