Hello, i wanted to create a Wii Play Tanks like game. For this, i need a tank which has a body and a turnable turret on top. Sadly my gun barrel is long so that the center of the turret image isnt the desired center for the turning dependent of the mouse icon position. I´ve read some articles on this topic but the solutions didnt really work out sadly. Also the turret is pointing -90 degrees relative to the mouse (if the mouse is on the right hand side of the tank, the barrel points to the top rather than to the right).
Also i cant figure out how to move my tank in its direction (forward and backward) (without turning the image!). Can i somehow have the Turret and Tank in one class with two independent images? Or is this already as clean it can get?
Heres my code:
Thank you in advance!
import greenfoot.*; public class Tank extends Actor { protected int rotation; protected Turret turret; protected TankWorld world; private GreenfootImage tankImage = new GreenfootImage("Tank_Blue_Base_Idle.png"); public Tank() { setImage(tankImage); getImage().scale(40,70); rotation = 0; setRotation(rotation); this.world = (TankWorld) getWorld(); } public TankWorld getTankWorld() { return this.world; } public void act() { if(Greenfoot.isKeyDown("w")) { //Move forward } else if(Greenfoot.isKeyDown("a")) { turn(-1); } else if(Greenfoot.isKeyDown("s")) { //Move backward } else if(Greenfoot.isKeyDown("d")) { turn(1); } } }
import greenfoot.*; public class Turret extends Actor { private Tank tank; private GreenfootImage turretImage = new GreenfootImage("Tank_Blue_Turret_Idle.png"); public Turret(Tank tank) { setImage(turretImage); getImage().scale(30,60); this.tank = tank; } public void act() { setLocation(tank.getX(), tank.getY()); turnTowards(Greenfoot.getMouseInfo().getX(),Greenfoot.getMouseInfo().getY()); } }