Sprite Jump Animation Problem
in
Programming Questions
•
3 years ago
Hi
,
I'm making a 2d sidescroller and my code, at the moment is all over the place. Currently, I am working on trying to get my jump animation to work when pressing the Up Key. I can't get it to animate correctly, and I am pressed for time for this project. I still haven't implemented collisionDetection, and I probably need help on that too. I think one way to do it would be to detect if there is a certain element below the player and if there is, etc..etc..but I can't figure out how to code it correctly if that is the case. If anyone is willing to help me, I would appreciate it. I want to learn, so if my code is extremely off or is terrible, please tell me how to improve it. The code and classes are posted below.
Level1 Class:
int x,y;
int frame=0;
int frameY=0;
PImage bg;
TileMap map;
int scrollNum=0;
int scrollNumY=0;
String images[]= {"blank.gif","tree_1.gif","tree_2.gif","tree_3.gif",
"tree_4.gif","tree_5.gif","tree_6.gif","tree_7.gif","tree_8.gif",
"tree_9.gif","tree_10.gif","tree_11.gif","tree_12.gif","tree_13.gif",
"tree_14.gif","tree_15.gif","tree_16.gif","tree_17.gif","tree_18.gif",
"tree_19.gif","tree_20.gif","tree_21.gif","tree_22.gif","tree_23.gif",
"tree_24.gif","tree_25.gif","tree_26.gif","tree_27.gif","tree_28.gif",
"tree_29.gif","Platform1.png","platform2.png","platform3.png",
"tree_bush.gif","rock 1.png","rock2.png","rock3.png","rock6.png",
"bridge_1.png","bridge_2.png","bridge_3.png"};
boolean hScroll = true;
boolean hScrollL= true;
boolean vScroll=true;
boolean vScrollU=true;
int xPos;
int yPos;
float spriteX=32;
float spriteY=170;
Mandrill mandrill;
boolean walkRight=false;
boolean walkLeft=false;
boolean jumpRight= false;
boolean jumpLeft=false;
boolean fallRight=false;
boolean fallLeft=false;
boolean isFacingRight=true;
boolean isFacingLeft=false;
void setup()
{
size(550,440);
map= new TileMap(100,20);
frameRate(5);
bg=loadImage("level_1_background.png");
for(int i=0;i<100;i++)
{
for(int j=0;j<20;j++)
{
int imageIndex= tile_map[j][i];
String imageName=images[imageIndex];
map.setTile(loadImage(imageName),j,i);
}
}
mandrill= new Mandrill(spriteX,spriteY);
}
void draw()
{
frame= constrain(frame,0,bg.width-width);
frameY= constrain(frameY,0,bg.height-height);
set(-frame,0,bg);
for(int i=0; i< map.getHeight();i++)
{
for(int j=0;j< map.getWidth();j++)
{
image(map.getTile(j,i),(32*j)-scrollNum,(32*i)-scrollNumY);
xPos=(32*j)-scrollNum;
yPos=(32*i)-scrollNumY;
}
}
if(walkRight==true)
{
mandrill.walkRight(spriteX,spriteY);
}
else if( walkLeft==true)
{
mandrill.walkLeft(spriteX,spriteY);
}
else if(jumpRight==true)
{
mandrill.jumpRight(spriteX,spriteY);
}
else if(jumpLeft==true)
{
mandrill.jumpLeft(spriteX,spriteY);
}
else
{
if(isFacingRight==true)
mandrill.drawRight(spriteX,spriteY);
if(isFacingLeft==true)
mandrill.drawLeft(spriteX,spriteY);
}
if(xPos <= 516) hScroll = false;
else hScroll = true;
if(xPos >=3200) hScrollL=false;
else hScrollL=true;
if(yPos>=734) vScroll=false;
else vScroll=true;
if(yPos <=410) vScrollU=false;
else vScrollU=true;
}
void keyPressed (KeyEvent e)
{
int key=e.getKeyCode();
switch(key)
{
case 37: //left
if(hScrollL==false)
scrollNum+=0;
else
{
if(spriteX<100)
{
scrollNum-=14;
frame--;
}
walkLeft=true;
spriteX-=6;
}
break;
case 39: //right
if(hScroll == false)
{
scrollNum-=0;
}
else
{
if(spriteX>width/2)
{
scrollNum+=14;
frame++;
}
walkRight=true;
spriteX+=6;
}
break;
case 38: //up
//if(spriteY<height/2)
//{
//scrollNumY-=14;
//}
jumpRight=true;
spriteY-=2;
spriteX++;
break;
case 40:
if(vScrollU==false)
scrollNumY-=0;
else
{
scrollNumY+=6;
}
}
}
void keyReleased(KeyEvent e)
{
int key= e.getKeyCode();
switch(key)
{
case 37:
walkLeft=false;
isFacingRight=false;
isFacingLeft=true;
break;
case 39:
walkRight=false;
isFacingRight=true;
isFacingLeft=false;
break;
case 38:
//scrollNum+=1;
jumpRight=false;
break;
case 40:
}
}
int[][]tile_map={
{0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4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0,0,0,0,0,0,0,23,24,25,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
23,24,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,31,0,34,36,37,0,0,0,0,34,
35,35,35,35,35,35,35,35,35,37,0,0,0,
0,0,0,0,0,0,0,0,31,31,31,31,31,31,31,
31,32,0
},
{0,0,0,0,0,0,0,33,33,0,0,33,0,26,27,28,
29,33,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,26,27,28,29,33,0,0,0,0,0,0,0,33,33,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
34,35,35,35,35,35,35,35,37,0,0,0,0,0,0,
0,0,0,0,0,0,35,35,35,35,35,35,35,35,37,
0
},
{0,30,31,31,31,31,31,31,31,31,31,31,31,
31,31,31,31,31,32,0,0,0,0,0,30,31,31,
31,31,31,31,31,31,31,31,31,31,31,31,31,
31,31,31,31,31,31,31,31,31,31,31,31,0,
0,0,0,31,31,32,0,0,0,0,0,0,0,0,0,0,0,
34,35,35,35,36,36,37,0,0,0,0,0,0,0,0,
0,33,33,0,0,35,35,35,35,35,35,35,37,0,
0
},
{0,34,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,37,0,0,0,0,0,34,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,0,
0,0,0,35,35,37,0,30,31,32,0,0,0,0,0,0,
0,0,36,36,36,0,0,0,0,0,0,0,0,0,0,31,
31,31,31,31,31,31,31,32,36,36,36,36,0,
0,0
},
{0,0,34,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,37,0,0,0,30,32,0,0,34,35,
35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,
35,31,31,31,31,35,37,0,0,35,35,35,32,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,35,35,35,35,35,35,35,35,37,0,0,
0,0,0,0,0
},
{0,0,0,34,36,36,36,35,35,35,36,36,36,
36,36,36,37,0,0,0,0,36,36,0,0,0,34,
35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,37,0,0,0,35,35,35,35,
31,32,0,0,0,0,38,39,39,39,39,40,0,0,
0,0,0,0,0,33,35,35,35,35,35,35,35,37,
0,0,0,0,0,0,0,0
},
{0,0,0,0,0,0,0,36,36,36,0,0,0,0,0,0,0,
33,0,0,0,0,0,0,0,0,0,34,36,34,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,35,35,35,35,35,35,37,
0,0,0,0,34,35,35,35,35,35,31,31,31,
31,0,0,0,0,0,0,31,31,31,31,31,31,31,
31,32,36,35,35,36,36,36,0,0,0,0,0,0,
0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,30,
31,32,0,0,0,0,0,0,0,0,0,0,34,36,36,36,
36,36,36,36,36,36,36,36,36,36,36,36,
36,36,36,36,36,36,36,36,37,0,0,0,0,0,
0,34,35,35,35,35,35,35,35,35,0,0,0,0,
0,0,34,35,35,35,35,35,35,35,37,0,36,
36,0,0,0,0,0,0,0,0,0,0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,36,
36,36,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,36,36,35,35,35,35,35,
36,0,0,0,0,0,0,0,34,35,35,35,35,
35,37,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,30,31,31,32,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,34,36,36,36,
37,0,0,0,0,0,0,0,0,0,36,36,36,36,
36,0,0,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,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0
}
};
TileMap Class:
class TileMap
{
public int width, height;
PImage[][] tiles;
TileMap(int x, int y)
{
this.width = x;
this.height = y;
tiles = new PImage[y][x];
}
int getWidth()
{
return width;
}
int getHeight()
{
return height;
}
void setTile(PImage image, int x, int y)
{
tiles[x][y] = image;
}
PImage getTile(int x, int y)
{
if(x < 0 || x > width || y < 0 || y > height)
{
println("Tile doesn't exist");
return null;
}
else
{
return tiles[y][x];
}
}
}
Player Class (a Mandrill):
class Mandrill
{
float xPos;
float yPos;
PImage mandrill;
PImage mandrill1;
int frame;
float gravity=-9.5;
Mandrill(float x,float y)
{
xPos=x;
yPos=y;
mandrill=loadImage("mandrill_walk0.gif");
mandrill1=loadImage("mandrill_walk_left0.gif");
}
void drawRight(float x,float y)
{
image(mandrill,x, y);
}
void drawLeft(float x,float y)
{
image(mandrill1,x,y);
}
void setXPos(float x)
{
xPos=x;
}
void setYPos(float y)
{
yPos=y;
}
float getXPos()
{
return xPos;
}
float getYPos()
{
return yPos;
}
void walkRight(float x,float y)
{
PImage[] images= new PImage[4];
for (int i = 0; i < 4; i++)
{
String filename = "mandrill_walk" + i + ".gif";
images[i] = loadImage(filename);
}
frameRate(8);
frame = (frame+1) % 4;
image(images[frame], x, y);
}
void walkLeft(float x, float y)
{
PImage[] images= new PImage[4];
for (int i = 0; i < 4; i++)
{
String filename = "mandrill_walk_left" + i + ".gif";
images[i] = loadImage(filename);
}
frameRate(8);
frame = (frame+1) % 4;
image(images[frame], x, y);
}
void jumpRight(float x, float y)
{
PImage[] images= new PImage[6];
for (int i = 0; i < 6; i++)
{
String filename = "mandrill_jump" + i + ".gif";
images[i] = loadImage(filename);
}
frameRate(8);
frame=(frame+1) % 4;
image(images[frame], x, y);
}
void jumpLeft(float x, float y)
{
PImage[] images= new PImage[6];
for (int i = 0; i < 6; i++)
{
String filename = "mandrill_jump_left" + i + ".gif";
images[i] = loadImage(filename);
}
frameRate(5);
frame = (frame+1) % 4;
image(images[frame], x, y);
}
}
I'm making a 2d sidescroller and my code, at the moment is all over the place. Currently, I am working on trying to get my jump animation to work when pressing the Up Key. I can't get it to animate correctly, and I am pressed for time for this project. I still haven't implemented collisionDetection, and I probably need help on that too. I think one way to do it would be to detect if there is a certain element below the player and if there is, etc..etc..but I can't figure out how to code it correctly if that is the case. If anyone is willing to help me, I would appreciate it. I want to learn, so if my code is extremely off or is terrible, please tell me how to improve it. The code and classes are posted below.
Level1 Class:
int x,y;
int frame=0;
int frameY=0;
PImage bg;
TileMap map;
int scrollNum=0;
int scrollNumY=0;
String images[]= {"blank.gif","tree_1.gif","tree_2.gif","tree_3.gif",
"tree_4.gif","tree_5.gif","tree_6.gif","tree_7.gif","tree_8.gif",
"tree_9.gif","tree_10.gif","tree_11.gif","tree_12.gif","tree_13.gif",
"tree_14.gif","tree_15.gif","tree_16.gif","tree_17.gif","tree_18.gif",
"tree_19.gif","tree_20.gif","tree_21.gif","tree_22.gif","tree_23.gif",
"tree_24.gif","tree_25.gif","tree_26.gif","tree_27.gif","tree_28.gif",
"tree_29.gif","Platform1.png","platform2.png","platform3.png",
"tree_bush.gif","rock 1.png","rock2.png","rock3.png","rock6.png",
"bridge_1.png","bridge_2.png","bridge_3.png"};
boolean hScroll = true;
boolean hScrollL= true;
boolean vScroll=true;
boolean vScrollU=true;
int xPos;
int yPos;
float spriteX=32;
float spriteY=170;
Mandrill mandrill;
boolean walkRight=false;
boolean walkLeft=false;
boolean jumpRight= false;
boolean jumpLeft=false;
boolean fallRight=false;
boolean fallLeft=false;
boolean isFacingRight=true;
boolean isFacingLeft=false;
void setup()
{
size(550,440);
map= new TileMap(100,20);
frameRate(5);
bg=loadImage("level_1_background.png");
for(int i=0;i<100;i++)
{
for(int j=0;j<20;j++)
{
int imageIndex= tile_map[j][i];
String imageName=images[imageIndex];
map.setTile(loadImage(imageName),j,i);
}
}
mandrill= new Mandrill(spriteX,spriteY);
}
void draw()
{
frame= constrain(frame,0,bg.width-width);
frameY= constrain(frameY,0,bg.height-height);
set(-frame,0,bg);
for(int i=0; i< map.getHeight();i++)
{
for(int j=0;j< map.getWidth();j++)
{
image(map.getTile(j,i),(32*j)-scrollNum,(32*i)-scrollNumY);
xPos=(32*j)-scrollNum;
yPos=(32*i)-scrollNumY;
}
}
if(walkRight==true)
{
mandrill.walkRight(spriteX,spriteY);
}
else if( walkLeft==true)
{
mandrill.walkLeft(spriteX,spriteY);
}
else if(jumpRight==true)
{
mandrill.jumpRight(spriteX,spriteY);
}
else if(jumpLeft==true)
{
mandrill.jumpLeft(spriteX,spriteY);
}
else
{
if(isFacingRight==true)
mandrill.drawRight(spriteX,spriteY);
if(isFacingLeft==true)
mandrill.drawLeft(spriteX,spriteY);
}
if(xPos <= 516) hScroll = false;
else hScroll = true;
if(xPos >=3200) hScrollL=false;
else hScrollL=true;
if(yPos>=734) vScroll=false;
else vScroll=true;
if(yPos <=410) vScrollU=false;
else vScrollU=true;
}
void keyPressed (KeyEvent e)
{
int key=e.getKeyCode();
switch(key)
{
case 37: //left
if(hScrollL==false)
scrollNum+=0;
else
{
if(spriteX<100)
{
scrollNum-=14;
frame--;
}
walkLeft=true;
spriteX-=6;
}
break;
case 39: //right
if(hScroll == false)
{
scrollNum-=0;
}
else
{
if(spriteX>width/2)
{
scrollNum+=14;
frame++;
}
walkRight=true;
spriteX+=6;
}
break;
case 38: //up
//if(spriteY<height/2)
//{
//scrollNumY-=14;
//}
jumpRight=true;
spriteY-=2;
spriteX++;
break;
case 40:
if(vScrollU==false)
scrollNumY-=0;
else
{
scrollNumY+=6;
}
}
}
void keyReleased(KeyEvent e)
{
int key= e.getKeyCode();
switch(key)
{
case 37:
walkLeft=false;
isFacingRight=false;
isFacingLeft=true;
break;
case 39:
walkRight=false;
isFacingRight=true;
isFacingLeft=false;
break;
case 38:
//scrollNum+=1;
jumpRight=false;
break;
case 40:
}
}
int[][]tile_map={
{0,0,0,0,0,0,0,0,0,0,0,0,1,2,3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0,0,0,0,0,0,0,0,23,24,25,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
23,24,25,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,31,0,34,36,37,0,0,0,0,34,
35,35,35,35,35,35,35,35,35,37,0,0,0,
0,0,0,0,0,0,0,0,31,31,31,31,31,31,31,
31,32,0
},
{0,0,0,0,0,0,0,33,33,0,0,33,0,26,27,28,
29,33,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,26,27,28,29,33,0,0,0,0,0,0,0,33,33,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
34,35,35,35,35,35,35,35,37,0,0,0,0,0,0,
0,0,0,0,0,0,35,35,35,35,35,35,35,35,37,
0
},
{0,30,31,31,31,31,31,31,31,31,31,31,31,
31,31,31,31,31,32,0,0,0,0,0,30,31,31,
31,31,31,31,31,31,31,31,31,31,31,31,31,
31,31,31,31,31,31,31,31,31,31,31,31,0,
0,0,0,31,31,32,0,0,0,0,0,0,0,0,0,0,0,
34,35,35,35,36,36,37,0,0,0,0,0,0,0,0,
0,33,33,0,0,35,35,35,35,35,35,35,37,0,
0
},
{0,34,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,37,0,0,0,0,0,34,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,0,
0,0,0,35,35,37,0,30,31,32,0,0,0,0,0,0,
0,0,36,36,36,0,0,0,0,0,0,0,0,0,0,31,
31,31,31,31,31,31,31,32,36,36,36,36,0,
0,0
},
{0,0,34,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,37,0,0,0,30,32,0,0,34,35,
35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,
35,31,31,31,31,35,37,0,0,35,35,35,32,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,35,35,35,35,35,35,35,35,37,0,0,
0,0,0,0,0
},
{0,0,0,34,36,36,36,35,35,35,36,36,36,
36,36,36,37,0,0,0,0,36,36,0,0,0,34,
35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,37,0,0,0,35,35,35,35,
31,32,0,0,0,0,38,39,39,39,39,40,0,0,
0,0,0,0,0,33,35,35,35,35,35,35,35,37,
0,0,0,0,0,0,0,0
},
{0,0,0,0,0,0,0,36,36,36,0,0,0,0,0,0,0,
33,0,0,0,0,0,0,0,0,0,34,36,34,35,35,
35,35,35,35,35,35,35,35,35,35,35,35,
35,35,35,35,35,35,35,35,35,35,35,37,
0,0,0,0,34,35,35,35,35,35,31,31,31,
31,0,0,0,0,0,0,31,31,31,31,31,31,31,
31,32,36,35,35,36,36,36,0,0,0,0,0,0,
0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,30,
31,32,0,0,0,0,0,0,0,0,0,0,34,36,36,36,
36,36,36,36,36,36,36,36,36,36,36,36,
36,36,36,36,36,36,36,36,37,0,0,0,0,0,
0,34,35,35,35,35,35,35,35,35,0,0,0,0,
0,0,34,35,35,35,35,35,35,35,37,0,36,
36,0,0,0,0,0,0,0,0,0,0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,36,
36,36,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,36,36,35,35,35,35,35,
36,0,0,0,0,0,0,0,34,35,35,35,35,
35,37,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,30,31,31,32,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,34,36,36,36,
37,0,0,0,0,0,0,0,0,0,36,36,36,36,
36,0,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,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0
}
};
TileMap Class:
class TileMap
{
public int width, height;
PImage[][] tiles;
TileMap(int x, int y)
{
this.width = x;
this.height = y;
tiles = new PImage[y][x];
}
int getWidth()
{
return width;
}
int getHeight()
{
return height;
}
void setTile(PImage image, int x, int y)
{
tiles[x][y] = image;
}
PImage getTile(int x, int y)
{
if(x < 0 || x > width || y < 0 || y > height)
{
println("Tile doesn't exist");
return null;
}
else
{
return tiles[y][x];
}
}
}
Player Class (a Mandrill):
class Mandrill
{
float xPos;
float yPos;
PImage mandrill;
PImage mandrill1;
int frame;
float gravity=-9.5;
Mandrill(float x,float y)
{
xPos=x;
yPos=y;
mandrill=loadImage("mandrill_walk0.gif");
mandrill1=loadImage("mandrill_walk_left0.gif");
}
void drawRight(float x,float y)
{
image(mandrill,x, y);
}
void drawLeft(float x,float y)
{
image(mandrill1,x,y);
}
void setXPos(float x)
{
xPos=x;
}
void setYPos(float y)
{
yPos=y;
}
float getXPos()
{
return xPos;
}
float getYPos()
{
return yPos;
}
void walkRight(float x,float y)
{
PImage[] images= new PImage[4];
for (int i = 0; i < 4; i++)
{
String filename = "mandrill_walk" + i + ".gif";
images[i] = loadImage(filename);
}
frameRate(8);
frame = (frame+1) % 4;
image(images[frame], x, y);
}
void walkLeft(float x, float y)
{
PImage[] images= new PImage[4];
for (int i = 0; i < 4; i++)
{
String filename = "mandrill_walk_left" + i + ".gif";
images[i] = loadImage(filename);
}
frameRate(8);
frame = (frame+1) % 4;
image(images[frame], x, y);
}
void jumpRight(float x, float y)
{
PImage[] images= new PImage[6];
for (int i = 0; i < 6; i++)
{
String filename = "mandrill_jump" + i + ".gif";
images[i] = loadImage(filename);
}
frameRate(8);
frame=(frame+1) % 4;
image(images[frame], x, y);
}
void jumpLeft(float x, float y)
{
PImage[] images= new PImage[6];
for (int i = 0; i < 6; i++)
{
String filename = "mandrill_jump_left" + i + ".gif";
images[i] = loadImage(filename);
}
frameRate(5);
frame = (frame+1) % 4;
image(images[frame], x, y);
}
}
1