【问题标题】:Why doesn't the ball move around in my applet when I press a button?为什么当我按下按钮时小球在我的小程序中不移动?
【发布时间】:2015-01-05 07:35:27
【问题描述】:

我对 Java 和一般编程非常陌生,所以我尝试编写一个简单的程序,它唯一的工作就是让球用箭头键移动并用空格键跳跃。程序编译了,但球不动。

我可能没有完全理解 Applet 的原理,你能解释一下我犯的错误吗?

小程序:

package test_game;

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class Starting_Point extends Applet implements Runnable, KeyListener {

    private static final long serialVersionUID = 1L;
    private static final int WIDTH = 700;
    private static final int HEIGHT = 300;
    Character Phil;
    Thread thread;

    @Override
    public void init() {
        setSize(WIDTH,HEIGHT);
        Phil = new Character(50, 200);
    }

    @Override
    public void start() {
        thread = new Thread(this);
        thread.start();
    }

    public void run() {
        while (true) {
            repaint();  
        }
    }

    @Override
    public void paint(Graphics arg0) {
        Phil.paint(arg0);
    }

    @Override
    public void keyPressed(KeyEvent arg0) {
        switch (arg0.getKeyCode()) {
            case (KeyEvent.VK_LEFT): Phil.setX(Phil.getX() - Phil.getDx());
                break;
            case (KeyEvent.VK_RIGHT): Phil.setX(Phil.getX() + Phil.getDx());
                break;
            case (KeyEvent.VK_SPACE): Phil.setDy(Character.gravity * Character.dt);
                Phil.setY(Phil.getY() + .5*Character.gravity*Character.dt*Character.dt + Phil.getDy()*Character.dt + Character.jump);
                break;
        }
    }

    @Override
    public void keyReleased(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void keyTyped(KeyEvent arg0) {
        // TODO Auto-generated method stub
    }
}

字符类:

package test_game;

import java.awt.Color;
import java.awt.Graphics;

public class Character {

    private int x;
    private int y;
    private int radius = 10;
    Graphics g;
    public static final int gravity = 15;
    private static final double dx = 4;
    public double dy;
    public static final double dt = .2;
    public static final int jump = 30;

    public Character(int x, int y) {
        this.x = x;
        this.y = y;
    }

    public void setX(double d) {
        this.x = (int) d;
    }

    public void setY(double d) {
        this.y = (int) d;
    }

    public double getDx() {
        return dx;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public void setDy(double dy) {
        this.dy = dy;
    }

    public double getDy() {
        return dy;
    }

    public void paint(Graphics g) {
        g.setColor(Color.black);
        g.fillOval(x, y, radius, radius);   
    }
}

【问题讨论】:

  • “我试着写一个简单的程序..” 小程序从来都不是简单的。先做相框。 1) 为什么要编写小程序?如果是老师指定的,请参考Why CS teachers should stop teaching Java applets。 2) 为什么使用 AWT?请参阅 this answer 了解放弃 AWT 使用支持 Swing 的组件的许多充分理由。

标签: java applet awt keylistener


【解决方案1】:

您在代码中定义了KeyListener。但是,您忘记将此事件侦听器注册到组件,因此它从未被调用。

如何注册您的 KeyListener:

将以下代码放入您的start()

this.addKeyListener(this);

这解决了角色向左或向右移动的问题,但是跳跃不是固定的,修复跳跃需要更多代码来计算您需要的正确 y+,当前代码总是加 0(四舍五入为0 使用您使用的 (int)) 到 y 坐标,当您按空格键时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-21
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 1970-01-01
    相关资源
    最近更新 更多