Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Animated sprites using tile-based movement

$
0
0

Hey! So I've been working on a Pokemon-style, top-down RPG where the player can move in 32-pixel increments. Recently, I tried adding animation to the player, but everything seems too choppy. I found a similar thread (https://forum.processing.org/one/topic/switch-between-gif-animations-on-keypressed.html), but if there was a solution that would have worked in my code, it must have went right over my head. Any help would be much appreciated! Also, sorry if this is a real noob question, I'm fairly new to this.

Anyway, here's the code:

import ddf.minim.*;

Map map;
Animation animation_left, animation_right, animation_up, animation_down;

int direction = 270;

// Sets default background location by number codes
int mapCode = 0;
// Sets the code for the last background and location visited
int prevMapCode;

boolean moving = false;

int timeFrame = 1;

int gameMode = 0;

//Starting location of player (Player will always be centered. Background moves.)
final int playerX = 128;
final int playerY = 128;

int bgX = -96;
int bgY = -192;

int targetX = -96;
int targetY = -192;

Minim minim;
AudioPlayer mainTheme;

PImage playerup;
PImage playerdown;
PImage playerleft;
PImage playerright;
PImage pauseImg;
PImage palletImg;

void setup() {

  size(320,288);
  map = new Map();
  minim = new Minim(this);
  animation_up = new Animation("playeru", 4);
  animation_down = new Animation("playerd", 4);
  animation_left = new Animation("playerl", 2);
  animation_right = new Animation("playerr", 2);
  loadResources();

  imageMode(CORNER);
  rectMode(CORNER);

}

void draw() {

  if(gameMode == 0) {

    splashScreen();

  }
  else if(gameMode == 1) {

    background(0);

    map.loadMap();

    map.loadBounds();

    loadPlayer();

    pauseMenu();

  }

}

void loadResources(){

  playerup = loadImage("Images/plSprites/playeru0000.gif");
  playerdown = loadImage("Images/plSprites/playerd0000.gif");
  playerleft = loadImage("Images/plSprites/playerr0000.gif");
  playerright = loadImage("Images/plSprites/playerl0000.gif");
  pauseImg = loadImage("Images/pauseMenu.png");
  palletImg = loadImage("Images/maps/palletImg.png");

}

void pauseMenu() {

    if(timeFrame == 0)
      image(pauseImg, 160, 0, 160, 256);

}

void loadPlayer() {


    //Temp player rectangle
    //noStroke();
    //fill(#0000FF);
    //rect(playerX, playerY, 32, 32);

    if (targetX > bgX) {
      bgX = bgX + 2;
    }
    if (targetX < bgX) {
      bgX = bgX - 2;
    }
    if (targetY > bgY) {
      bgY = bgY + 2;
    }
    if (targetY < bgY) {
      bgY = bgY - 2;
    }

    // Checks to see if the player is moving
    if (targetX == bgX && targetY == bgY) {
      moving = false;
      //Stationary images
      if(direction == 0){
        image(playerleft, playerX, playerY, 32, 32);
      }else if(direction == 90){
        image(playerup, playerX, playerY, 32, 32);
      }else if(direction == 180){
        image(playerright, playerX, playerY, 32, 32);
      }else if(direction == 270){
        image(playerdown, playerX, playerY, 32, 32);
      }
    }

    if(mousePressed){

    if (key == CODED){

      if (keyCode == DOWN && moving && timeFrame == 1) {

        animation_down.display(playerX, playerY);

      }else if(keyCode == UP && moving && timeFrame == 1) {

        animation_up.display(playerX, playerY);

      } else if (keyCode == RIGHT && moving && timeFrame == 1) {

        animation_right.display(playerX, playerY);

      } else if (keyCode == LEFT && moving && timeFrame == 1) {

        animation_left.display(playerX, playerY);

      }

    }

  }

}

void keyPressed(){

  if (key == 'p' || key == 'P') {

    // PAUSE - Toggles timeframe
    timeFrame = 1 - timeFrame;

  } else if (key == CODED){

    if (keyCode == DOWN && !moving && timeFrame == 1) {

        direction = 270;
        moving = true;
        targetY -= 32;

    } else if (keyCode == UP && !moving && timeFrame == 1) {

        direction = 90;
        moving = true;
        targetY += 32;

    } else if (keyCode == RIGHT && !moving && timeFrame == 1) {

        direction = 0;
        moving = true;
        targetX -= 32;

    } else if (keyCode == LEFT && !moving && timeFrame == 1) {

        direction = 180;
        moving = true;
        targetX += 32;

    }

  }
}


class Animation
{

  PImage[] images;
  int imageCount;
  int frame;

  Animation(String imagePrefix, int count) {

    imageCount = count;
    images = new PImage[imageCount];

    for(int i=0; i<imageCount; i++) {
      //Uses nf() to make the number format 4 digits long
      String filename = "Images/plSprites/" + imagePrefix + nf(i,4) + ".gif";
      images[i] = loadImage(filename);
    }

  }

  void display(float xpos, float ypos) {

    frame = (frame+1) % imageCount;
    image(images[frame], xpos, ypos);

  }

  int getWidth() {

    return images[0].width;

  }

}

Viewing all articles
Browse latest Browse all 2896

Trending Articles