- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我有一个Java swing应用程序,其中有一艘宇宙飞船获取旋转角度,然后沿该方向加速(在其轴上旋转并移动到 scapy 所在的位置)。我遇到的麻烦是当船指向与之前方向相反的方向时让船减速。
What Happens and what I need to fix
如果你看一下图像,你会发现当我尝试通过完全相反的方向转向来补偿我的速度时,速度会增加,而我需要做的是有办法降低速度,如果船舶正在补偿其先前的速度。
示例1:
import java.util.Set;
/**
* Created by griffin on 12/7/2015.
*/
public class Example1 extends JPanel {
public enum Input {
ROTATE_LEFT,
ROTATE_RIGHT,
UP,
DOWN
}
private final int B_WIDTH = 640;
private final int B_HEIGHT = 480;
private Set<Input> inputs;
Ship player = new Ship(320, 240);
public Example1() {
initExample1();
}
private void initExample1() {
inputs = new HashSet<>(25);
setFocusable(true);
setBackground(Color.BLACK);
setPreferredSize(new Dimension(B_WIDTH, B_HEIGHT));
addKeyBinding("rotate-left", KeyEvent.VK_A, Input.ROTATE_LEFT);
addKeyBinding("rotate-right", KeyEvent.VK_D, Input.ROTATE_RIGHT);
addKeyBinding("up", KeyEvent.VK_W, Input.UP);
addKeyBinding("down", KeyEvent.VK_S, Input.DOWN);
Timer timer = new Timer(40, new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
if (inputs.contains(Input.ROTATE_LEFT)) {
player.angle -= 5;
}
if (inputs.contains(Input.UP)) {
player.thrust = true;
player.moveForwards();
}
else
player.moveForwards();
if (inputs.contains(Input.ROTATE_RIGHT)) {
player.angle += 5;
}
player.checkAngle();
player.screenWrap();
repaint();
}
});
timer.start();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.WHITE);
g2d.drawString("Speed " + (int) player.speed, 15, 15);
AffineTransform old = AffineTransform.getTranslateInstance(player.x, player.y);
old.rotate(Math.toRadians(player.angle), player.getWidth() / 2, player.getHeight() / 2);
g2d.setTransform(old);
g2d.drawImage(player.ship, 0, 0, this);
Toolkit.getDefaultToolkit().sync();
}
private void addKeyBinding(String name, int keyCode, Input input) {
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
inputMap.put(KeyStroke.getKeyStroke(keyCode, 0, false), name + ".pressed");
actionMap.put(name + ".pressed", new InputAction(input, true));
inputMap.put(KeyStroke.getKeyStroke(keyCode, 0, true), name + ".released");
actionMap.put(name + ".released", new InputAction(input, false));
}
private class InputAction extends AbstractAction {
private Input input;
private boolean pressed;
public InputAction(Input input, boolean pressed) {
this.input = input;
this.pressed = pressed;
}
@Override
public void actionPerformed(ActionEvent e) {
if (pressed) {
inputs.add(input);
} else {
inputs.remove(input);
}
}
}
}
发货:
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
/**
* Created by griffin on 12/7/2015.
*/
public class Ship {
float directionX, directionY;
boolean thrust = false;
int x, y;
float speed = 1;
int angle = 0, vAngle = 0;
BufferedImage ship;
private String path = "rocketc.png";
public Ship(int x, int y) {
this.x = x;
this.y = y;
getImage();
}
private void getImage() {
try {
ship = ImageIO.read(new File(path));
} catch (Exception e) {
e.printStackTrace();
}
}
public int getWidth() {
return ship.getWidth();
}
public int getHeight() {
return ship.getHeight();
}
public void moveForwards() {
directionX = (float) (Math.cos(Math.toRadians(vAngle))) * speed;
directionY = (float) (Math.sin(Math.toRadians(vAngle))) * speed;
if (thrust) {
speed++;
vAngle = angle;
thrust = false;
}
x -= directionX;
y -= directionY;
}
public void checkAngle () {
if (angle > 360) {
angle = 0;
}
if (angle < 0) {
angle = 360;
}
}
public void screenWrap () {
if (x > 640) {
x = 0;
}
if (x < 0) {
x = 640;
}
if (y > 480) {
y = 0;
}
if (y < 0) {
y = 480;
}
}
}
最佳答案
您现在可能已经明白了这一点,但是当您检查向上键是否被按下时,您没有将 player.thrust
设置回 false
.
if (inputs.contains(Input.UP)) {
player.thrust = true;
player.moveForwards();
}
else{
player.thrust = false; // added this line
player.moveForwards();
}
此外,如果您想要拖动(即未按下向上键时减速),则可以在 Ship.moveForwards()
中减小速度
:
if (thrust) {
speed++;
vAngle = angle;
thrust = false;
}
else{ // added this block
speed--;
}
您可以通过不同的值来增加/减少速度
以获得不同的加速度/阻力。
关于Java "Asteroids"喜欢游戏 - 放慢船速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34215164/
是否可以减慢 setAttribute() 的速度?例如,我有以下代码: function hide(i) { var previewDiv = document.getElementById
我有一个动画,我一直试图放慢速度,但到目前为止我一直没有成功,我尝试了持续时间并在最后添加时间,但动画似乎以相同的速度运行。 任何帮助都会很棒。 $("document").ready(functio
我已经将 Redis 与 Laravel 连接起来用于排队电子邮件,一切都很好......但在开发环境中,我使用 mailtrap.io(免费版)。 问题是 mailtrap 每秒只允许接收 2 封电
我正在尝试使用 canvas 制作一些东西,我可以在其中传递一个数字,该数字等于某个度数 0-360,并且一条线将从其当前位置到我设置的度数的任何位置进行动画处理。 现在,我的路线可以达到我想要的任何
我是一名优秀的程序员,十分优秀!