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 › Triggering multiple events
Page Index Toggle Pages: 1
Triggering multiple events (Read 914 times)
Triggering multiple events
Jan 7th, 2008, 2:47am
 
Hello, this is probably a question more about programming than processing specifically, so apologies if it's in the wrong place or anything.

I'm pretty new to processing and programming in general, I've been messing around a bit trying to write a very basic shoot 'em up, which processing is maybe not ideal for, but it's a fun step away from flash.
My problems come with trying to get the ship to shoot, specifically getting more than one bullet on the screen. I haven't figured out how to press the shoot button and press it again to get a different bullet, if that makes sense.
I'm wondering if i need an string that would hold the information for each individual bullet?

So far trying to sort this out has mainly been trial and error, but i'd really appreciate it if someone would be able to help me in the right direction with this.
Thanks in advance,
niall.

Here is the code if anyone is interested.


final static int NORTH = 1;
final static int EAST = 2;
final static int SOUTH = 4;
final static int WEST = 8;
int result;
float x,y;
boolean shoot;
float shootx, shooty;




void setup(){
 size (400, 400);
 frameRate(30);  

}

void draw(){
 background(55551199);
 fill (500,y-50,100);
 triangle(10+x,30+y,30+x,20+y,10+x,10+y);
 fill (500,50,300);
 switch(result) {
 case NORTH:
   y-=5;
   break;
 case EAST:
   x+=5;
   break;
 case SOUTH:
   y+=5;
   break;
 case WEST:
   x-=5;
   break;
 case NORTH|EAST:
   y-=5;
   x+=5;
   break;
 case NORTH|WEST:
   y-=5;
   x-=5;
   break;
 case SOUTH|EAST:
   y+=5;
   x+=5;
   break;
 case SOUTH|WEST:
   y+=5;
   x-=5;
   break;
 }
if (shoot) {
 shootx += 5;
}
line (shootx, shooty, 10+shootx, shooty);
}


void keyPressed(){
 switch(key) {
   case('w'):
   case('W'):
   result |=NORTH;
   break;
   case('d'):
   case('D'):
   result |=EAST;
   break;
   case('s'):
   case('S'):
   result |=SOUTH;
   break;
   case('a'):
   case('A'):
   result |=WEST;
   break;
 }
  if (key == 'l' || key == 'L') {
     shoot = true;
     shootx = x+20;
     shooty = y+20;
  }
}

void keyReleased(){  
 switch(key) {
   case('w'):
   case('W'):
   result ^=NORTH;
   break;
   case('d'):
   case('D'):
   result ^=EAST;
   break;
   case('s'):
   case('S'):
   result ^=SOUTH;
   break;
   case('a'):
   case('A'):
   result ^=WEST;
   break;
 }
}

Re: Triggering multiple events
Reply #1 - Jan 7th, 2008, 10:31am
 
I think you'll benefit from object oriented programming as described http://processing.org/learning/basics/objects.html

After you've gotten a handle on objects try to turn them into many types of bullets
http://processing.org/learning/basics/inheritance.html

This will make your life easier and reduce a lot of code.

It looks like you're doing bitshifting, which is the right idea here.
Re: Triggering multiple events
Reply #2 - Jan 7th, 2008, 4:45pm
 
Quote:
I'm wondering if i need an string that would hold the information for each individual bullet?


each bullet is an object with a specific position on screen, so you could start with declaring what a bullet is :
Code:
class Bullet {
float posX;
float posY;
}


then, declare an (empty) array of bullets :
Code:
Bullet[] bullets = new Bullet[0]; 



then, instanciate a new bullet each time the shoot key is pressed, and add it to a bullet array :
Code:
if (key == 'l' || key == 'L') {
Bullet b = new Bullet();
b.posX = x+20;
b.posY = y+20;
bullets = (Bullet[])append(bullets, b); // add it to the array
}


finally, iterate through the bullets array in the draw() method to make them move and display them on screen :
Code:
for (int i = 0; i < bullets.length; i++) {
bullets[i].posX += 10;
line (bullets[i].posX, bullets[i].posY, bullets[i].posX + 10, bullets[i].posY);
}



In fact, I would better recommend using an Arraylist to store bullet objects, but the solution above is more simple. I don't like using arrays and the append() method, which gives you no easy way to delete bullets when they get out of screen.
Re: Triggering multiple events
Reply #3 - Jan 9th, 2008, 2:37am
 
thanks so much guys, I'm still about halfway to figuring out what's going on with arrays, but I'll get back when it's sorted.
Thanks again.
Page Index Toggle Pages: 1