We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › "shots" disappear in shooter game
Page Index Toggle Pages: 1
"shots" disappear in shooter game (Read 841 times)
"shots" disappear in shooter game
Jan 11th, 2010, 6:18am
 
Hi, this is a basic question. But I'm just starting and I'm stuck. I wanted to create a basic space invader type game were a shot is fired when the user hits the mouse.

I've been able to get a square "shot" to move when the user hits the mouse. I created an object called "Shot" and have an array of them on my main program. When I hit the mouse one shot fires.

So far so good. My problem is that when ever I hit the mouse again, It appears to erase the shot that is currently on the screen, and replace it with a new shot. I want both (or many) to be on the screen at once:

The code is below:

// Global variables
// Intializing Shot object
// Why does shoting a new shot stop the old instance from displaying?
//-------------------------------------------------
Shot[] shot = new Shot[12];

boolean mouseCheck = false;
int totalShots = 0;

void setup()
{
 size(400,400);
 for(int i = 0; i < shot.length; i++)
 {
   shot[i] = new Shot(random(0,375));
   println("shot instance name is: shot" + i);
 }
}

void draw()
{
 background(150);

 if(mouseCheck)
 {
   shot[totalShots].display();
   shot[totalShots].move();
   println("shot instance display name is: shot" + totalShots);
 }

}

void mousePressed()
{
 totalShots++;
 mouseCheck = true;

 //println("Mouse has been pressed 2");
}

// Shot class

class Shot
{
 float shotX;
 int shotY;
 int shotWidth;
 int shotHeight;
 int speed;


 Shot(float tempShotX)
 {
 shotX = tempShotX;  
 shotY = 375;
 shotWidth = 25;
 shotHeight = 25;
 speed = 1;
 }

 void move()
 {
 shotY--;
 }

 void display()
 {
   //Display rectangle
   rect(shotX, shotY, shotWidth, shotHeight);
   //println("Mouse has been pressed 1");
 }

}

Re: "shots" disappear in shooter game
Reply #1 - Jan 11th, 2010, 6:37am
 
you only draw the latest shot not all of them. thats why you only see the last fired shot.
you have to loop through them to draw them all. like this :

Code:
void draw()
{
background(150);

if(mouseCheck)
{
  for(int i = 0;i<totalShots;i++){
  shot[i].display();
  shot[i].move();
  println("shot instance display name is: shot" + totalShots);
} }

}


you still get problems when couter reaches 12 as this is the size of your array.  Maybe think about an arraylist you can expand. you can add and delete objects... this is a good tutorial.
http://processing.org/learning/topics/arraylistclass.html

Re: "shots" disappear in shooter game
Reply #2 - Jan 11th, 2010, 7:53am
 
I played with your code a bit, mostly just adding a loop to go through your shots objects ... and added some properties to give you a hint of what you could do ...

Shot[] shot = new Shot[12];

boolean mouseCheck = false;
int totalShots = 0;

void setup()
{
 size(400,400);
 for(int i = 0; i < shot.length; i++)
 {
   shot[i] = new Shot(random(0,375));
   println("shot instance name is: shot" + i);
 }
}

void draw()
{
 background(150);

 // if(mouseCheck) // redundant ... you should loop through the shots objects regardless ...

 for (int l=0; l<shot.length; l++){
   {
     if (shot[l].fired){
     shot[l].display();
     shot[l].move();
     }
 
    // println("shot instance display name is: shot" + totalShots);
   }
 }

}

void mousePressed()
{
 totalShots++;
 totalShots %= shot.length;
 shot[totalShots].shotY = mouseY;
 shot[totalShots].shotX = mouseX;
 shot[totalShots].fired = true;
 //mouseCheck = true;
}

//println("Mouse has been pressed 2");


// Shot class

class Shot
{
 float shotX;
 int shotY;
 int shotWidth;
 int shotHeight;
 int speed;
 boolean fired; // also indicates if the bullet should be displayed or not ...

 Shot(float tempShotX)
 {
   shotX = tempShotX;  
   shotY = 375;
   shotWidth = 5;
   shotHeight = 25;
   speed = 1;
   fired = false;
 }

 void move()
 {
     shotY--;
   if (shotY < 0) {
     fired = false;
     shotY = 375;
   }
 }

 void display()
 {
   //Display rectangle
   rect(shotX, shotY, shotWidth, shotHeight);
   //println("Mouse has been pressed 1");
 }
}
Re: "shots" disappear in shooter game
Reply #3 - Jan 12th, 2010, 6:24am
 
Smiley Thank you for the answers. knutEinar what you came up with is what I had in mind.

Without you I would of been staring at code not getting anywhere - or I would of just given up.

Thanks again!
Page Index Toggle Pages: 1