作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一款自上而下的 2D 游戏,你可以四处走动射击坏人。我希望能够向鼠标射击,无论它朝哪个方向,但我完全不知道该怎么做。
这是我的bullet
类:
public class bullet {
public double x, y,dy,dx,mx,my;
public int dir;
public Rectangle r = new Rectangle((int) x, (int) y, 5, 5);
public bullet(double x, double y) {
this.x = x+10;
this.y = y+10;
this.mx = Comp.mx;
this.my = Comp.my;
r = new Rectangle((int) x, (int) y, 5, 5);
if (x < mx+play.camx) {
dx = 1;
}
if (x > mx+play.camx) {
dx = -1;
}
if (y < my+play.camy) {
dy = 1;
}
if (y > my+play.camy) {
dy = -1;
}
}
public void tick() {
x+=dx;
y+=dy;
r = new Rectangle((int) x - play.camx, (int) y - play.camy, 5, 5);
}
public void render(Graphics g) {
g.setColor(Color.black);
g.fillRect((int) x - play.camx, (int) y - play.camy, 5, 5);
}
}
最佳答案
基本上,您需要计算起点和终点之间的角度,例如......
angle = -Math.toDegrees(Math.atan2(startX - endX, startY - endY)) + 180;
举个例子:
要跟踪鼠标,请使用 MouseListener
和 MouseMotionListerner
看一下:
关于Java向鼠标射击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27554412/
我需要实现射击机制。我需要射弹的连续运动,但我无法对其进行编码。此时,当我按下空格键时,程序就会卡住。我们将不胜感激。 from tkinter import * # creates window w
我遇到了一个问题。如果有任何帮助,我将不胜感激。 我正在尝试从玩家位置射击到鼠标点击位置。代码没有给我任何错误,根据我的逻辑,它应该可以工作,但它没有 它创建了项目符号对象,仅此而已。 //Bulle
我正在用 Java 开发一款 Android 游戏,其中我将有一个 Sprite 跟随用户的手指,并且应该每秒发射一颗子弹。换句话说,我试图附加一个每秒向上移动的位图。位图从主要角色 Sprite 的
我需要在 JQuery 中使用 focusin 和 focusout 触发器。我将它与 Bootstrap Popovers 一起使用。 目前,由于我认为某些奇怪的原因,它不起作用。 这有什么问题吗?
我是一名优秀的程序员,十分优秀!