首页 > 解决方案 > 让玩家向鼠标指针坐标射击子弹

问题描述

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

这是子弹类:

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 参数传递给函数,因为我不能调用它。

如何进行?

标签: javaclass2d

解决方案


首先,如果你想BulletShootBullet方法拍摄 a,你应该在方法中作为参数传递,Player以便你可以访问它。

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

其次,如果你想让你的子弹连续移动,只需添加一个boolean变量,比如说inAirBullet类中,当你 tick() 检查所有inAir设置为true. 那些应该打勾()。


第三,如果你想让你的子弹沿着光标的方向移动(我在这里假设子弹的方向总是有相同的角度并且子弹不会弯曲),你只需使用三角函数并在你的类中存储一个angle变量。Bullet

您的deltaX意志等于bulletSpeed * cos(angle)(此处以弧度表示的角度)并且您的deltaY意志等于bulletSpeed * sin(angle)


推荐阅读