按下时未能尝试更改图像

时间:2014-02-09 20:41:58

标签: java animation keyevent

我正试图这样做当我按下右键时弹出一张新照片让它看起来像我的角色正在行走,不太确定该怎么做但是......这是我的代码:

import java.awt.*;


public class Dude {
    int x, dx, y;
    Image still;

    public Dude() {
            ImageIcon i = new ImageIcon("Ken3.png");
            ImageIcon ii = new ImageIcon("KenTurn1.png");
            still = i.getImage();
            x = 50;
            y = 785;
    }


    public void move() {
            x = x + dx;
    }

    public int getX() {
            return x;
    }

    public int getY() {
            return y;
    }

    public Image getImage() {
            return still;
    }

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

            if (key == KeyEvent.VK_D) 
                    dx = 2;

            if (key == KeyEvent.VK_SPACE)
                dx = 5;

    }

    public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();

            if (key == KeyEvent.VK_A)
                    dx = 0;

            if (key == KeyEvent.VK_D)
                    dx = 0;

任何帮助都会非常棒!谢谢!

2 个答案:

答案 0 :(得分:0)

这样的事情,我对此发表了评论。祝你好运

public class Dude {

    private int walkingIndex;
    private Image walkingFrames;
    private Image still;

    private String state;

    public Dude() {
        // say were starting off standing
        state = "standing";
        // init our still frame
        still = (new ImageIcon("Ken3.png")).getImage();

        // set our walking frame to 0 to start
        walkingIndex = 0;
        walkingFrames = new Image[3]; // or however many images you have in your walking animation
        // go through each frame and initialize the image to KenWalking{index}.png
        for(int i=0;i<walkingFrames.length;i++) {
            walkingFrames[i] = (new ImageIcon("KenWalking"+i+".png")).getImage();
        }
    }

    public void move() {
        x+=dx;
        // add 1 to the walking index and make sure its not greater than our number of walking frames, % means get the remainder of
        // so 12 % 10 = 2, and 8 % 7 = 1
        walkingIndex = (walkingIndex+1)%walkingFrames.length;
    }

    public Image getImage() {
        // if our state is walking then give out a walking frame
        if(state.equals("walking")) {
            return walkingFrames[walkingIndex];
        } else {
            // otherwise we can assume were standing, so show that image instead
            return still;
        }
    }

    public void keyPressed(KeyEvent e) {
        ... same stuff
        state = "walking";
    }

    public void keyReleased(KeyEvent e) {
        ... same stuff
        state = "standing";
    }
}

答案 1 :(得分:0)

如果我理解正确的问题,那么您正在寻找一个keyListener。在这种情况下,你可以做两件事。添加键绑定或添加KeyListener。 请记住,我没有在您的模板上测试代码,它在AWT框架中适合我。希望它有所帮助

首先将EventListener实现为Dude

public class Dude implements KeyListener{ ...

要使KeyListener起作用,您必须拥有一个可聚焦的组件..

    component.addKeyListener(this);
    component.setFocusable(true);
    component.setFocusTraversalKeysEnabled(false);

以下方法将使用KeyListener

实现
@Override
public void keyPressed(KeyEvent e) {
    int x = e.getKeyCode();

    switch(x){
        case KeyEvent.VK_UP:
            // Do Your stuff
            break;
        case KeyEvent.VK_DOWN:
            // Do Your stuff
            break;
        case KeyEvent.VK_RIGHT:
            // Do Your stuff
            break;
        case KeyEvent.VK_LEFT:
            // Do Your stuff
            break;
    }
}