How to move two things at once?

If I have 2 ellipses, and then I have two different sets of keys then how can I move them at the same time? My code works but I can only move one of them at a time, not together.

Comments

  • edited October 2013

    My faithful Move Object example: (~~)
    http://studio.processingtogether.com/sp/pad/export/ro.9bY07S95k2C2a/latest

    In there, I use 4 boolean variables -> north, south, west, east. And 2 int 1s for position -> x, y.

    What you can do is expand them to something like:
    * north1, south1, west1, east1. x1, y1, w1, h1.
    * north2, south2, west2, east2. x2, y2, w2, h2.

    So each group represents a different moving sprite! (*)
    And of course, you can encapsulate all of those variables into a class. *-:)

  • edited October 2013

    Hmm some of my code is similar to yours but I don't quite understand some lines in your code. Here is my code.

     final static int N1= 1;                           //players can move at the same time
    final static int S1 = 2;                           
    final static int E1 = 4;                           
    final static int W1 = 8;                           // Speed limit. Round out edges for field (silhouette)
    // if time Collision detection
    
    final static int N2 = 16;
    final static int S2 = 32;
    final static int E2 = 64;
    final static int W2 = 128;
    
    
    final static int N3 = 256;
    final static int S3 = 512;
    final static int E3 = 1024;
    final static int W3 = 2048;
    
    final static int N4 = 4096;
    final static int S4 = 8192;
    final static int E4 = 16384;
    final static int W4 = 32768;
    
    int result, k;                                  //store key input and initial number of players on field
    float x1, x2, x3, x4, y1, y2, y3, y4, n, r;             //Decleare some useful variables
    
    boolean sketchFullScreen() {                        //Make full screen
      return true;
    }
    
    void setup() {                                      //Basic setup
      size(displayWidth, displayHeight);                //Window size maximum
      noCursor();                                       //Hide mouse cursor
      frameRate(60);                                   //set the frame rate, can lower the rate for lower performent pc
      result = 0;
      k = 1;
      n = 8;                                            //initial movement speed
      r = 15;                                           //initial radius of bubble
    
      x1 = 2*width/3;                                    
      y1 = height/3; 
      x2 = 2*width/3;                                    //light dot initial position
      y2 = 2*height/3; 
      x3 = width/3;                                  
      y3 = height/3; 
      x4 = width/3;                                    
      y4 = 2*height/3;
    }
    
    void draw() {                                        //Start drawing
      background(0);                                     //Black background(COMPLETELY BLACK)
      switch(result) {   
      case N1: 
        y1=y1-n; 
        break;
      case E1: 
        x1=x1+n; 
        break;
      case S1: 
        y1=y1+n; 
        break;
      case W1: 
        x1=x1-n; 
        break;                            //create movement to the dot by reassigning the coordinates
      case N1|E1: 
        y1=y1-n; 
        x1=x1+n; 
        break;                    
      case N1|W1: 
        y1=y1-n; 
        x1=x1-n; 
        break;
      case S1|E1: 
        y1=y1+n; 
        x1=x1+n; 
        break;
      case S1|W1: 
        y1=y1+n; 
        x1=x1-n; 
        break;
    
      case N2: 
        y2=y2-n; 
        break;
      case E2: 
        x2=x2+n; 
        break;
      case S2: 
        y2=y2+n; 
        break;
      case W2: 
        x2=x2-n; 
        break;                            //create movement to the dot by reassigning the coordinates
      case N2|E2: 
        y2=y2-n; 
        x2=x2+n; 
        break;                    
      case N2|W2: 
        y2=y2-n; 
        x2=x2-n; 
        break;
      case S2|E2: 
        y2=y2+n; 
        x2=x2+n; 
        break;
      case S2|W2: 
        y2=y2+n; 
        x2=x2-n; 
        break;
    
      case N3: 
        y3=y3-n; 
        break;
      case E3: 
        x3=x3+n; 
        break;
      case S3: 
        y3=y3+n; 
        break;
      case W3: 
        x3=x3-n; 
        break;                            //create movement to the dot by reassigning the coordinates
      case N3|E3: 
        y3=y3-n; 
        x3=x3+n; 
        break;                    
      case N3|W3: 
        y3=y3-n; 
        x3=x3-n; 
        break;
      case S3|E3: 
        y3=y3+n; 
        x3=x3+n; 
        break;
      case S3|W3: 
        y3=y3+n; 
        x3=x3-n; 
        break;
    
      case N4: 
        y4=y4-n; 
        break;
      case E4: 
        x4=x4+n; 
        break;
      case S4: 
        y4=y4+n; 
        break;
      case W4: 
        x4=x4-n; 
        break;                            //create movement to the dot by reassigning the coordinates
      case N4|E4: 
        y4=y4-n; 
        x4=x4+n; 
        break;                    
      case N4|W4: 
        y4=y4-n; 
        x4=x4-n; 
        break;
      case S4|E4: 
        y4=y4+n; 
        x4=x4+n; 
        break;
      case S4|W4: 
        y4=y4+n; 
        x4=x4-n; 
        break;
      }
    
      if (k>=5) {
        k=4;
      }                                  //max bumber of players is 4
      if (k<0) {
        k=0;
      }                                   //least amount of players is 0
    
    
        if (x1<0) {
        x1=r/2;
      }
      else if (x1>displayWidth) {
        x1=displayWidth-r/2;
      }
      if (x2<0) {
        x2=r/2;
      }
      else if (x2>displayWidth) {
        x2=displayWidth-r/2;
      }
      if (x3<0) {
        x3=r/2;
      }
      else if (x3>displayWidth) {
        x3=displayWidth-r/2;
      }
      if (x4<0) {
        x4=r/2;
      }
      else if (x4>displayWidth) {
        x4=displayWidth-r/2;
      }
    
      if (y1<0) {
        y1=r/2;
      }
      else if (y1>displayHeight) {
        y1=displayHeight-r/2;
      }
      if (y2<0) {
        y2=r/2;
      }
      else if (y2>displayHeight) {
        y2=displayHeight-r/2;
      }
      if (y3<0) {
        y3=r/2;
      }
      else if (y3>displayHeight) {
        y3=displayHeight-r/2;
      }
      if (y4<0) {
        y4=r/2;
      }
      else if (y4>displayHeight) {
        y4=displayHeight-r/2;
      }
    
    
      stroke(255);                                        //White light stroke
      if (k==1) {
        ellipse(x1, y1, r, r);
      }                       //Draw an ellipse at location (x,y) xwith radius of 15 pixels
      if (k==2) {
        ellipse(x1, y1, r, r); 
        ellipse(x2, y2, r, r);
      }
    
      if (k==3) {
        ellipse(x1, y1, r, r); 
        ellipse(x2, y2, r, r);
        ellipse(x3, y3, r, r);
      }
    
      if (k==4) {
        ellipse(x1, y1, r, r); 
        ellipse(x2, y2, r, r);
        ellipse(x3, y3, r, r);
        ellipse(x4, y4, r, r);
      }
    }         
    
    void keyPressed() {
      switch(key) {
        case('w'):
        case('W'):
        result |=N1;
        break;
        case('d'):
        case('D'):
        result |=E1;
        break;
        case('s'):
        case('S'):
        result |=S1;
        break;              //classify the cases for the key pressed
        case('a'):
        case('A'):
        result |=W1;
        break;
      }
      switch(key) { 
        case('8'):
        result |=N2;
        break;
        case('6'):
        result |=E2;
        break;
        case('5'):
        result |=S2;
        break;              
        case('4'):
        result |=W2;
        break;
      }
      switch(key) {
        case('t'):
        case('T'):
        result |=N3;
        break;
        case('h'):
        case('H'):
        result |=E3;
        break;
        case('g'):
        case('G'):
        result |=S3;
        break;              
        case('f'):
        case('F'):
        result |=W3;
        break;
      }
      switch(key) {
        case('i'):
        case('I'):
        result |=N4;
        break;
        case('l'):
        case('L'):
        result |=E4;
        break;
        case('k'):
        case('K'):
        result |=S4;
        break;              
        case('j'):
        case('J'):
        result |=W4;
        break;
      }
      switch(key) {
        case('+'):
        case('='): 
        n++;
        break;
        case('_'):
        case('-'): 
        n--;
        break;                    //change movement speed
        case('['):
        case('{'): 
        r=r-1;
        break;                  //change radius of bubble
        case(']'):
        case('}'): 
        r=r+1;
        break;
        case(':'): 
        k--;
        break;                            //add and take away players
        case('"'): 
        k++;
        break;
      }
    }
    
    void keyReleased() {  
      switch(key) {                                      
        case('w'):
        case('W'):
        result ^=N1;
        break;
        case('d'):
        case('D'):
        result ^=E1;
        break;
        case('s'):
        case('S'):
        result ^=S1;
        break;              //classify the cases for the key pressed
        case('a'):
        case('A'):
        result ^=W1;
        break;
      }
      switch(key) {  
        case('8'):
        result ^=N2;
        break;
        case('6'):
        result ^=E2;
        break;
        case('5'):
        result ^=S2;
        break;              
        case('4'):
        result ^=W2;
        break;
      }
      switch(key) {
        case('t'):
        case('T'):
        result ^=N3;
        break;
        case('h'):
        case('H'):
        result ^=E3;
        break;
        case('g'):
        case('G'):
        result ^=S3;
        break;              
        case('f'):
        case('F'):
        result ^=W3;
        break;
      }
      switch(key) {
        case('i'):
        case('I'):
        result ^=N4;
        break;
        case('l'):
        case('L'):
        result ^=E4;
        break;
        case('k'):
        case('K'):
        result ^=S4;
        break;              
        case('j'):
        case('J'):
        result ^=W4;
        break;
      } 
      switch(key) {    
        case('+'):
        case('='): 
        n++;
        break;
        case('_'):
        case('-'): 
        n--;
        break;
        case('['):
        case('{'): 
        r=r-1;
        break;
        case(']'):
        case('}'): 
        r=r+1;
        break;
      }
    }
    }
    
  • If you don't understand anything from my example, just ask! :bz
    And when posting a Processing code, select that and press CTRL+K in order to format it! >:)

  • I posted my code and reformated it. can you take a look?

  • So, it's not 2 players, but 4!!! :-O
    You should use arrays in order to shorten your code w/ loops. Or better yet, create a Player class! <):)

  • Oh yes, sorry about that! 4 players. I tried to used arrays but it was really messy. Can you show me how to make a Player class?

Sign In or Register to comment.