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 & HelpPrograms › Im not able to get an animated SpaceInvader shot
Page Index Toggle Pages: 1
Im not able to get an animated SpaceInvader shot (Read 1112 times)
Im not able to get an animated SpaceInvader shot
Mar 29th, 2010, 10:35pm
 
hi guys, I'm working on a final project with processing. And I decided to redo a space Invaders game.
But with my code I'm not able to create the shot animation when I press the spacebar it creates a line of shots and not the animation...
can someone help me ?

Ship mothership;
Enemy creature1;
Fire shot;

void setup() {
 size(350,800);
 smooth();
 mothership=new Ship(width/2,height-10,40,10);
 creature1=new Enemy(400,200,75,75);
 shot=new Fire(width/2,10,10);
 noStroke();
}

void draw() {
 background(0);
 shot.posXShot=mothership.posXMothership;
 shot.posYShot=mothership.posYMothership;
 shot.drawShot();
 mothership.displayMothership();
 creature1.displayCreature1();
}

class Enemy {
 float posXCreature1;
 float posYCreature1;
 float wCreature1;
 float hCreature1;

 Enemy(float posXCreature1, float posYCreature1, float wCreature1, float hCreature1) {
   this.posXCreature1=posXCreature1;
   this.posYCreature1=posYCreature1;
   this.wCreature1=wCreature1;
   this.hCreature1=hCreature1;
 }

 void displayCreature1() {
   fill(255);
   rectMode(CENTER);
   rect(posXCreature1,posYCreature1,wCreature1,hCreature1);
 }
}

class Fire {
 boolean finishLine;
 float posXShot;
 float posYShot;
 float sizeShot;

 Fire (float posXShot, float posYShot, float sizeShot){
   this.posXShot=posXShot;
   this.posYShot=posYShot;
   this.sizeShot=sizeShot;
 }
 void drawShot(){
   if (keyPressed && key == 32){
     while (posYShot>-10 && !finishLine){
       posYShot-=10;
       fill(255);
       ellipse(posXShot,posYShot,sizeShot,sizeShot);
     }
   }
 }
}

class Ship {
 float posXMothership;
 float posYMothership;
 float rectWMothership;
 float rectHMothership;

 Ship(float posXMothership,float posYMothership, float rectWMothership, float rectHMothership) {
   this.posXMothership=posXMothership;
   this.posYMothership=posYMothership;
   this.rectWMothership=rectWMothership;
   this.rectHMothership=rectHMothership;
 }

 void displayMothership() {
   fill(255);
   rectMode(CENTER);
   rect(posXMothership,posYMothership,rectWMothership,rectHMothership);
   if(keyPressed){
     if(keyCode==LEFT && posXMothership>rectWMothership-rectWMothership/2){
         posXMothership--;
     }
     if(keyCode==RIGHT && posXMothership<width-rectWMothership/2){
         posXMothership++;
     }
   }
 }
}


Re: Im not able to get an animated SpaceInvader shot
Reply #1 - Mar 29th, 2010, 10:59pm
 
Quote:
Ship mothership;
Enemy creature1;
Fire shot;

void setup() {
 size(350,800);
 smooth();
 mothership=new Ship(width/2,height-10,40,10);
 creature1=new Enemy(400,200,75,75);
 shot=new Fire(width/2,10,10);
 noStroke();
}

void draw() {
 background(0);
 //shot.posXShot=mothership.posXMothership;
 //shot.posYShot=mothership.posYMothership;
 shot.drawShot();
 mothership.displayMothership();
 creature1.displayCreature1();
}

class Enemy {
 float posXCreature1;
 float posYCreature1;
 float wCreature1;
 float hCreature1;

 Enemy(float posXCreature1, float posYCreature1, float wCreature1, float hCreature1) {
   this.posXCreature1=posXCreature1;
   this.posYCreature1=posYCreature1;
   this.wCreature1=wCreature1;
   this.hCreature1=hCreature1;
 }

 void displayCreature1() {
   fill(255);
   rectMode(CENTER);
   rect(posXCreature1,posYCreature1,wCreature1,hCreature1);
 }
}

class Fire {
 boolean finishLine;
 float posXShot;
 float posYShot;
 float sizeShot;

 Fire (float posXShot, float posYShot, float sizeShot){
   this.posXShot=posXShot;
   this.posYShot=posYShot;
   this.sizeShot=sizeShot;
 }
 void drawShot(){
   if (keyPressed && key == 32){
    posXShot =  mothership.posXMothership;
    posYShot =  mothership.posYMothership;
   }
   if( posYShot > -30 ){
       posYShot-=10;
       fill(255);
       ellipse(posXShot,posYShot,sizeShot,sizeShot);
   }
 }
}

class Ship {
 float posXMothership;
 float posYMothership;
 float rectWMothership;
 float rectHMothership;

 Ship(float posXMothership,float posYMothership, float rectWMothership, float rectHMothership) {
   this.posXMothership=posXMothership;
   this.posYMothership=posYMothership;
   this.rectWMothership=rectWMothership;
   this.rectHMothership=rectHMothership;
 }

 void displayMothership() {
   fill(255);
   rectMode(CENTER);
   rect(posXMothership,posYMothership,rectWMothership,rectHMothership);
   if(keyPressed){
     if(keyCode==LEFT && posXMothership>rectWMothership-rectWMothership/2){
         posXMothership--;
     }
     if(keyCode==RIGHT && posXMothership<width-rectWMothership/2){
         posXMothership++;
     }
   }
 }
}



While this fixes the immediate issue of your shot not animating, it doesn't fix the other issues I can see with this code - and they're not simple fixes, they are massive structural changes! If you want more help, please ask!
Re: Im not able to get an animated SpaceInvader shot
Reply #2 - Mar 30th, 2010, 9:44am
 
Thx but I'm having another problem now.. so if I press the left or right arrow to move the ship, it moves everything is ok but as soon as I touch another key, for exemple the spacebar to shot, it stops the moving platform... I have to release it and reclick... is there a fix to that?

And also is there a way making my shot appear only when the key is released so that it doesn't pop out is I have the key down.

Ship mothership;
Enemy creature1;
Fire shot;


void setup() {
 size(350,800);
 smooth();
 mothership=new Ship(width/2,height-10,40,10);
 creature1=new Enemy(width/2,200,20,20);
 shot=new Fire(width/2,10,10);
 noStroke();
}

void draw() {
 background(0);
 shot.drawShot();
 mothership.displayMothership();
 creature1.displayCreature1();
}

class Enemy {
float posXCreature1;
float posYCreature1;
float wCreature1;
float hCreature1;

Enemy(float posXCreature1, float posYCreature1, float wCreature1, float hCreature1) {
  this.posXCreature1=posXCreature1;
  this.posYCreature1=posYCreature1;
  this.wCreature1=wCreature1;
  this.hCreature1=hCreature1;
}

void displayCreature1() {
  fill(255);
  rectMode(CENTER);
  rect(posXCreature1,posYCreature1,wCreature1,hCreature1);
}
}

int shottimer;
int counter;

class Fire {
 boolean finishLine;
 float posXShot;
 float posYShot;
 float sizeShot;

 Fire (float posXShot, float posYShot, float sizeShot){
   this.posXShot=posXShot;
   this.posYShot=posYShot;
   this.sizeShot=sizeShot;
 }
 void drawShot(){
   shottimer++;
   if (keyPressed && key == 32 && shottimer > 85){
     counter ++;
     println(counter);
     shottimer = 0;
     posXShot =  mothership.posXMothership;
     posYShot =  mothership.posYMothership;
   }
   if( posYShot > -30 && !finishLine){
     posYShot-=10;
     fill(255);
     ellipse(posXShot,posYShot,sizeShot,sizeShot);
   }
   println(shottimer);
 }
}

class Ship {
float posXMothership;
float posYMothership;
float rectWMothership;
float rectHMothership;

Ship(float posXMothership,float posYMothership, float rectWMothership, float rectHMothership) {
  this.posXMothership=posXMothership;
  this.posYMothership=posYMothership;
  this.rectWMothership=rectWMothership;
  this.rectHMothership=rectHMothership;
}

void displayMothership() {
  fill(255);
  rectMode(CENTER);
  rect(posXMothership,posYMothership,rectWMothership,rectHMothership);
  if(keyPressed){
    if(keyCode==LEFT && posXMothership>rectWMothership-rectWMothership/2){
        posXMothership--;
    }
    if(keyCode==RIGHT && posXMothership<width-rectWMothership/2){
        posXMothership++;
    }
  }
}
}


Re: Im not able to get an animated SpaceInvader shot
Reply #3 - Mar 30th, 2010, 10:35am
 
The registering multiple key presses issue is a FAQ: just type 'multiple key presses' into the search box somewhere up there ^

Or just look at this which may well go some way towards answering your other question...
Page Index Toggle Pages: 1