I am brand new to java game programming (started learning about a week ago). So far, I’ve taken someone else’s Space Invader tutorial and started with a skeleton and modified a few things. Right now what I have is a blank screen with the ship in the center. Aliens spawn randomly and move in a random direction (supposed to be toward the player, but that bug is for another day). I’ve figured out how to take mouse clicks and turn it into a shot in the direction of the mouse click, but the shots that come out are always pointing up. I want to apply AffineTransform to rotate the shot on the angle that its traveling. That is where I get lost. Between the AffineTranform API and any sort of tutorial, I can’t figure out how or where to put any of it.
here’s what I have so far:
package shooter;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.geom.AffineTransform;
public class Sprite {
//create the tranform and image used for the sprite
private AffineTransform transform = new AffineTransform();
private Image image;
private double angle;
//creates the sprite with a scale and angle parameter
public Sprite (Image image, double scale, double angle){
this.angle = angle;
//uses the scale to make the sprite the appropriate size
this.image = image.getScaledInstance((int)(image.getWidth(null) * scale), (int)(image.getHeight(null) * scale), Image.SCALE_DEFAULT);
}
public int getWidth() {
return image.getWidth(null);
}
public int getHeight() {
return image.getHeight(null);
}
public void draw(Graphics g, int x, int y) {
//not sure if this is how you're supposed to do this,
//but I'm trying to move the sprite to the passed x and y
//then rotate it
transform.translate(x, y);
transform.getRotateInstance(Math.toRadians(angle), image.getWidth(null)/2, image.getHeight(null)/2);
g.drawImage(image, x, y, null);
}
}
the transform is never actually applied to the image how i have it here because affineTranform doesn’t have any way to pass and image to it… I’m so lost… ???
This is just the sprite part of another class called Entity which is used to create the player, aliens, and shots. Hopefully this makes enough sense that someone can help me. If you do need anything else, let me know because I want to figure this out…