Java 我的子弹发错地方了

Java 我的子弹发错地方了,java,game-physics,affinetransform,projectile,Java,Game Physics,Affinetransform,Projectile,在我的游戏中,玩家可以使用箭头键旋转。根据玩家的旋转角度,当子弹发射时,它会沿着玩家面对的方向移动 但是,这部分是有效的,子弹是在错误的地方产生的。下面是两张图片,显示了繁殖中的差异。在第一张图片中,子弹在红点上生成。但是,旋转时,圆点不会相应移动,这会阻止子弹正确繁殖 注意:红点仅用于视觉效果(基于子弹的生成坐标) 以下是相关代码: 主要的KeyListener: if (e.getKeyCode() == KeyEvent.VK_SPACE) { //Cr

在我的游戏中,玩家可以使用箭头键旋转。根据玩家的旋转角度,当子弹发射时,它会沿着玩家面对的方向移动

但是,这部分是有效的,子弹是在错误的地方产生的。下面是两张图片,显示了繁殖中的差异。在第一张图片中,子弹在红点上生成。但是,旋转时,圆点不会相应移动,这会阻止子弹正确繁殖

注意:红点仅用于视觉效果(基于子弹的生成坐标)

以下是相关代码:

主要的KeyListener:

if (e.getKeyCode() == KeyEvent.VK_SPACE) {
                //Creates bullets when the spacebar is pressed, as long as the user is not exceeding 3 at a time
                if(player.amountOfBullets() >= 0 && player.amountOfBullets() < 10)
                {
                player.setNB(player.getGunX(), player.getGunY(), player.getAngle());
                }
}

好的,这是你新的绘画移动和旋转方法-你还需要变换枪的点:

public void paint(Graphics g2d){
    // Drawing the rotated image at the required drawing locations
    if(op!=null) g2d.drawImage(op.filter(currentImg, null), (int)x, (int)y, null);
    else g2d.drawImage(currentImg, (int)x, (int)y, null);
    g2d.setColor(Color.blue);
    g2d.fillOval(gunX, gunY, 10, 10);
}


public void move(double xM, double yM){
    x += xM * Math.cos(Math.toRadians(angle));
    y += yM * Math.sin(Math.toRadians(angle));

    gunY += yM * Math.sin(Math.toRadians(angle));
    gunX += xM * Math.cos(Math.toRadians(angle));

    repaint();
}


public void turn(double angle){    
    this.angle += angle;
    double rotationRequired = Math.toRadians (this.angle);
    double locationX = currentImg.getWidth() / 2;
    double locationY = currentImg.getHeight() / 2;
    AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
    op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
    int gx= currentImg.getWidth() - 15;
    int gy=0;
    Point pt1=new Point(gx, gy), pt2=new Point();
    tx.transform(pt1, pt2);
    gunX=(int)x+pt2.x; gunY=(int)y+pt2.y;
    repaint();
}
你需要:

全局变量

AffineTransformOp op;

在Player中。

在任何情况下都不应修改paint或paintComponent方法中数据对象的状态。绘画可以一秒钟开始几次,因为各种各样的原因,而这些原因不在你的控制之下。有时只要移动鼠标就会触发它!“在任何情况下,您都不应修改paint或paintComponent方法中数据对象的状态。”:我想知道,如果您一再提出这些想法,而拒绝解决主要问题,您怎么会不感到尴尬?这是一种免费的解决方案,您的解决方案非常容易理解,而且非常有效。非常感谢你!
public void paint(Graphics g2d){
    // Drawing the rotated image at the required drawing locations
    if(op!=null) g2d.drawImage(op.filter(currentImg, null), (int)x, (int)y, null);
    else g2d.drawImage(currentImg, (int)x, (int)y, null);
    g2d.setColor(Color.blue);
    g2d.fillOval(gunX, gunY, 10, 10);
}


public void move(double xM, double yM){
    x += xM * Math.cos(Math.toRadians(angle));
    y += yM * Math.sin(Math.toRadians(angle));

    gunY += yM * Math.sin(Math.toRadians(angle));
    gunX += xM * Math.cos(Math.toRadians(angle));

    repaint();
}


public void turn(double angle){    
    this.angle += angle;
    double rotationRequired = Math.toRadians (this.angle);
    double locationX = currentImg.getWidth() / 2;
    double locationY = currentImg.getHeight() / 2;
    AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
    op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
    int gx= currentImg.getWidth() - 15;
    int gy=0;
    Point pt1=new Point(gx, gy), pt2=new Point();
    tx.transform(pt1, pt2);
    gunX=(int)x+pt2.x; gunY=(int)y+pt2.y;
    repaint();
}
AffineTransformOp op;