Why my animation in my game isn't action ,when it's move?

edited September 2017 in Library Questions

Hello bro.

I was design my game and I used gifAnimation libraries but when I call thief1.display(); my animation isn't action, it only move.why not?

Thief thief1;
import gifAnimation.*; //import package gifAnimation
// Declare image variable
PImage sleepingman;  
PImage town;
PImage oldman;
PImage thief2;
PImage thief3;
PImage ball;

PImage[] animation;
Gif loopingGif;
boolean pause = false;

//Declare ball object
float x = 240;
float xspeed=10;

// Example 1-2: Bouncing Ball, with PVector!
PVector location;
PVector velocity;
int pos = 0;

float ballX = 150;
float ballY = 580;
float speed = 5;

boolean upPressed = false;
boolean downPressed = false;
boolean leftPressed = false;
boolean rightPressed = false;

void setup() {
  size(1226, 718);
  frameRate(50);
  // The image file must be in the data folder of the current sketch 
  // to load successfully
  town =loadImage("background.jpg");
  sleepingman = loadImage("mansleeping.png");  // Load the image into the program  
  oldman = loadImage("oldman.png");   
  thief2 = loadImage("thief2.png");
  ball = loadImage("ball.png");

  smooth();

  location = new PVector(100, 100);
  velocity = new PVector(2.5, 5);

  loopingGif = new Gif(this, "thiefrun.gif");
  loopingGif.loop();
  //image(loopingGif,); 
  //thief1 = new Thief(680, 570, 5);
  thief1 = new Thief(680, 570, 5);  

}

void draw() {  
  // Displays the image at its actual size at point (0,0)
  background(town);
  image(sleepingman, 550, 520, 200, 200);
  //image(oldman, 150, 580);
  image(thief2, 680, 570);

  image(loopingGif,680,570);  

  x = x+xspeed; //ball +xspeed  
  location.add(velocity); // ball move

  pos++;


  thief1.display(); 
  thief1.move();   

   if (upPressed) {
    ballY -= speed;
  }
  if (downPressed) {
    ballY += speed;
  }
  if (leftPressed) {
    ballX -= speed;
  }
  if (rightPressed) {
    ballX += speed;
  }

  image(oldman,ballX, ballY,180,150);

}
void keyPressed(KeyEvent e) {
  if (key == CODED) {
    if (keyCode == UP) {
      upPressed = true;
    }
    else if (keyCode == DOWN) {
      downPressed = true;
    }
    else if (keyCode == LEFT) {
      leftPressed = true;
    }
    else if (keyCode == RIGHT) {
      rightPressed = true;
    }
  }
}

void keyReleased(KeyEvent e) {
  if (key == CODED) {
    if (keyCode == UP) {
      upPressed = false;
    }
    else if (keyCode == DOWN) {
      downPressed = false;
    }
    else if (keyCode == LEFT) {
      leftPressed = false;
    }
    else if (keyCode == RIGHT) {
      rightPressed = false;
    }
  }
}
class Thief {
  int xspeed;
  int xpos;
  int ypos;
  PImage[] animation;
  Gif loopingGif;
  boolean pause = false;

  //constructor defined
  Thief(int tempXpos, int tempYpos, int tempXspeed) {
    xpos = tempXpos;
    ypos = tempYpos;
    xspeed = tempXspeed;
  }  
  void display() {  
    thief3 = loadImage("thiefrun.gif");
    image(thief3, xpos, ypos);
  }  
  void move() {
    xpos = xpos + xspeed;
    if (xpos > width) {
      xpos = 680;
    }
  }
}

not_action

Tagged:

Answers

  • edited March 2015

    Why thief1.display(); can't display animation if it insert

        class Thief {
          int xspeed;
          int xpos;
          int ypos;
    
          loopingGif = new Gif(this, "thiefrun.gif");
          loopingGif.loop();
    
          //constructor defined
          Thief(int tempXpos, int tempYpos, int tempXspeed) {
            xpos = tempXpos;
            ypos = tempYpos;
            xspeed = tempXspeed;
          }  
          void display() {  
            thief3 = loadImage("thiefrun.gif");
            image(thief3, xpos, ypos);
          }  
          void move() {
            xpos = xpos + xspeed;
            if (xpos > width) {
              xpos = 680;
            }
          }
        }
    

    an error appear

    Unexpected token:loopingGif

  • The first question is unclear.
    The second one is simple: you cannot put instructions, beside variable initializations, outside of any functions. The loop() call should be done in the Thief() constructor, for example.
    Note: you should keep the image loadings in setup().

  • edited March 2015

    A trimmed version w/ Thief class only. Hope it's easier to figure out now: ~O)

    /**
     * Thief Gif Anim (v2.0)
     * by  Champy (2015/Mar/30)
     * mod GoToLoop
     *
     * forum.processing.org/two/discussion/10114/
     * why-my-animation-in-my-game-isn-t-action-when-it-s-move
     */
    
    import gifAnimation.Gif;
    
    Thief  rogue;
    PImage town;
    
    void setup() {
      if (town == null)  town = loadImage("background.jpg");
      size(town.width, town.height, JAVA2D);
    
      smooth(4);
      frameRate(50);
      imageMode(CORNER);
    
      rogue = new Thief(new Gif(this, "thiefrun.gif"), 680, 570, 3);
    }
    
    void draw() {
      background(town);
      rogue.display();
      rogue.move();
    }
    
    class Thief {
      final Gif thief;
      int x;
      final int x0, y, v;
    
      Thief(Gif gif, int posX, int posY, int velX) {
        (thief = gif).loop();
    
        x0 = x = posX;
        y = posY;
        v = velX;
      }
    
      void display() {
        image(thief, x, y);
      }
    
      void move() {
        if ((x += v) > width)  x = x0;
      }
    }
    
  • Now it can animate ! PhiLho and GoToLoop a lot of thank .

Sign In or Register to comment.