Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中移动GObject_Java - Fatal编程技术网

在Java中移动GObject

在Java中移动GObject,java,Java,大家早上好,我是Java新手,所以我希望你们能帮助我。这就是我面临的问题。顺便说一句,我应该使用ASM图形 那是我的Gobject课程 package objects; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import acm.graphics.*; public class Mario extends GCompou

大家早上好,我是Java新手,所以我希望你们能帮助我。这就是我面临的问题。顺便说一句,我应该使用ASM图形

那是我的Gobject课程

package objects;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import acm.graphics.*;

public class Mario extends GCompound implements ActionListener{
public static double dx;
public static double dy;
public static GImage mario;
private static int x = 40;
private static int y = 350;
private final int min = 350;
private final int gravity = 5;
private final int speed = 5;
private final int max_jump = min - 220;
private int direction;
private int number = 1;
public static boolean standing = true;

public Mario () {
    if (standing == true) {
        mario = new GImage("Mario_Big_Right_Still.png");
        mario.setSize(60, 105);
    }
    else {
        mario = new GImage("Mario_Big_Crouch_Left.png");
        mario.setSize(60, 95);
    }
    mario.setLocation(x + dx, y + dy);
    add(mario);
}

public boolean onBottom() {
        if (mario.getLocation().getY() >= min ) return true;
    return false;
}

public void keyPressed(KeyEvent e) {

    int key = e.getKeyCode();

    if (key == KeyEvent.VK_LEFT) {
            dx = -5;
    }
    if (key == KeyEvent.VK_RIGHT) {
            dx = 5;
    }
    if (key == KeyEvent.VK_UP) {
            dy = -5;
    }
    if (key == KeyEvent.VK_DOWN) {
            standing = false;
    }
}

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 = 5;
    }

    if (key == KeyEvent.VK_DOWN) {
            standing = true;
    }
}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}
}
这是申请文件

import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import acm.graphics.*;
import acm.program.*;
import objects.Mario;

public class Application extends GraphicsProgram implements ActionListener {
private static final long serialVersionUID = 1L;

public static final int WINDOW_WiDTH = 800;
public static final int WINDOW_HEIGHT = 600;
public static final int REFRESH = 60;
public int dx;
public int dy;

public void run () {
    setSize(WINDOW_WiDTH, WINDOW_HEIGHT);
    addKeyListeners();
    while (true) {
        Mario mario = new Mario();
        add(mario);
        mario.setLocation(mario.getLocation().getX() + dx, mario.getLocation().getY() + dy);
        pause(REFRESH);
    }
}
}

此外,我还需要能够更改图像。欢迎提出任何建议。提前谢谢。

现在还不清楚你在这个问题上向我们提出了什么要求……我看到了代码,看到了一些与你所做的相关的句子,但我不确定你具体在问什么。你面临的问题在哪里?@Makoto我请求帮助我创建一个动作动画。我面临的问题是,当我单击箭头时,什么也没有发生,程序没有看到我按下键。@f1sh问题是我无法让我的键盘侦听器工作。addKeylisteners();也不管用