简体   繁体   中英

make player shoot bullets to the mouse pointer coordinates

I have this class Bullet and I want to shoot a bullet from my hero position to the mouse cursor position.

Here's the Bullet class:

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);
    }

}

Now if I click the left button the bullet is moving to the right but only if I keep the click pressed. How to make the ball (bullet) move smoothly after one click? And also, now it starts from the position where I create this bullet. How to make the start position from the hero position?

This is the hero class

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);
    }
}

I tried to make a function in Bullet class but I need to access the coordinates of the player and I don't know how to access them.

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

Here in this function I need to subtract the coordinates of my player but I don't know how to acces them. I want to use this function in tick() method so I can't pass a Player argument to the function because the I can't call it.

How to proceed?

First, if you want to shoot a Bullet with the ShootBullet method, you should pass the Player as an argument in the method so that you can access it.

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

Second, if you want your bullet to move continually, just add a boolean variable, let's say inAir in the Bullet class, and when you tick() check for all the bullets that have inAir set to true . Those that are should tick().


Third, if you want your bullet to move in the direction of the cursor (I assume here that the bullet direction will always have the same angle and the bullet won't curve), you simply use trigonometry and store an angle variable in your Bullet class.

Your deltaX will be equal to bulletSpeed * cos(angle) (angle in radians here) and your deltaY will be equal to bulletSpeed * sin(angle) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM