Im creating a side scroller that i need help with. I want the bullets to shoot towards the mouse. However, the current code I have only allows for shooting diagonally:
[quote]Bullet.Java:
[/quote]
public class Bullet extends Entity{
public Bullet(int x, int y, int type) {
super(x, y);
this.type = type;
Image();
}
public void update() {
y += velY;
x += velX;
if (type == 0) { currentImage = lead_Bullet;}
if (type == 1) { currentImage = laser_Bullet;}
}
public void draw(Graphics2D g2d) {
g2d.drawImage(currentImage, x, y, null);
if (Game.debugMode) { g2d.draw(getBounds()); }
}
public int velX;
public int velY;
public int type=0;
public Image currentImage;
// Characters
private Image lead_Bullet;
private Image laser_Bullet;
public void Image() {
ImageIcon q1 = new ImageIcon(this.getClass().getResource("/misc/lead bullet.png"));
lead_Bullet = q1.getImage();
ImageIcon q2 = new ImageIcon(this.getClass().getResource("/mobs/player old 1/player_right_old1.png"));
laser_Bullet = q2.getImage();
}
public Rectangle getBounds() {
return new Rectangle (x,y,9,9);
}
}
[quote]Shooting Code:
[/quote]
public static void shoot() {shot = true;}
public void BulletUpdate() {
if (shot==true) {
shotTimer++;
Bullet tempBullet = new Bullet(Game.player.x, Game.player.y, 0);
Bullets.add(tempBullet);
if (Object_Control.msx > x) {
tempBullet.velX=Environment.speed;
}
if (Object_Control.msx < x) {
tempBullet.velX=-Environment.speed;
}
if (Object_Control.msy > y) {
tempBullet.velY=Environment.speed;
}
if (Object_Control.msy < y) {
tempBullet.velY=-Environment.speed;
}
System.out.print(shot);
}
if (shotTimer > 1) {
shot = false;
shotTimer = 0;
}
}