Simple platformer game question

edited July 2015 in Questions about Code

Hi,

I was working on a simple platformer game. The simple idea is that there are three arrays. Those arrays contain the obstacles.

One array for the x cord, one for the y cord, and one for the radius of the obstacle.

What i have working so far is the player to be able to go to the obstacles, and it will automatically move up. the issue is it is not moving down.

The way it checks is it has a for loop. the for loop runs through the array and detects if the x and y cord of the player overlap with the obstacle. the problem is when it is hitting one obstacle, that obstacle is telling it to go up but the other one is telling it to go down, so in the end nothing really happens. Heres the code.



//Start
int ex = 20;
int ey = 480;
int ew = 20;
int[] xo = {
  400, 
  200
};
int[] yo = {
  490, 
  490
};
int[] ro = {
  50, 
  50
};
//Define Obstacles
//Program
void setup() {
  size(750, 500);//--------------------------
}
void draw() {

  background(200, 200, 200);
  fill(0, 0, 255);
  ellipse(ex, ey, 20, 20);
  for (int i = 0; i < xo.length; i++) {
    fill(255, 255, 255);
    ellipse(xo[i], yo[i], ro[i], ro[i]);
    if (dist(ex, ey, xo[i], yo[i]) < ro[i]-ew/2) {
      println("up");
      ey--;
    }
  }
}


void keyPressed() {
  if (keyCode == UP) {
    ey-= 15;
  } 
  if (keyCode == LEFT) {
    ex-= 5;
  }
  if (keyCode == RIGHT) {
    ex+= 5;
  }
}
//end

If anybody could help out that would be great. thanks!

edit: I fixed it.

Tagged:
Sign In or Register to comment.