hello, i am a new starter with processing and have become really stuck on how to animate a character or player. i was wondering if anyone could start me off or point me in the right direction.i have added the code i have so far below:
//key pressed variables
boolean leftPressed = false;
boolean rightPressed = false;
boolean upPressed = false;
PImage w;
PImage e;
int y;
int x = 470;
int r;
Player myPlayer;
Platform[] platforms = new Platform[10];//AN array to set the amount of platforms.
void setup() {
size(500,500);
smooth();
noStroke();
myPlayer = new Player();
w = loadImage("Ground.png");//Loading the image from the data file.
e = loadImage("Background2.png");//Loading the image from the data file.
for(int i=0; i<platforms.length; i++) {
platforms[i] = new Platform(random(width),random(height));
}
}
void draw() {
background(255);
image(e,r,y);
image(w,y,x);
for(int i=0; i<platforms.length; i++) {
platforms[i].update();
}
myPlayer.update();
myPlayer.collisionCheck();
}
void keyPressed() {
if (key == CODED) {
if (keyCode == LEFT) leftPressed = true;
else if (keyCode == RIGHT) rightPressed = true;
else if (keyCode == UP ) upPressed = true;
}
}
void keyReleased() {
if (key == CODED) {
if (keyCode == LEFT) leftPressed = false;
else if (keyCode == RIGHT) rightPressed = false;
else if (keyCode == UP) upPressed = false;
}
}
class Platform {
float x;
float y;
PImage s;
int yvel = 1;//Setting the speed of the platforms.
int blocklength = 120;//Setting the length of the platforms.
int blockheight = 10;//Setting the height of the platforms.
color c = color(0);//Setting the colour of the platforms.
Platform(float xpos, float ypos) {
x = xpos;
y = ypos;
s = loadImage("Platform.png");//Loading the image from the data file.
}
void update() {
y+=yvel;
if(y+blockheight > height || y < 0) {
yvel = -yvel;
}
//fill(c);
image(s,x,y);
}
}
class Player {
PImage q;//Sets image name.
float x;
float y;
int xspeed = 7;//Sets movement speed of player.
int jumpspeed = 25;//Sets how high the player can jump.
int drag = 1;
float playersize;
float playerbase;
int gravity = 5;//Sets how fast the player hits the ground.
Player() {
q = loadImage("mario.png");//Loading the image from the data file.
x = width/2;
y = height/2;
playersize = 37;//Size of the player.
}
void jump() {
if(upPressed) {
jumpspeed *= drag;
y-=jumpspeed;
println(y);
}
}
void update() {
if(leftPressed) x-=xspeed;//speed on which the player moves to the left.
else if(rightPressed) x+=xspeed;//speed on which the player moves to the right.
if(y>430) y=430;//Stopping the character from going through the ground.
if(x<0) x=0;//stopping the character from going off the screen.
if((x+playersize)>width) x=(width-playersize);//stopping the character from going off the screen.
jump();
fill(255);
image(q,x,y);
y+=gravity;
}
void collisionCheck() {//creates the method to detect collisions.
playerbase = y+playersize;
if(playerbase > height ) {
y = height-playersize;
}
for(int i=0; i<platforms.length; i++) {
if(playerbase > platforms[i].y && playerbase < platforms[i].y+platforms[i].blockheight && x > platforms[i].x && x < platforms[i].x+platforms[i].blocklength) {