This site requires JavaScript, please enable it in your browser!
Greenfoot back
askgriff
askgriff wrote ...

2013/10/26

Having a Turret Follow A Tank

1
2
askgriff askgriff

2013/10/26

#
Okay, so I am working on a tank game and I want my turret (a different class) to follow my tank. I would like to have my Turret class follow my Tank class with something like a getX() and getY() and getRotation() kind of thing. Right now I just apply the same "if key is pressed" function with both classes, but they sometimes get out of sync. Here's my sample code: http://www.greenfoot.org/scenarios/9727. I would appreciate any help. I can bypass this and have the tank and turret be fixed to each other -- but I would kind of like to move my tank around and rotate the turret separately. And now that I think of it, I'll need to do the getX(), get(Y), and getRotation() to launch a bullet as well. Maybe this is beyond my skills at this point? LOL I'm teaching a brand new programming class at our high school and I'm wanting it to be JAVA -- so I am doing my best to stay a week or two ahead of the students at this point. I can use ANY and all help. :) Thanks everybody!
Kartoffelbrot Kartoffelbrot

2013/10/26

#
You have to save the tank (body) object in the turret or otherwise around. Here is an example for the Tank: private Turret turret; public Turret(Turret turret){ this.turret = turret; } public void addedToWorld(World world){ world.addObject(turret, getX(), getY()); } public void follow(){ turret.setLocation(getX(), getY()); turret.setRotation(getRotation());//optional }
danpost danpost

2013/10/26

#
Each turret object you create will 'belong' to a specific tank object. Therefore, you should probably have an instance field in the turret class to hold which tank each instance of the turret class belongs to. In the following code, I added control for the rotation of the turret using the 'left' and 'right' arrow keys. An instance field is used to keep track of the current rotation of the turret with respect to the tank.
// in Turret1 class, add instance fields
private Tank1 tank; // the tank the turret belongs to
private int rotation; // the rotation of turret on tank
// change constructor to this (the argument ('tank') is the tank object this turret object belongs to)
public Turret1(Tank tank)
{
    this.tank = tank; // saves the tank object this turret object belongs to
}
// with methods
public void act()
{
    checkKeys(); // checks for changes in rotation of turret on tank
    setLocation(tank.getX(), tank.getY()); // keeping up with the tank
    setRotation(tank.getRotation()+rotation); // combines rotations
}

public void checkKeys()
{
    if (Greenfoot.isKeyDown("left")) rotation = (rotation+359)%360;
    if (Greenfoot.isKeyDown("right")) rotation = (rotation+1)%360;
}
Your 'prepare' method in the world class should be like this:
public void prepare()
{
    Tank1 tank = new Tank1(); // creates a new tank object (and sets it to local field)
    addObject(tank, 100, 310); // adds tank to world
    addObject(new Turret1(tank), 100, 310); // adds new turret to world (passing the tank object it belongs to in the constructor call)
}
askgriff askgriff

2013/10/26

#
So I add that to the Tank class? And don't place the Turret in the World?
danpost danpost

2013/10/26

#
Line 5 in part 1 of my previously posted code should be
public Turret1(Tank1 tank)
askgriff askgriff

2013/10/26

#
I'm looking at yours, too Danpost. Thanks. I'll let you guys know if I have the brains to figure this out. LOL
danpost danpost

2013/10/26

#
I believe that in the example that Kartoffelbrot gave, the first 2 lines should have been:
private Turret1 turret; // (for the instance field)

public Tank1(Turret1 turret) // (for the constructor declaration statement)
Also, there was a lack of mention that the 'checkKeys' method should be removed from that 'Tank1' class for that example.
Kartoffelbrot Kartoffelbrot

2013/10/26

#
Yes danpost, you are right. I didn't saw that.
askgriff askgriff

2013/10/27

#
Has this overcomlicated the classes to make it more difficult to have bullets? I want the bullets to come out at the same rotation as the turrets (obviously)... but is there a simple way to do that? Also, is the "rotation" you added to turret preferable to the "turn" method? Should I change my tank to rotate like that as well? I've noticed that I can turn slightly and I still go in a straight line.
danpost danpost

2013/10/27

#
askgriff wrote...
Has this overcomlicated the classes to make it more difficult to have bullets? I want the bullets to come out at the same rotation as the turrets (obviously)... but is there a simple way to do that? Also, is the "rotation" you added to turret preferable to the "turn" method? Should I change my tank to rotate like that as well? I've noticed that I can turn slightly and I still go in a straight line.
Overcomplicated? Setting the proper rotation for a bullet is no more complicated than before. It would take on the rotation of the turret. 'turn' method? The rotation of the turret must be set to that of the tank plus turned more for its rotation. This could be broken down into two steps (set rotation of tank followed by turn its own rotation), but one statement is preferable. The 'turn' method, in essense, uses the 'setRotation' method within it anyway. Turn slightly and still go straight? Absolutely, If you only move one pixel at a time, you would only be able to move in eight directions, Each increase in the number of pixels moved per act cycle would increase the number of possible directions that the actor can move in by eight.
askgriff askgriff

2013/10/27

#
So I uploaded my scenario game I have been working on (http://www.greenfoot.org/scenarios/9734) which showcases some of the things I have learned from you guys. (I sincerely appreciate the help.) The two problems I am having with the code at this point include: 1.) I seem to have broken my counter classes. I set up the "if someone presses the spacebar" section but can't get the counter to add. 2.) I'm not sure of the best approach to adding the bullets.
danpost danpost

2013/10/27

#
For the bullet, best would be to detect the keystroke and add the bullet from the Turret class (so you can set the rotation of the bullet to the turret):
Bullet bullet = new Bullet();
bullet.setRotation(getRotation());
getWorld().addObject(bullet, getX(), getY());
bullet.move(10);
askgriff askgriff

2013/10/27

#
Thanks danpost. I tried that, but I think I am forgetting something. The bullet just sits there like a mine. LOL Should I put it in the "Act" method instead of the check key area? Sorry -- I'm working and learning as fast as I can. Definitely making progress, though. Also, my mute button only works half way. Not sure what I'm doing wrong but I know it's simply and something stupid that I did. :)
Zamoht Zamoht

2013/10/27

#
You should add move(x); to the bullet's act method. Here x is the amount of pixels (I think) the bullet moves every act. So change x to a number that fits for your game.
askgriff askgriff

2013/10/27

#
Skip the bullet moving thing -- I realized I didn't put a "move(10)" in the Bullet class. :sigh: I hate asking stupid questions.
There are more replies on the next page.
1
2