我试图在每个数组中创建一个包含3个帧的精灵类,表示每个方向。这个类将实现一个actionlistener和keylistener,所以当我按下W时,类会动画精灵向前移动绘制图像框架,给出它正在行走的错觉。
我通过网络研究了一些有关此问题的主题,结果是尝试失败。
我必须初始化当前位置的整数和速度,显示它在屏幕上的移动速度。我需要一个计时器类来运行动画。
我必须获得矩形边界,以便我可以计算碰撞之间的交叉点。我正在学习游戏开发,并且已经学习了计算机编程的基础知识。我仍然是一个amatur 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();
}
}
如何减慢动画速度。图像变快。没有延迟
答案 0 :(得分: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();
}
答案 1 :(得分: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;
}
}
}