We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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;
}
}
}
Answers
Why thief1.display(); can't display animation if it insert
an error appear
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().
A trimmed version w/ Thief class only. Hope it's easier to figure out now: ~O)
Now it can animate ! PhiLho and GoToLoop a lot of thank .