Reputation: 1
I am stuck with getting my sprite character to execute 2 animation frames (link_frame_2 followed by link_frame_1) by pressing the d
. It only executes one animation frame(link_frame_2_face_right.png).
Here's the code:
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import acm.graphics.GImage;
import acm.graphics.GPoint;
import acm.program.GraphicsProgram;
public class LinkGame extends GraphicsProgram {
public void run(){
setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
addLink();
addKeyListeners();
addMouseListeners();
}
private void addLink(){
linkCharacter = new GImage("link sprites/link_frame_1_face_right.png");
add(linkCharacter,link_Location_XCoord,link_Location_YCoord);
}
public void keyPressed(KeyEvent e){
char linkMoveRightKey = e.getKeyChar();
if(linkMoveRightKey == 'd'){
// y should not change so goku moves in a straight line
link_Location_YCoord = 0;
linkCharacter.move(link_Location_XCoord,link_Location_YCoord);
set_Link_Anim_Frame_2_face_left();
}
}
public void set_Link_Anim_Frame_2_face_left(){
linkCharacter.setImage("link sprites/link_frame_2_face_right.png");
}
public void set_Link_Anim_Frame_1_face_left(){
linkCharacter.setImage("link sprites/link_frame_1_face_right.png");
}
private GImage linkCharacter;
private int link_Location_XCoord = 50;
private int link_Location_YCoord = 50 ;
private final int APPLICATION_WIDTH = 600;
private final int APPLICATION_HEIGHT = 600;
}
Upvotes: 0
Views: 552
Reputation: 3492
At the moment what frame to show is not being updated as you move. If you use an array of images and loop through then you will be able to have any number in your animation. I have provided the updates into your code below so you can try it out. I added a speed instead of using the initial position for your movement so you can change these two values independently. Also moving slower allows you to see the frames changing easier.
import java.awt.event.KeyEvent;
import acm.graphics.GImage;
import acm.program.GraphicsProgram;
public class LinkGame extends GraphicsProgram {
public void run(){
setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
addLink();
addKeyListeners();
addMouseListeners();
}
private void addLink(){
linkCharacter = new GImage("link sprites/link_frame_1_face_right.png");
add(linkCharacter,link_Location_XCoord,link_Location_YCoord);
}
public void keyPressed(KeyEvent e){
char linkMoveRightKey = e.getKeyChar();
if(linkMoveRightKey == 'd'){
linkCharacter.move(xSpeed,ySpeed);
linkCharacter.setImage(images[frame]);
frame++;
if(frame>=images.length){
frame = 0;
}
}
}
private GImage linkCharacter;
private int link_Location_XCoord = 50;
private int link_Location_YCoord = 50 ;
private final int APPLICATION_WIDTH = 600;
private final int APPLICATION_HEIGHT = 600;
private String[] images = {"link sprites/link_frame_1_face_right.png","link sprites/link_frame_2_face_right.png"}; //Add in as many images as you want for your animation
private int frame = 0;
private int xSpeed = 1; //the number of pixels to move in x
private int ySpeed = 0; //0 so you only move horizontally
}
Upvotes: 2