2014-11-02 64 views
-1

我试图创建一个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(); 

} 

}

我如何动画放缓。图像变为快速。没有延迟

+1

'“......结果是一次失败的尝试。” - 你没有向我们展示过(?)。 – 2014-11-02 02:02:52

+1

您需要知道动画的整个帧速率和循环中的当前帧,例如,如果渲染速度为25fps,则每个动画循环需要显示精灵表中的每帧8.3帧。 – MadProgrammer 2014-11-02 02:34:59

+0

每2个像素就会移动到它增加帧的方向。我可以加载图像并将其显示在Jpanel上。我无法做的是加载一组图像,并在屏幕上每移动2像素,每帧显示一个图像。我也不想要像一串图像一样的线索。我将不得不处置并重新绘制。我无法将代码放在一起。如果有人写了一个工作示例,这将是很好的。 – 2014-11-02 03:23:38

回答

0

我想出如何通过使用定时器的增量是有单独的定时器,在保存所有的图形调用的JPanel重绘帧动画放缓。

在这个课程中,你将最终得到一个来回移动的精灵。我甚至初始化了碰撞检测的边界框。

public class NPC { 
    private int x; 
    private int y; 
    private int dx; 
    private int dy; 

    Image npc_img; 
    Image[] npc_l; 
    Image[] npc_r; 
    Image[] npc_d; 
    Image[] npc_u; 

    Rectangle boundBox; 

    int current_frame = 0; 
    int distance = 0; 

    Timer t = new Timer(300, new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 
      current_frame++; 
      if(current_frame == 4)current_frame = 0; 

      initLeft(current_frame); 

      if(dx == -1 && dy == 0)initLeft(current_frame); 
      if(dx == 1 && dy == 0)initRight(current_frame); 
      if(dy == -1 && dx == 0)initUp(current_frame); 
      if(dy == 1 && dx == 0)initDown(current_frame); 

      distance++; 
      if(distance <= 5){ 
       dx = -1; 
       dy = 0; 
      } 
      if(distance >= 5){ 
       dx = 1; 
       dy = 0; 
      } 
      if(distance == 9)distance = 0; 

     } 
    }); 

    public int getX(){return x;} 
    public int getY(){return y;} 

    public void move(){ 
     x += dx; 
     y += dy; 
     boundBox = new Rectangle(x, y, 60, 60); 
    } 

    public Image getImage(){return npc_img;} 

    public void initLeft(int frame){ 
     npc_l = new Image[4]; 
     npc_l[0] = new ImageIcon("C:\\imgs\\zion\\npc_l1.png").getImage(); 
     npc_l[1] = new ImageIcon("C:\\imgs\\zion\\npc_l2.png").getImage(); 
     npc_l[2] = new ImageIcon("C:\\imgs\\zion\\npc_l3.png").getImage(); 
     npc_l[3] = new ImageIcon("C:\\imgs\\zion\\npc_l4.png").getImage(); 
     npc_img = npc_l[frame]; 
    } 
    public void initRight(int frame){ 
     npc_r = new Image[4]; 
     npc_r[0] = new ImageIcon("C:\\imgs\\zion\\npc_r1.png").getImage(); 
     npc_r[1] = new ImageIcon("C:\\imgs\\zion\\npc_r2.png").getImage(); 
     npc_r[2] = new ImageIcon("C:\\imgs\\zion\\npc_r3.png").getImage(); 
     npc_r[3] = new ImageIcon("C:\\imgs\\zion\\npc_r4.png").getImage(); 
     npc_img = npc_r[frame]; 
    }public void initDown(int frame){ 
     npc_d = new Image[4]; 
     npc_d[0] = new ImageIcon("C:\\imgs\\zion\\npc_d1.png").getImage(); 
     npc_d[1] = new ImageIcon("C:\\imgs\\zion\\npc_d2.png").getImage(); 
     npc_d[2] = new ImageIcon("C:\\imgs\\zion\\npc_d3.png").getImage(); 
     npc_d[3] = new ImageIcon("C:\\imgs\\zion\\npc_d4.png").getImage(); 
     npc_img = npc_d[frame]; 
    } 
    public void initUp(int frame){ 
     npc_u = new Image[4]; 
     npc_u[0] = new ImageIcon("C:\\imgs\\zion\\npc_u1.png").getImage(); 
     npc_u[1] = new ImageIcon("C:\\imgs\\zion\\npc_u2.png").getImage(); 
     npc_u[2] = new ImageIcon("C:\\imgs\\zion\\npc_u3.png").getImage(); 
     npc_u[3] = new ImageIcon("C:\\imgs\\zion\\npc_u4.png").getImage(); 
     npc_img = npc_u[frame]; 
    } 

    public void interact(Graphics g){ 
     g.setColor(Color.red); 
     g.drawString("Greetings", (x - 20), (y - 10)); 
    } 

    public NPC(){ 
     x = 250; 
     y = 250; 
     t.start(); 
    } 
} 

在JPanel Paint内部被调用。

public void paint(Graphics g){ 
     super.paint(g); 

     Graphics2D g2d = (Graphics2D)g; 
     g2d.drawImage(sophia.getImage(), sophia.getX(), sophia.getY(), this); 
     g2d.drawImage(npc.getImage(), npc.getX(), npc.getY(), this); 
     if(sophia.boundBox.intersects(npc.boundBox))npc.interact(g); 

     Toolkit.getDefaultToolkit().sync(); 
     g.dispose(); 
    } 
0

更新了Sophia类。哪个是玩家。

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; 

    Rectangle boundBox; 

    public Sophia(){ 
     x = 40; 
     y = 60; 

    } 

    public void move(){ 
     x += dx; 
     y += dy; 
     boundBox = new Rectangle(x, y, 62, 62); 
     t.start(); 
    } 

    public int getX(){ 
     return x; 
    } 

    public int getY(){ 
     return y; 
    } 

    public Image getImage(){ 
     return img; 
    } 

    int current_frame = 0; 
    Timer t = new Timer(150, new ActionListener(){ 
     public void actionPerformed(ActionEvent e){ 

      current_frame++; 
      if(current_frame == 4)current_frame = 0; 
     } 
    }); 


    public void keyPressed(KeyEvent e){ 
     int key = e.getKeyCode(); 
     if(key == KeyEvent.VK_LEFT){ 
      dx = -1; 

      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[current_frame]; 
     } 
     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[current_frame]; 

     } 
     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[current_frame]; 
     } 
     if(key == KeyEvent.VK_DOWN){ 
      dy = 1; 

      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[current_frame]; 
     } 

    } 


    public void keyReleased(KeyEvent e){ 
     int key = e.getKeyCode(); 
     if(key == KeyEvent.VK_LEFT){ 
      dx = 0; 

     } 
     if(key == KeyEvent.VK_RIGHT){ 
      dx = 0; 

     } 
     if(key == KeyEvent.VK_UP){ 
      dy = 0; 

     } 
     if(key == KeyEvent.VK_DOWN){ 
      dy = 0; 

     } 
    } 
}