我很难通过按下d
让我的精灵角色执行2个动画帧(link_frame_2后跟link_frame_1)。它只执行一个动画帧(link_frame_2_face_right.png)。
以下是代码:
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;
}
答案 0 :(得分:2)
目前显示的框架在移动时没有更新。如果您使用图像数组并循环浏览,那么您将能够在动画中包含任何数字。我已在下面的代码中提供了更新,因此您可以尝试一下。我添加了一个速度而不是使用初始位置进行移动,因此您可以单独更改这两个值。同样慢速移动可让您更轻松地看到帧变化。
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
}