Hi, I am pretty sure there is a more effective way of achieving this. Would love for some input from you guys.
Basically, I have a class called GamePanel and it contains this:
SpaceShip spaceship = new SpaceShip("Galatar");
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.drawImage(bgI,0,0,null);
g2d.drawImage(spaceship.setSpaceShipSprite(""),sShipX,sShipY,null);
}
//KeyPressed method
if(e.getKeyCode()==KeyEvent.VK_LEFT){
sShipX = spaceship.move_left(sShipX);
spaceship.setSpaceShipSprite("left");
if(e.getKeyCode()==KeyEvent.VK_RIGHT){
sShipX = spaceship.move_right(sShipX);
spaceship.setSpaceShipSprite("right");
}
getSpaceShipSprite() and setSpaceShipSprite() call methods in my SpaceShip class as follows:
public Image setSpaceShipSprite(String i)
{
if(i.equals("right")){
img = new ImageIcon("src\\resources\\spaceShipR.png").getImage();
}
else if(i.equals("left")){
img = new ImageIcon("src\\resources\\spaceShipL.png").getImage();
}
else
return img;
return img;
}
I don't know why, but this seems like a sloppy way to do things, I could be over thinking this which is one of my main problems(haha) but any input would be appreciated.
EDIT: the getSpaceShipSprite() method is used once to get a starting image