Java键盘事件方向键按下移动大小累加怎么消除
[code=java]packagemyGame;importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;...
[code=java]
package myGame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FightFace extends JFrame {
private ImageIcon myPlaner = new ImageIcon("src/myGame/image/玩家飞机小.png");
private static int x=160,y=550; // 飞机的坐标
final int moveSize = 1;// 按键一次移动的距离
Image offScreenImage = null;
@Override
public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(30,24);
}
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.GRAY);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(myPlaner.getImage(), x, y, this);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e){
super.keyPressed(e);
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_DOWN:
FightFace.y += moveSize;
repaint();break;
case KeyEvent.VK_UP:
FightFace.y -= moveSize;
repaint();break;
case KeyEvent.VK_LEFT:
FightFace.x -= moveSize;
repaint();break;
case KeyEvent.VK_RIGHT:
FightFace.x += moveSize;
repaint();break;
default: break;
}
}
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
}
});
}
public static void main(String[] args) {
FightFace frame = new FightFace();
frame.setTitle("MyGame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
[/code]
这里按键之后,每次移动的距离会累积起来,而不是每次都移动moveSize大小,为什么?求大神告诉怎么解决。 展开
package myGame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FightFace extends JFrame {
private ImageIcon myPlaner = new ImageIcon("src/myGame/image/玩家飞机小.png");
private static int x=160,y=550; // 飞机的坐标
final int moveSize = 1;// 按键一次移动的距离
Image offScreenImage = null;
@Override
public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(30,24);
}
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.GRAY);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(myPlaner.getImage(), x, y, this);
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e){
super.keyPressed(e);
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_DOWN:
FightFace.y += moveSize;
repaint();break;
case KeyEvent.VK_UP:
FightFace.y -= moveSize;
repaint();break;
case KeyEvent.VK_LEFT:
FightFace.x -= moveSize;
repaint();break;
case KeyEvent.VK_RIGHT:
FightFace.x += moveSize;
repaint();break;
default: break;
}
}
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
}
});
}
public static void main(String[] args) {
FightFace frame = new FightFace();
frame.setTitle("MyGame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
[/code]
这里按键之后,每次移动的距离会累积起来,而不是每次都移动moveSize大小,为什么?求大神告诉怎么解决。 展开
展开全部
你的代码我看了好久 。。。。
debug时发现他在不停的重新调用KeyTyped()方法。
后来才知道问题在于 你把添加adapter的逻辑写到 paint()方法里面了
其实你每repaint()的时候都会重新调用paint()方法 导致 你每按一次键盘就会多一个adapter()导致你生成了好多adapter而且变量还是static几个Adapter都能改变他的值所以就越变越多!!
我在FightFace类里添加了一个构造函数在那里添加了Adapter()
那样的话没生成一个FightFace对象就生成一个Adapter 就没有问题了
你先把原来那个this.addKeyListener(); 删掉写在构造函数里
代码如下
package myGame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FightFace extends JFrame {
private ImageIcon myPlaner = new ImageIcon("src/myGame/image/dddd.jpg");
private static int x=160,y=550; // 飞机的坐标
final int moveSize = 1;// 按键一次移动的距离
Image offScreenImage = null;
FightFace(){
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e){
super.keyPressed(e);
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_DOWN:
FightFace.y = FightFace.y + moveSize;
repaint();break;
case KeyEvent.VK_UP:
FightFace.y = FightFace.y - moveSize;
repaint();break;
case KeyEvent.VK_LEFT:
FightFace.x = FightFace.x -moveSize;
repaint();break;
case KeyEvent.VK_RIGHT:
FightFace.x = FightFace.x + moveSize;
repaint();break;
default: break;
}
}
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
}
});
}
@Override
public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(30,24);
}
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.GRAY);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(myPlaner.getImage(), x, y, this);
// System.out.println("paint");
// this.addKeyListener(new KeyAdapter() {
//
// @Override
// public void keyTyped(KeyEvent e){
// super.keyTyped(e);
//
// System.out.println("KeyTyped");
// int key = e.getKeyCode();
// switch(key){
// case KeyEvent.VK_DOWN:
// FightFace.y = FightFace.y + moveSize;
// repaint();break;
// case KeyEvent.VK_UP:
// FightFace.y = FightFace.y - moveSize;
// repaint();break;
// case KeyEvent.VK_LEFT:
// FightFace.x = FightFace.x -moveSize;
// repaint();break;
// case KeyEvent.VK_RIGHT:
// FightFace.x = FightFace.x + moveSize;
// repaint();break;
// default: break;
// }
// }
// @Override
// public void keyPressed(KeyEvent e){
// super.keyPressed(e);
// System.out.println("KeyPressed");
// int key = e.getKeyCode();
// switch(key){
// case KeyEvent.VK_DOWN:
// FightFace.y = FightFace.y + moveSize;
// repaint();break;
// case KeyEvent.VK_UP:
// FightFace.y = FightFace.y - moveSize;
// repaint();break;
// case KeyEvent.VK_LEFT:
// FightFace.x = FightFace.x -moveSize;
// repaint();break;
// case KeyEvent.VK_RIGHT:
// FightFace.x = FightFace.x + moveSize;
// repaint();break;
// default: break;
// }
// }
// @Override
// public void keyReleased(KeyEvent e) {
// super.keyReleased(e);
//
// System.out.println("KeyPressed");
// }
//
// });
}
public static void main(String[] args) {
FightFace frame = new FightFace();
frame.setTitle("MyGame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
debug时发现他在不停的重新调用KeyTyped()方法。
后来才知道问题在于 你把添加adapter的逻辑写到 paint()方法里面了
其实你每repaint()的时候都会重新调用paint()方法 导致 你每按一次键盘就会多一个adapter()导致你生成了好多adapter而且变量还是static几个Adapter都能改变他的值所以就越变越多!!
我在FightFace类里添加了一个构造函数在那里添加了Adapter()
那样的话没生成一个FightFace对象就生成一个Adapter 就没有问题了
你先把原来那个this.addKeyListener(); 删掉写在构造函数里
代码如下
package myGame;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class FightFace extends JFrame {
private ImageIcon myPlaner = new ImageIcon("src/myGame/image/dddd.jpg");
private static int x=160,y=550; // 飞机的坐标
final int moveSize = 1;// 按键一次移动的距离
Image offScreenImage = null;
FightFace(){
this.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e){
super.keyPressed(e);
int key = e.getKeyCode();
switch(key){
case KeyEvent.VK_DOWN:
FightFace.y = FightFace.y + moveSize;
repaint();break;
case KeyEvent.VK_UP:
FightFace.y = FightFace.y - moveSize;
repaint();break;
case KeyEvent.VK_LEFT:
FightFace.x = FightFace.x -moveSize;
repaint();break;
case KeyEvent.VK_RIGHT:
FightFace.x = FightFace.x + moveSize;
repaint();break;
default: break;
}
}
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
}
});
}
@Override
public void update(Graphics g) {
if(offScreenImage == null) {
offScreenImage = this.createImage(30,24);
}
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.GRAY);
g.fillRect(0, 0, getWidth(), getHeight());
g.drawImage(myPlaner.getImage(), x, y, this);
// System.out.println("paint");
// this.addKeyListener(new KeyAdapter() {
//
// @Override
// public void keyTyped(KeyEvent e){
// super.keyTyped(e);
//
// System.out.println("KeyTyped");
// int key = e.getKeyCode();
// switch(key){
// case KeyEvent.VK_DOWN:
// FightFace.y = FightFace.y + moveSize;
// repaint();break;
// case KeyEvent.VK_UP:
// FightFace.y = FightFace.y - moveSize;
// repaint();break;
// case KeyEvent.VK_LEFT:
// FightFace.x = FightFace.x -moveSize;
// repaint();break;
// case KeyEvent.VK_RIGHT:
// FightFace.x = FightFace.x + moveSize;
// repaint();break;
// default: break;
// }
// }
// @Override
// public void keyPressed(KeyEvent e){
// super.keyPressed(e);
// System.out.println("KeyPressed");
// int key = e.getKeyCode();
// switch(key){
// case KeyEvent.VK_DOWN:
// FightFace.y = FightFace.y + moveSize;
// repaint();break;
// case KeyEvent.VK_UP:
// FightFace.y = FightFace.y - moveSize;
// repaint();break;
// case KeyEvent.VK_LEFT:
// FightFace.x = FightFace.x -moveSize;
// repaint();break;
// case KeyEvent.VK_RIGHT:
// FightFace.x = FightFace.x + moveSize;
// repaint();break;
// default: break;
// }
// }
// @Override
// public void keyReleased(KeyEvent e) {
// super.keyReleased(e);
//
// System.out.println("KeyPressed");
// }
//
// });
}
public static void main(String[] args) {
FightFace frame = new FightFace();
frame.setTitle("MyGame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 600);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
2014-01-27
展开全部
case KeyEvent.VK_DOWN:
FightFace.y += moveSize;
repaint();break;
case KeyEvent.VK_UP:
FightFace.y -= moveSize;
repaint();break;
case KeyEvent.VK_LEFT:
FightFace.x -= moveSize;
repaint();break;
case KeyEvent.VK_RIGHT:
FightFace.x += moveSize;
repaint();break;
default: break;
我觉得应该是+=的问题,这不就是累加么?
FightFace.y += moveSize;
repaint();break;
case KeyEvent.VK_UP:
FightFace.y -= moveSize;
repaint();break;
case KeyEvent.VK_LEFT:
FightFace.x -= moveSize;
repaint();break;
case KeyEvent.VK_RIGHT:
FightFace.x += moveSize;
repaint();break;
default: break;
我觉得应该是+=的问题,这不就是累加么?
追问
a += 1; 就是 a = a + 1;
我需要的是a触动一次就加1,现在的问题是a每次触动后加的数不是1,而触动第一次是加1,第二次是加2,第三次加3。。。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
分支语句里,repaint()怎么重复那么多?你直接用数字试过没,不用变量计算步数
追问
试过了
追答
我改好了,你的事件处理写在paint方法了,我放在构造函数就一点没问题
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询