簡體   English   中英

讓玩家向鼠標指針坐標射擊子彈

[英]make player shoot bullets to the mouse pointer coordinates

我有這個 class 子彈,我想從我的英雄 position 向鼠標 cursor Z4757FE07FD492A3DBE0ZEA6 射擊子彈。

這是子彈 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);
    }

}

現在,如果我單擊左鍵,子彈就會向右移動,但前提是我一直按住單擊。 如何讓小球(子彈)一鍵移動順暢? 而且,現在它從我創建這個子彈的 position 開始。 如何從英雄position開始position?

這是英雄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);
    }
}

我試圖在 Bullet class 中制作 function 但我需要訪問玩家的坐標,但我不知道如何訪問它們。

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

在此 function 中,我需要減去播放器的坐標,但我不知道如何訪問它們。 我想在 tick() 方法中使用這個 function 所以我不能將 Player 參數傳遞給 function 因為我不能調用它。

如何進行?

首先,如果你想用ShootBullet方法發射Bullet ,你應該將Player作為參數傳遞給方法,以便你可以訪問它。

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

其次,如果你想讓你的子彈連續移動,只需添加一個boolean變量,比如說在Bullet inAir中的 inAir ,當你 tick() 時檢查所有inAir設置為true的子彈。 那些應該打勾()。


第三,如果你想讓你的子彈朝着 cursor 的方向移動(我在這里假設子彈方向總是有相同的角度,子彈不會彎曲),你只需使用三角函數並在你的Bullet中存儲一個angle變量class。

您的deltaX將等於bulletSpeed * cos(angle) (此處以弧度表示的角度),您的deltaY將等於bulletSpeed * sin(angle)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM