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 › Help moving objects via key presses and more.
Page Index Toggle Pages: 1
Help moving objects via key presses and more. (Read 884 times)
Help moving objects via key presses and more.
Apr 21st, 2010, 8:56pm
 
So I'm working on this program and I have most of it done, but I can't figure out one of the most important things I want to have in it. In the program there are 10 ships in the air which look fine, but I want them to move when I press their corresponding number. So ship0 i press 0 and that ship should be the ship with active controls that i control with the arrow keys. That needs to work with all the ships 0-9. As of now they just sit in place.

Also I have the ships where I want them to start when the program launches but I was wondering if I could turn on gravity easily to make them float to the bottom of the screen until i want to move them. Thanks in advance.


here is what i have so far. I know my syntax is sloppy, just warning you Tongue
Code:


Ship[] ships;
int x;

void setup()
{
 smooth();
 size(500,500);
 ships = new Ship[10];
 ships[0] = new Ship(300,300,1);
 ships[1] = new Ship(150,150,1);
 ships[2] = new Ship(450,200,1);
 ships[3] = new Ship(400,400,1);
 ships[4] = new Ship(500,450,1);
 ships[5] = new Ship(225,200,1);
 ships[6] = new Ship(450,325,1);
 ships[7] = new Ship(100,250,1);
 ships[8] = new Ship(300,150,1);
 ships[9] = new Ship(175,400,1);
 x = 0;

 if (keyPressed == true)
 {
   if (key == '0')
   x = 0;
   else if (key == '1')
   x = 1;
   else if (key == '2')
   x = 2;
   else if (key == '3')
   x = 3;
   else if (key == '4')
   x = 4;
   else if (key == '5')
   x = 5;
   else if (key == '6')
   x = 6;
   else if (key == '7')
   x = 7;
   else if (key == '8')
   x = 8;
   else if (key == '9')
   x = 9;
   if(key == CODED)
   {
     if (keyCode == UP)
     {
       for(int i = 0; i < 10; i++)
       ships[x].moveUP();
     }
     else if (keyCode == DOWN)
     {
       for(int i = 0; i < 10; i++)
       ships[x].moveDOWN();
     }
     else if (keyCode == LEFT)
     {
       for(int i = 0; i < 10; i++)
       ships[x].moveLEFT();
     }
     else if (keyCode == RIGHT)
     {
       for(int i = 0; i < 10; i++)
       ships[x].moveRIGHT();
     }
   }
 }  
}

void draw()
{
 background(125);
 for (int i = 0; i < 10; i++)
 {
   ships[i].drawShip();
 }
}

class Ship
{
 int locationX;
 int locationY;
 float scalar;
 
 Ship(int x, int y, float scl)
 {
   moveShip(x,y);
   scalar = scl;
 }
 
 void moveShip(int x, int y)
 {
   locationX = x;
   locationY = y;
 }

 
 void drawShip()
 {
   pushMatrix();
   strokeWeight(2);
   stroke(0);
   scale(scalar,scalar);
   translate(locationX,locationY);
   fill(192,192,192);
   ellipse(-50,-50,25,25);
   fill(0,255,0);
   ellipse(-50,-40,75,20);  
   fill(0,0,0);
   ellipse(-50,-33,35,10);
   strokeWeight(2);
   stroke(255,0,0);
   popMatrix();
 }

   
void moveUP()
 {
   locationY++;
 }
   
void moveDOWN()
 {
   locationY--;
 }    
   
void moveLEFT()
 {
   locationX--;
 }    
 
void moveRIGHT()
 {
   locationX++;
 }    
 
}


Re: Help moving objects via key presses and more.
Reply #1 - Apr 22nd, 2010, 12:00am
 
They sit in place because the code that's looking for key presses is in setup()! Here's a fixed version:

Quote:
Ship[] ships;
int x;
int[] startX = {300,150,450,400,500,225,450,100,300,175};
int[] startY = {300,150,200,400,450,200,325,250,150,400};
float[] scalars = {1,1,1,1,1,1,1,1,1,1};

void setup(){
  smooth();
  size(500,500);
  ships = new Ship[10];
  for(int i=0;i<ships.length;i++){
    ships[i] = new Ship(startX[i], startY[i],scalars[i]);
  }
  x = 0;
// End of setup()

void draw(){
  background(125);
  for (int i = 0; i < 10; i++)
  {
    ships[i].drawShip();
  }
// End of draw()

void keyPressed(){ // This function is called everytime a key is pressed.
  if( key >= '0' && key <= '9'){
    x = key - '0';
    return;
  }
  if(key == CODED)
  {
    if (keyCode == UP)
    {
      ships[x].moveUP();
    }
    else if (keyCode == DOWN)
    {
      ships[x].moveDOWN();
    }
    else if (keyCode == LEFT)
    {
      ships[x].moveLEFT();
    }
    else if (keyCode == RIGHT)
    {
      ships[x].moveRIGHT();
    }
  } 
// End of keyPressed()

class Ship {
  int locationX;
  int locationY;
  float scalar;
  Ship(int x, int y, float scl)
  {
    locationX = x;
    locationY = y;
    scalar = scl;
  }
  void drawShip()
  {
    pushMatrix();
    strokeWeight(2);
    stroke(0);
    scale(scalar,scalar);
    translate(locationX,locationY);
    fill(192,192,192);
    ellipse(-50,-50,25,25);
    fill(0,255,0);
    ellipse(-50,-40,75,20);  
    fill(0,0,0);
    ellipse(-50,-33,35,10);
    strokeWeight(2);
    stroke(255,0,0);
    popMatrix();
  }
  void moveUP()
  {
    locationY--;
  }
  void moveDOWN()
  {
    locationY++;
  }    
  void moveLEFT()
  {
    locationX--;
  }    
  void moveRIGHT()
  {
    locationX++;
  }    
// End class Ship

Re: Help moving objects via key presses and more.
Reply #2 - Apr 22nd, 2010, 6:59am
 
So that's what the problem was, thanks for the help Smiley
Page Index Toggle Pages: 1