OK so this may be a long a complicated problem, I have made a Boomerang class (yes it’s that guy) that determines the boomerangs start point by getting the X,Y position of the player and then modifying these co-ords so the boomerang will appear from the centre of the player.
First problem: It doesn’t, it appears significantly higher and to the left of the player.
The class then creates the path of the boomerang and stores it in a point array and every time the sprite is updated moves it to the next point in the array.
Second problem: Although it does move in a straight, diagonal line, it moves vertically down from the start point, even though the path is calculated by adding X,Y positions to the last “node” of the path.
I will post the class and see if you can make head or tails of where the problem is coming from, part of me thinks that it may be using points to store the X,Y co-ords, as these use Java’s co-ordinate system and points aren’t used throughout the rest of the code. I wanted someone else’s opinion before I rewrite the class or the rest of the engine to use or not to use points.
Here’s the class:
public class Boomerang extends Sprite
{
private static double DURATION = 0.5; // secs
// total time to cycle through all the images
private int period; // in ms; the game's animation period
private BricksManager brickMan;
private int speed; //used to calculate how many nodes to create along the
//boomerangs path.
protected int locx, locy;
private Boolean isVisible;
private JumperSprite rat;
private Point startPoint = new Point();
private Point nextPoint = new Point();
private int nodeLength;
private int nodes = 0;
private Point[] boomPath;
public Boomerang (int x, int y, BricksManager bm, ImagesLoader imsLd, int p, String name, JumperSprite js)
{
super(x, y, 30, 32, imsLd, "Boomerang");
locx = x;
locy = y;
super.setImage(name);
speed = 3;
period = p;
isVisible = false;
rat = js;
boomPath = new Point[20];
}
public void fire(){
int x;
int y;
int ratx = rat.getXPosn();
int raty = rat.getYPosn();
System.out.println("ratx:" + ratx);
System.out.println("raty:" + raty);
Point endp = new Point();
int midx = rat.getWidth()/2; //get the mid point of rat' sprite
//System.out.println("midx:" + midx);
int midy = rat.getHeight()/2;
//System.out.println("midy:" + midy);
startPoint.setLocation((ratx + midx), (raty - midy));
//set the start of boomPath to the mid
//positions.
//System.out.print("startPoint original" +startPoint);
boomPath[0] = startPoint;
PointerInfo MousePosn = MouseInfo.getPointerInfo();
endp = MousePosn.getLocation();
nodeLength = (int) (startPoint.distance(endp)/(20-speed));
//System.out.println();
//System.out.println("nodeLength:" + nodeLength);
//cut the distance into equal segments to increment boomPath
for(int i = 0; i<(20- speed); i++){ //add points to boomPoint using nodeLength increments
//System.out.println();
//System.out.println("boomPath[0].x:" + boomPath[0].x);
//System.out.println();
//System.out.println(i + 1);
nextPoint.setLocation((boomPath[i].x - nodeLength), (boomPath[i].y - nodeLength));
boomPath[i+1] = nextPoint;
//System.out.println();
//System.out.println("boompath[" + i + "]:" + boomPath[i]);
} //end of for loop
isVisible = true;
}//end of fire()
public boolean isVisible(){
return this.isVisible;
}
public void update() {
if(isVisible){
if(nodes < boomPath.length){
locx = boomPath[nodes].x;
//System.out.println("boomPath[" + nodes + "]" + "locx:" + locx);
locy = boomPath[nodes].y;
//System.out.println("boomPath[" + nodes + "]" + "locx:" + locy);
nodes ++;
}
}
}//end of update()
}//end of Boomerang
Cheers for any help you can give me