Animation (Link Isn't Showing!)

I’m a beginner in coding and I’m trying to make a game. I only have the very basics right now but I seem to be having a problem. Link (the character I’m using for the game) Isn’t showing up on the screen when I run the program and only shows when I press “a” or “d” to move left and right. I’m probably just forgetting to include some line of code but any help would be much appreciated. Thank you! (My code I have so far is below)

PImage[] rightImages=new PImage[6];
PImage[] leftImages=new PImage[6];

Float frame=0.0;
//x   y   for link
int[] link={80, 110};
boolean[] keys=new boolean[128];

boolean moveRight=false;
boolean moveLeft=false;

void setup() {
  size(300, 300);
  frameRate(60);
  //loading all "right" images
  for (int i=1; i<rightImages.length; i++) {
    rightImages[i]=loadImage("linkright"+(i)+".png");
  }

  //loading all "left" images
  for (int i=1; i<leftImages.length; i++) {
    leftImages[i]=loadImage("linkleft"+(i)+".png");
  }
}//end setup

void draw() {
  background(200, 80, 50);
  moveLink();

  if (moveRight) {
    image(rightImages[int(frame)], link[0], link[1]);
  } else if (moveLeft) {
    image(leftImages[int(frame)], link[0], link[1]);
  }


  frame+=0.1;
  if (frame>6) {
    frame=1.0;
  }
}//end draw

void moveLink() {
  if (keys[65]) {//65 is 'a' - move left
    link[0]-=3;
    moveLeft=true;
  } else {
    moveLeft=false;
  }

  if (keys[68]) {//68 is 'd' - move right
    link[0]+=3;
    moveRight=true;
  } else {
    moveRight=false;
  }
}//end moveLink

void keyPressed() {
  keys[keyCode]=true;
}

void keyReleased() {
  keys[keyCode]=false;
}
1 Like

</> is your friend

you are only drawing an image if the left or right key is held as soon as those variables go false you draw nothing. it would work if you removed the moveLeft/moveRight vars and just took the sign of link[0] or the sign of the x position of link.

so i don’t have the images to run this but i’m fairly sure this will work

PImage[] rightImages=new PImage[6];
PImage[] leftImages=new PImage[6];

Float frame=0.0;
//x y for link
int[] link={80, 110};
boolean[] keys=new boolean[128];

void setup() {
  size(300, 300);
  frameRate(60);
  //loading all “right” images
  for (int i=1; i<rightImages.length; i++) {
    rightImages[i]=loadImage("linkright"+(i)+".png");
  }

  //loading all “left” images
  for (int i=1; i<leftImages.length; i++) {
    leftImages[i]=loadImage("linkleft"+(i)+".png");
  }
}//end setup

void draw() {
  background(200, 80, 50);
  moveLink();

  if(link[0] > 0) {
    image(rightImages[int(frame)], link[0], link[1]);
  } else {
    image(leftImages[int(frame)], link[0], link[1]);
  }

  frame+=0.1;
  if (frame>6) {
    frame=1.0;
  }
}//end draw

void moveLink() {
  if (keys[65]) {//65 is ‘a’ - move left
    link[0]-=3;
  }

  if (keys[68]) {//68 is ‘d’ - move right
    link[0]+=3;
  }
}//end moveLink

void keyPressed() {
  keys[keyCode]=true;
}

void keyReleased() {
  keys[keyCode]=false;
}

it might be worthwhile looking at building an animation class in which you can set a spritesheet and framerate. you probably don’t want link walking in the spot too so you might need a standing animation so if link[0] === 0 you would draw the frame of that image and make the appropriate changes to the if/else in draw.

2 Likes