making a figure walk

The move function works, but I am trying to get the character to walk vs slide across the screen. Upon adding the if statements for individual images it broke, and I am out of ideas to fix it. I am relatively new to coding, so it's possible that I am over complicating this. Is there an easier method that I am not aware of?

Thanks :-)

//8 bit test walk for Fred the stick figure

// 0 = nothing
// 1 = stand/look
// 2,3,4 = steps

int[] walkArray = 
{
  0, 0, 0, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 1, 0, 0, 0, 0
};

Fred[] freds = new Fred[20];

void setup() 
{
  size(1000, 700);
  rectMode(CENTER);
  frameRate(30);

  for (int i = 0; i < freds.length; i++)
  {
    freds[i] = new Fred (10, 20, 1, walkArray[i]);
  }
}

void draw() 
{ 
  background(0);

  for (int i = 0; i < freds.length; i++)
  {
    freds[i].walk();
  }
}

class Fred 
{ 
  int n = 8;  //number = pixel size
  int count;  //count from array
  int x, y;   //position
  float speed; 

  Fred (int xpos, int ypos, float sp, int c) 
  {  
    x = xpos; 
    y = ypos;
    speed = sp;
    count = c;
  } 
  void walk() 
  { 
    if (frameCount % 5 == 0)  //time delay
    {
      x += speed; 
      if (x > height) 
      { 
        x = 0;
      }
    } 

    if (count == 1) //look right
    {
      background(0);

      noStroke();
      fill(130);
      rect((x-1)*n, (y)*n, 1*n, 6*n);
      rect((x+1)*n, (y)*n, 1*n, 6*n);
      rect((x+.5)*n, (y+2.5)*n, 4*n, 1*n);
    }

    else if (count == 2) //step 1
    {
      background(0);

      noStroke();
      fill(130);
      rect((x)*n, (y)*n, 1*n, 6*n);
      rect((x+.5)*n, (y+2.5)*n, 2*n, 1*n);
      rect((x-1)*n, (y-2)*n, 1*n, 2*n);
      rect((x-2)*n, (y)*n, 1*n, 2*n);
      rect((x-3)*n, (y+2)*n, 1*n, 2*n);
    }

    else if (count == 3) //step 2
    {
      background(0);

      noStroke();
      fill(130);
      rect((x)*n, (y)*n, 1*n, 6*n);
      rect((x+1)*n, (y-2)*n, 1*n, 2*n);
      rect((x+2)*n, (y)*n, 1*n, 2*n);
      rect((x+1)*n, (y+2)*n, 1*n, 2*n);
    }

    else if (count == 4) //step 3
    {
      background(0);

      noStroke();
      fill(130);
      rect((x)*n, (y)*n, 1*n, 6*n);
      rect((x+.5)*n, (y+2.5)*n, 2*n, 1*n);
      rect((x+1)*n, (y-2)*n, 1*n, 2*n);
      rect((x+2)*n, (y)*n, 1*n, 2*n);
      rect((x+3)*n, (y+2)*n, 1*n, 2*n);
    }

    else //no nothing 
    {
      background(0);
    }
  }
}

Answers

  • edited December 2013 Answer ✓

    I haven't dug too deep in your code yet; but at least got it showing some "legs"! :-j

    // forum.processing.org/two/discussion/1916/
    // making-a-figure-walk
    
    //8 bit test walk for Fred the stick figure
    
    // 0 = nothing
    // 1 = stand/look
    // 2,3,4 = steps
    
    static final byte[] walkArray = {
      0, 0, 0, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 1, 0, 0, 0, 0
    };
    
    static final Fred[] freds = new Fred[walkArray.length];
    
    void setup() {
      size(1000, 700);
      smooth(4);
      frameRate(1);
      rectMode(CENTER);
      fill(0200);
      noStroke();
    
      for (int i = 0; i < freds.length; i++)
        freds[i] = new Fred (10, 20, 1);
    }
    
    void draw() { 
      background(0);
      final int frame = frameCount % walkArray.length;
      freds[frame].walk(frame);
    }
    
    class Fred { 
      static final int N = 8;  //number = pixel size
      int x;
      final int y;
      final int speed; 
    
      Fred (int xpos, int ypos, int sp) {  
        x = xpos; 
        y = ypos;
        speed = sp;
      }
    
      void walk(int frame) {
        final int count = walkArray[frame];
    
        if ((x += speed) > height)  x = 0;
    
        if (count == 1) { //look right
          rect((x-1)*N, (y)*N, 1*N, 6*N);
          rect((x+1)*N, (y)*N, 1*N, 6*N);
          rect((x+.5)*N, (y+2.5)*N, 4*N, 1*N);
        }
    
        else if (count == 2) { //step 1
          rect((x)*N, (y)*N, 1*N, 6*N);
          rect((x+.5)*N, (y+2.5)*N, 2*N, 1*N);
          rect((x-1)*N, (y-2)*N, 1*N, 2*N);
          rect((x-2)*N, (y)*N, 1*N, 2*N);
          rect((x-3)*N, (y+2)*N, 1*N, 2*N);
        }
    
        else if (count == 3) { //step 2
          rect((x)*N, (y)*N, 1*N, 6*N);
          rect((x+1)*N, (y-2)*N, 1*N, 2*N);
          rect((x+2)*N, (y)*N, 1*N, 2*N);
          rect((x+1)*N, (y+2)*N, 1*N, 2*N);
        }
    
        else if (count == 4) { //step 3
          rect((x)*N, (y)*N, 1*N, 6*N);
          rect((x+.5)*N, (y+2.5)*N, 2*N, 1*N);
          rect((x+1)*N, (y-2)*N, 1*N, 2*N);
          rect((x+2)*N, (y)*N, 1*N, 2*N);
          rect((x+3)*N, (y+2)*N, 1*N, 2*N);
        }
      }
    }
    
  • awesome! now i'm going to look up the new terms you introduced. :)

    I am a little confused with this bit:

    final int frame = frameCount % walkArray.length;
      freds[frame].walk(frame); 
    

    If i make the array larger will this cause problems? :)

  • edited December 2013

    I've made more cleanups and discoveries as well:
    No need to instantiate more than 1 Fred, since its walk() method knows how to perform all the "steps"! :P

    /** 
     * Walking Fred (v3.01)
     * by  JackMiester (2013/Dec)
     * mod GoToLoop
     * 
     * forum.processing.org/two/discussion/1916/making-a-figure-walk
     */
    
    // 0 = nothing; 1 = stand/look; 2,3,4 = steps.
    
    static final byte[] walks = {
      0, 0, 0, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 1, 0, 0, 0, 0
    };
    
    final Fred fred = new Fred(-Fred.N, 20, 4);
    
    void setup() {
      size(900, 300);
      frameRate(1);
      smooth(4);
    
      rectMode(CENTER);
      fill(0200);
      noStroke();
    }
    
    void draw() { 
      background(0);
      fred.walk(walks[frameCount % walks.length]);
    }
    
    class Fred { 
      static final int N = 8;
    
      int x;
      final int y, s;
    
      Fred (int xx, int yy, int sp) {  
        x = xx; 
        y = yy;
        s = sp;
      }
    
      void walk(int frame) {
        if ((x += s)*N > width - N)   x = -N;
    
        switch(frame) {
        case 1:   //look right
          rect((x-1)*N, (y)*N, 1*N, 6*N);
          rect((x+1)*N, (y)*N, 1*N, 6*N);
          rect((x+.5)*N, (y+2.5)*N, 4*N, 1*N);
          break;
    
        case 2:    //step 1
          rect((x)*N, (y)*N, 1*N, 6*N);
          rect((x+.5)*N, (y+2.5)*N, 2*N, 1*N);
          rect((x-1)*N, (y-2)*N, 1*N, 2*N);
          rect((x-2)*N, (y)*N, 1*N, 2*N);
          rect((x-3)*N, (y+2)*N, 1*N, 2*N);
          break;
    
        case 3:    //step 2
          rect((x)*N, (y)*N, 1*N, 6*N);
          rect((x+1)*N, (y-2)*N, 1*N, 2*N);
          rect((x+2)*N, (y)*N, 1*N, 2*N);
          rect((x+1)*N, (y+2)*N, 1*N, 2*N);
          break;
    
        case 4:    //step 3
          rect((x)*N, (y)*N, 1*N, 6*N);
          rect((x+.5)*N, (y+2.5)*N, 2*N, 1*N);
          rect((x+1)*N, (y-2)*N, 1*N, 2*N);
          rect((x+2)*N, (y)*N, 1*N, 2*N);
          rect((x+3)*N, (y+2)*N, 1*N, 2*N);
        }
      }
    }
    
  • I totally forgot about switch statements! that makes sense! THANK YOU!!!!!! ^:)^

  • edited December 2013

    Well, if..else if..else chained blocks are perfect fine. Very rarely I touch switch...case at all! :-$
    It's just that since your blocks were thick w/ even 5 statements that switch...case is aesthetically better!
    Regular if..else if..else are better for few statements in each 1!

Sign In or Register to comment.