【问题标题】:Can't get my image to move using KeyListner无法使用 KeyListener 移动我的图像
【发布时间】:2016-05-25 01:50:55
【问题描述】:

我正在制作一个口袋妖怪游戏,这个类只是应该将地图绘制到屏幕上并根据箭头键上检测到的击键移动角色,但 keyPressed 方法没有检测到击键和我的角色不会动。我想不通。

import java.awt.*;
import java.applet.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Math.*;
import java.util.*;
import java.util.ArrayList;
import java.awt.Image.*;
import java.awt.event.KeyEvent;

public class map extends JApplet implements KeyListener
{
Image grass;
Image Sgrass;
Image sand;
Image CharDown, CharUp, CharLeft, CharRight;
Image finalboss;
Image drawn;
Image[][] gr = new Image[13][15];
public int X;
public int Y;
javax.swing.Timer t;
double ttt = 0;
double B = 0;
public void init()
{
    addKeyListener (this);
    grass = getImage(getCodeBase(),"Grass.jpg");
    Sgrass = getImage(getCodeBase(),"Non-Grass.jpg");
    sand = getImage(getCodeBase(),"Sand.jpg");
    finalboss = getImage(getCodeBase(), "PLeft.jpg");
    CharDown = getImage(getCodeBase(), "CharDown.gif");
    CharUp = getImage(getCodeBase(), "CharUp.gif");
    CharLeft = getImage(getCodeBase(), "CharLeft.gif");
    CharRight = getImage(getCodeBase(), "CharRight.gif");
    drawn = CharDown;
    grid();
}
public void grid()
{
    for(int i = 0; i<13; i++)
    {
        for(int j = 0; j<15; j++)
        {
            double x = Math.random();
            if(x<=0.70)
            {
                gr[i][j] = grass;
            }
            else if(x>0.70 && x<=0.90)
            {
                gr[i][j] = Sgrass;
            }
            else if (x>0.90)
            {
                gr[i][j] = sand;
            }
        }
    }
}  
public void paint(Graphics g)
{
    int a = 0;
    int b = 0;
    for(int z = 0; z<13; z++)
    {
        for(int w = 0; w<15; w++)
        {
            g.drawImage(gr[z][w],a,b,50,50,this);
            a+=50;            
            if (a > 750)
            break;
        }
        b+=50;
        a = 0;
        if (b > 650)
        break;
    }
    g.drawImage(drawn, X, Y, 50, 50, this);
    g.drawImage(finalboss, 700, 600, 50, 50, this);
}
public void keyPressed(KeyEvent e)
{
    JOptionPane.showMessageDialog(null, "Key was pressed");
    int key = e.getKeyCode();
    if (key == 37)
    {
        //left
        X=X-50;
        drawn = CharLeft;
    }
    else if (key == 39)
    {
        //right
        X=X+50;
        drawn = CharRight;
    }
    else if (key == 38)
    {
        //up
        Y=Y-50;
        drawn = CharUp;
    }
    else if (key == 40)
    {
        //down
        Y=Y+50;
        drawn = CharDown;
    }
    repaint();
}
public void keyTyped(KeyEvent e) { }
public void keyReleased(KeyEvent e) { }
}

【问题讨论】:

  • 拍摄我没有意识到这是多么难以理解
  • 程序也会编译并绘制地图、角色和最终boss,但该方法不会检测到任何击键。

标签: java image swing keylistener


【解决方案1】:

最可能的问题是您的 JApplet 没有焦点,因此事件被传递到不同的组件。您可以尝试一些解决方法。首先,您可以尝试将setFocusable(true); 行添加到您的JAppletinit() 方法中。这不是最强大的解决方案,但我过去曾成功使用过它。或者,您可以按照this question 中的建议使用 KeyBindings。

【讨论】:

    猜你喜欢
    • 2013-06-06
    • 1970-01-01
    • 2018-10-05
    • 2012-10-22
    • 2014-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    相关资源
    最近更新 更多