我的子弹课是朝错误的方向射击。我究竟做错了什么?

时间:2015-04-30 12:54:42

标签: java mouse physics direction projectile

这是我的子弹课。我一直试图让我的子弹沿着鼠标的方向前进。

但问题是,当我射击子弹时,它不会朝着我想要的方向前进。有时它是正确的,但大多数是约20像素。

public class Bullet extends Entity {

    public Bullet(int x, int y, int width, int height, boolean solid, Id id,
        GameHandler handler,int targetPosX, int targetPosY, int speed) {
        super(x, y, width, height, solid, id, handler);

        this.setTargetPosX(targetPosX);
        this.setTargetPosY(targetPosY);
        this.speed = speed;

        angle = Math.toDegrees(Math.atan2(targetPosY-y, targetPosX-x));     
    }

    @Override
    public void render(Graphics g) {
        g.setColor(Color.BLACK);
        g.fillOval(x-(width/2), y-(height/2), width, height);
    }

    @Override
    public void update() {
        x += (float)(Math.cos(Math.toRadians(angle)))*speed;
        y += (float)(Math.sin(Math.toRadians(angle)))*speed;
    }

    public void moveToTarget(){
    }

    public int getTargetPosX() {
        return targetPosX;
    }

    private void setTargetPosX(int targetPosX) {
        this.targetPosX = targetPosX;
    }

    public int getTargetPosY() {
        return targetPosY;
    }

    private void setTargetPosY(int targetPosY) {
        this.targetPosY = targetPosY;
    }

    public int getSpeed() {
        return speed;
    }
}

这是我的Cursor类:

public class GameCursor extends Entity {

    private LinkedList<Bullet> bulletList = new LinkedList<Bullet>();

private enum State{
    AIM,
    DEFULT,
    RELODING;   
}



public GameCursor(Entity en,GameHandler handler) {

    super(MouseInfo.getPointerInfo().getLocation().x, MouseInfo.getPointerInfo().getLocation().y, 0, 0, false, Id.cursor , handler);

    this.state = State.AIM;
            this.en = en;

}

@Override
public synchronized void  render(Graphics g) {
    switch(this.state){
        case AIM:             
            g.setColor(Color.red);
            g.drawOval(x-15, y-15, 30, 30);
            g.fillRect(x, y, 1, 1);             
            break;
    }

    for(Bullet b: getBulletList()){
        b.render(g);
    }
}

@Override
public synchronized void update() { 


        x = MouseInput.getMousePosX()-(-en.getX() + Frame.WIDTH/2);
        y = MouseInput.getMousePosY()-(-en.getY() + Frame.HEIGHT/2 + 100);


                    for(Bullet b: getBulletList()){
                        b.update();

                    }



}

    public void shoot(){
        Bullet b = new Bullet(en.getX() + (en.width/2) ,en.getY()+ (en.height/2),10,10,true, Id.bullet, handler, getX(), getY(), 12);
        addBullet(b);


    }
}

0 个答案:

没有答案