使玩家向鼠标指针坐标发射子弹



我有这个类Bullet,我想从我的英雄位置向鼠标光标位置发射一颗子弹。

这是子弹级:

public class Bullet extends Entity {

private float bulletSpeed = 1.2f;
private float dx, dy;
public Bullet(Handler handler, float x, float y, int width, int height) {
super(handler, x, y, width, height);
}
@Override
public void tick() {
if (handler.getMouseManager().isLeftPressed()) {
x += bulletSpeed;
}
}
@Override
public void render(Graphics g) {
g.setColor(Color.RED);
g.fillOval((int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height);
}
}

现在,如果我单击左边的按钮,项目符号会向右移动,但前提是我一直按下按钮。如何使球(子弹(在一次点击后顺利移动?而且,现在它从我创建这个子弹的位置开始。如何从英雄位置开始定位?

这是英雄级

package BunnyFights.Entities.Creatures;
import BunnyFights.Game;
import BunnyFights.Handler;
import BunnyFights.gfx.Assets;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Player extends Creature {
private BufferedImage image;
public Player(Handler handler, float x, float y) {
super(handler, x, y, Creature.DEFAULT_CREATURE_WIDTH, Creature.DEFAULT_CREATURE_HEIGHT);
image = Assets.heroLeft;
bounds.x = 16;
bounds.y = 32;
bounds.width = 32;
bounds.height = 32;
}
@Override
public void tick() {
getInput();
move();
handler.getGameCamera().centerOnEntity(this);
if(handler.getKeyManager().left == true)
{
image = Assets.heroLeft;
}
if(handler.getKeyManager().right == true) {
image = Assets.heroRight;
}
}
public void getInput() {
xMove = 0;
yMove = 0;
if (handler.getKeyManager().up) {
yMove = -speed;
}
if (handler.getKeyManager().down) {
yMove = speed;
}
if (handler.getKeyManager().left) {
xMove = -speed;
}
if (handler.getKeyManager().right) {
xMove = speed;
}
}
@Override
public void render(Graphics g) {
g.drawImage(image, (int)(x - handler.getGameCamera().getxOffset()), (int)(y - handler.getGameCamera().getyOffset()), width, height, null);
// g.setColor(Color.red);
// g.fillRect((int)(x + bounds.x - handler.getGameCamera().getxOffset()),
//       (int)(y + bounds.y - handler.getGameCamera().getyOffset()),
//       bounds.width, bounds.height);
}
}

我试图在Bullet类中创建一个函数,但我需要访问玩家的坐标,我不知道如何访问它们。

public void ShootBullet()
{
double bulletVelocity = 1.0; //however fast you want your bullet to travel
//mouseX/Y = current x/y location of the mouse
//originX/Y = x/y location of where the bullet is being shot from
double angle = Math.atan2(handler.getMouseManager().getMouseX() - player.getX(), handler.getMouseManager().getMouseY() - player.getY());
double xVelocity = (bulletVelocity) * Math.cos(angle);
double yVelocity = (bulletVelocity) * Math.sin(angle);
x += xVelocity;
y += yVelocity;
}

在这个函数中,我需要减去我的玩家的坐标,但我不知道如何加速它们。我想在tick((方法中使用这个函数,这样我就不能将Player参数传递给函数,因为我不能调用它。

如何继续?

首先,如果你想用ShootBullet方法拍摄Bullet,你应该在方法中传递Player作为参数,这样你就可以访问它。

public void ShootBullet(Player player){
// Perform calculations and everything.
// The player is accessible here.
}

其次,如果您希望项目符号持续移动,只需添加一个boolean变量,比如Bullet类中的inAir,当您tick((时,检查所有将inAir设置为true的项目符号。那些是的应该tick((。


第三,如果您希望项目符号沿光标的方向移动(我在这里假设项目符号的方向将始终具有相同的角度,并且项目符号不会弯曲(,您只需使用三角法并在Bullet类中存储一个angle变量。

您的deltaX将等于bulletSpeed * cos(angle)(此处的角度以弧度为单位(,而deltaY将等于bulletSpeed * sin(angle)

最新更新