Java tower defense game

New Video: http://www.youtube.com/watch?v=Ymww6vpgndA

Put up the new video showing the towers firing, I’m gonna try and get the towers to rotate with the line in order to get 3D feel.

It’s looking good, great progress!

One comment: "public class ad … " Really?! :o
If you want to obfuscate your code, use a tool like Proguard. Don’t do it manually! :point: :stuck_out_tongue:

haha just laziness. But thanks for checking it out! I’m trying to get the image to follow the line right now.

Or that’s really Ad class, for showing ad before you purchase the game :stuck_out_tongue:

i really cant figure out how to make the tower rotate toward enemies, anyone have any ideas?

All you need is the rotation angle of the tower when it faces the enemy:


double rot = Math.atan2(enemy.y-tower.y,enemy.x-tower.x);

// then when rendering:

g.rotate(rot,tower.x+tower.width/2,tower.y+tower.height/2);

Ok so here is my code and its getting an error,


//Tower
		double rot;
		for(int i = 0; i < tower.length; i++){
			rot = Math.atan2(mobs[i].y - tower[i].y, mobs[i].x - tower[i].y);
			if(towerID[i] != air) {
				g.drawImage(aa.f.bp.tower[towerID[i]], tower[i].x, tower[i].y, tower[i].width, tower[i].height, null);
				g.rotate(rot, tower[i].x + tower.length / 2, tower[i].y + tower.height / 2);
		}
		}






you should be rotating before drawing the image then you need to reset the transform:


g.rotate...
g.drawImage...
g.getTransform().setToIdentity();

ok so i have some rotation but its very random and all over the place, and when i place a second tower it rotates around the first one. Anyone see why?




//Tower
				double rot;
				Graphics2D g2d = (Graphics2D) g;
				for(int i = 0; i < tower.length; i++){
					
					rot = Math.atan2(mobs[i].y - tower[i].y, mobs[i].x - tower[i].y);
					if(towerID[i] != air) {
						g2d.rotate(rot, tower[i].x + tower[i].width / 2, tower[i].y + tower[i].height / 2);
						g.drawImage(aa.f.bp.tower[towerID[i]], tower[i].x, tower[i].y, tower[i].width, tower[i].height, null);
				}
				}




You need to reset the transform like I showed you in the previous post! :stuck_out_tongue:

That doesn’t help its something wrong with my calculations. Its not rotating toward the enemies its more just a random spinning and then the second tower placed down spins randomly around the first so on an so on. I can post the whole class if you want to take a look otherwise ill keep trying.

Sure, but your problem sounds like you’re still not resetting the transform :slight_smile:

alright so here is the whole class let me know if you see anything, thanks for checking it out!



hidden


The only relevant code was paintComponent(Graphics g) :expressionless:

You reset the transform before you draw the image when, as I pointed out before, you’re supposed to reset after drawing each image:


Graphics2D g2d = (Graphics2D) g;
for(int i = 0; i < tower.length; i++){
    double rot = Math.atan2(mobs[i].y - tower[i].y, mobs[i].x - tower[i].y);
    g2d.rotate(rot, tower[i].x + tower[i].width / 2, tower[i].y + tower[i].height / 2);
    if(towerID[i] != air) {
        g2d.drawImage(aa.f.bp.tower[towerID[i]], tower[i].x, tower[i].y, tower[i].width, tower[i].height, null);
        g2d.getTransform().setToIdentity();
    }
}

This is because the transform is applied to the AffineTransform. AffineTransform stores a 3x3 matrix that, through magic (and math), transforms drawing commands. The call to rotate(…) sets a rotation transform and so all future drawing commands are rotated accordingly. Resetting the AffineTransform to the identity matrix is like setting a multiplier to 1. All inputs will equal the output.

So I kinda gave up on the rotation for now, I’ll come back to it, its not working no matter what I do. But i posted a new screen shot of stuff I did today. Check it out there will be a new video up later. Thanks!

that’s not counted as logic part, angle calculation can be added later. Just do another stuff :slight_smile:

You can better NOT follow tutorials in my opinion. You should try to make it from scratch. Much better that way. This way you will learn how you can start a project, what is needed, and how it should work (draw it out on paper, helps!). And just google as much as you can, this is the way im making my towerdefence. I’ve just started with java 1-2 weeks as well, but at least i know exactly what im doing, and thus can fine tune it much more, and make it much more to my liking.

Also dont just give up when people help you so much. These are the moments where you build personality. You find a problem, solve it. Dont dodge it!
how ive done it:
first i calculate the angle, using the coordinates where my arrow has to go to (This should be your enemy’s coordinates) which i give in my constructor:

	    this.angle = Math.atan2(toY-Y_Position, toX-X_Position);  

Then in my draw function:

	public void draw(Graphics g){
		Graphics2D g2d = (Graphics2D)g;
		g2d.rotate(angle-Math.toDegrees(90), getX_Position(), getY_Position());
		g2d.drawImage(image,getX_Position(),getY_Position(),null);
		g2d.rotate(-angle+Math.toDegrees(90), getX_Position(), getY_Position());
		
	}

i do the -90 because of the way my image is turned (its looking up) and then i invoke the function g2d.rotate one more time so everything else in my game draws using the right rotation.