简体   繁体   English

创建一个“子弹”类(Java / Swing)

[英]Creating a “bullet” class (Java/swing)

I'm trying to create a Tower Defense style game, where the object is to stop the attacking forces from reaching their target. 我正在尝试创建一个塔防风格的游戏,目的是阻止攻击部队到达目标。 This is done by building towers, which attacks the waves of enemies with different kinds of attacks. 这是通过建造塔楼来完成的,塔楼以各种攻击方式攻击敌人的海浪。 As I am new to programming, I was hoping for some help with creating my bullet's SetBulletLocation method. 刚接触编程时,我希望在创建项目符号的SetBulletLocation方法方面获得帮助。

I've been trying to copy: this example , but I can't make my bullet move smoothly towards the target location 我一直在尝试复制: 此示例但是我无法使子弹平稳地移向目标位置

public class Bullets extends JComponent{
//x,y = the towers coordinates, where the shoot initiliazes from.
//tx, ty = Target's x and y coordinates.
private int x,y,tx,ty;
private Point objectP = new Point();
    public Bullets(int x, int y, int tx, int ty)
        this.x = x;
        this.y = y;
        this.tx = tx;
        this.ty = ty;
        setBounds(x,y,50,50);
        //Create actionlistener to updateposition of the bullet (setLocation of component)
        ActionListener animate = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent ae) {

            setBulletLocation();


        }
        };
        Timer t = new Timer(500,animate);
        t.start();

    }
public void setBulletLocation() {
    objectP = this.getLocation();
    double xDirection =  5* Math.cos(-(Math.atan2(tx - x, tx - y))+ 90);
    double yDirection =  5* Math.sin(-(Math.atan2(tx - x, tx - y))+ 90);
    System.out.println(xDirection + " , " + yDirection);
    x = (objectP.x + (int) xDirection);
    y = (objectP.y + (int) yDirection);
    setLocation(x, y);

    repaint();
 }
@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.fillOval(0, 0, 50, 50);
}

All the Java math trig functions take radians for angular arguments, not degrees. 所有Java数学trig函数均采用弧度表示角度参数,而不是度数。 Try Math.PI/2 instead of 90 in: 在以下位置尝试使用Math.PI / 2而不是90:

double xDirection = 5* Math.cos(-(Math.atan2(tx - x, tx - y))+ 90); double xDirection = 5 * Math.cos(-(Math.atan2(tx-x,tx-y))+ 90);

double yDirection = 5* Math.sin(-(Math.atan2(tx - x, tx - y))+ 90); double yDirection = 5 * Math.sin(-(Math.atan2(tx-x,tx-y))+ 90);

I noticed there is error in calculating displacement 我注意到计算位移时有错误

fragment: 分段:

Math.atan2(tx - x, tx - y))

didn't you mean ?: 你不是说吗?

Math.atan2(tx - x, ty - y))

Your paintComponent() seems to be painting the bullet in the same location and size every time regardless of your calculations. 无论您进行何种计算,每次您的paintComponent()似乎都在相同的位置和大小上绘制项目符号。

Store your new x and new y values into member variables and use those in paintComponent. 将新的x和y值存储到成员变量中,并在paintComponent中使用它们。

Also - the Java's trig functions use radians, not degrees so update your references to 90 degrees with pi / 2. 另外-Java的trig函数使用弧度而不是度数,因此请使用pi / 2将引用更新为90度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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