我试图创建一个sprite类,每个数组中包含3个帧,表示每个方向。这个类将实现一个actionlistener和keylistener,所以当我按下W时,这个类将动画向前移动的sprite绘制图像框架,给出它正在行走的错觉。Java - 如何从图像数组中依次绘制图像
我研究了关于这个问题在网上几个主题,结果却是失败的尝试。
我必须初始化当前位置intergers和速度显示它在屏幕上移动的速度有多快。我需要一个计时器类来运行动画。
我就一定得矩形范围,所以我可以碰撞之间计算交叉点。我正在学习游戏开发,并学习了计算机编程的基础知识。我仍然是一个java编程,并且只编译了一些解决我的问题的尝试。
这里是图像加载的地方。依赖于KeyEvent,它改变.getImage()递增current_frame整数。
public class Sophia {
private int dx;
private int dy;
private int x;
private int y;
private Image img;
private Image[] sophia_down;
private Image[] sophia_left;
private Image[] sophia_right;
private Image[] sophia_up;
public Sophia(){
x = 40;
y = 60;
}
public void move(){
x += dx;
y += dy;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public Image getImage(){
return img;
}
int current_frame = 0;
public int tick(){
if(current_frame == 4)current_frame =0;
return current_frame++;
}
public void keyPressed(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT){
dx = -1;
ImageIcon ii = new ImageIcon("C:\\imgs\\zion\\sophia_l1.png");
sophia_left = new Image[4];
sophia_left[0] = new ImageIcon("C:\\imgs\\zion\\sophia_l1.png").getImage();
sophia_left[1] = new ImageIcon("C:\\imgs\\zion\\sophia_l2.png").getImage();
sophia_left[2] = new ImageIcon("C:\\imgs\\zion\\sophia_l3.png").getImage();
sophia_left[3] = new ImageIcon("C:\\imgs\\zion\\sophia_l4.png").getImage();
img = sophia_left[tick()];
}
if(key == KeyEvent.VK_RIGHT){
dx = 1;
sophia_right = new Image[4];
sophia_right[0] = new ImageIcon("C:\\imgs\\zion\\sophia_r1.png").getImage();
sophia_right[1] = new ImageIcon("C:\\imgs\\zion\\sophia_r2.png").getImage();
sophia_right[2] = new ImageIcon("C:\\imgs\\zion\\sophia_r3.png").getImage();
sophia_right[3] = new ImageIcon("C:\\imgs\\zion\\sophia_r4.png").getImage();
img = sophia_right[tick()];
}
if(key == KeyEvent.VK_UP){
dy = -1;
sophia_up = new Image[4];
sophia_up[0] = new ImageIcon("C:\\imgs\\zion\\sophia_u1.png").getImage();
sophia_up[1] = new ImageIcon("C:\\imgs\\zion\\sophia_u2.png").getImage();
sophia_up[2] = new ImageIcon("C:\\imgs\\zion\\sophia_u3.png").getImage();
sophia_up[3] = new ImageIcon("C:\\imgs\\zion\\sophia_u4.png").getImage();
img = sophia_up[tick()];
}
if(key == KeyEvent.VK_DOWN){
dy = 1;
ImageIcon ii = new ImageIcon("C:\\imgs\\zion\\sophia_d1.png");
sophia_down = new Image[4];
sophia_down[0] = new ImageIcon("C:\\imgs\\zion\\sophia_d1.png").getImage();
sophia_down[1] = new ImageIcon("C:\\imgs\\zion\\sophia_d2.png").getImage();
sophia_down[2] = new ImageIcon("C:\\imgs\\zion\\sophia_d3.png").getImage();
sophia_down[3] = new ImageIcon("C:\\imgs\\zion\\sophia_d4.png").getImage();
img = sophia_down[tick()];
}
}
public void keyReleased(KeyEvent e){
int key = e.getKeyCode();
if(key == KeyEvent.VK_LEFT){
dx = 0;
current_frame = 0;
}
if(key == KeyEvent.VK_RIGHT){
dx = 0;
current_frame = 0;
}
if(key == KeyEvent.VK_UP){
dy = 0;
current_frame = 0;
}
if(key == KeyEvent.VK_DOWN){
dy = 0;
current_frame = 0;
}
}
}
这里是用图像被描绘。
public class Map extends JPanel implements ActionListener {
Timer t;
Sophia sophia;
public Map(){
addKeyListener(new TAdapter());
setFocusable(true);
setBackground(Color.black);
setDoubleBuffered(true);
sophia = new Sophia();
npc = new NPC();
t = new Timer(5, this);
t.start();
}
public void paint(Graphics g){
super.paint(g);
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(sophia.getImage(), sophia.getX(), sophia.getY(), this);
Toolkit.getDefaultToolkit().sync();
g.dispose();
}
public void actionPerformed(ActionEvent e){
sophia.move();
repaint();
}
private class TAdapter extends KeyAdapter {
public void keyReleased(KeyEvent e){
sophia.keyReleased(e);
}
public void keyPressed(KeyEvent e){
sophia.keyPressed(e);
}
}
}
这里是主类
public class Zion extends JFrame {
int screenWidth = 640;
int screenHeight = 480;
public Zion(){
Map m = new Map();
add(m);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(600,600);
setLocationRelativeTo(null);
setTitle("Zion - Version: 0.3");
setResizable(false);
setVisible(true);
}
public static void main(String[] args) {
new Zion();
}
}
我如何动画放缓。图像变为快速。没有延迟
'“......结果是一次失败的尝试。” - 你没有向我们展示过(?)。 – 2014-11-02 02:02:52
您需要知道动画的整个帧速率和循环中的当前帧,例如,如果渲染速度为25fps,则每个动画循环需要显示精灵表中的每帧8.3帧。 – MadProgrammer 2014-11-02 02:34:59
每2个像素就会移动到它增加帧的方向。我可以加载图像并将其显示在Jpanel上。我无法做的是加载一组图像,并在屏幕上每移动2像素,每帧显示一个图像。我也不想要像一串图像一样的线索。我将不得不处置并重新绘制。我无法将代码放在一起。如果有人写了一个工作示例,这将是很好的。 – 2014-11-02 03:23:38