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

2017/5/25

Condensing multiple classes into one

JeSuisFish JeSuisFish

2017/5/25

#
I have multiple classes where the code is the exact same except the controls. The colors of each tank are green, red, blue, and yellow, but I only pasted the code of the green tank since the code is the same for all the classes. Is there any way to make these all into one class? [Disallowed URL] Tank-
import greenfoot.*;
public class GreenC extends Tank
{
    private double movement;
    private double velocity;
    private int turning;
    private static int health;
    public GreenC()
    {
        movement = 0;
        velocity = 0.96;
        turning = 3;
        health = 2;
    }
    public void act()
    {
        if ( isTouching( Shell.class ) )
        {
            gotHit();
            removeTouching( Shell.class );
        }
        if ( health > 0 )
        {
            movement *= velocity;
            if ( Greenfoot.isKeyDown( "w" ) )
            {
                movement += 0.11;
            }
            if ( Greenfoot.isKeyDown( "s" ) )
            {
                movement -= 0.08;
            }
            if ( Greenfoot.isKeyDown( "d" ) )
            {
                turn( turning );
                if ( isAtEdge() || isTouching( WallV.class ) || isTouching( WallH.class ) || isTouching( Tank.class ) )
                {
                    turn( -turning );
                }
            }
            if ( Greenfoot.isKeyDown( "a" ) )
            {
                turn( -turning );
                if ( isAtEdge() || isTouching( WallV.class ) || isTouching( WallH.class ) || isTouching( Tank.class ) )
                {
                    turn( turning );
                }
            }
            if ( isAtEdge() || isTouching( WallV.class ) || isTouching( WallH.class ) || isTouching( Tank.class ) )
            {
                movement *= -1;
            }
            move( movement );
        }
    }
    public void gotHit()
    {
        health--;
    }
    public int getHealth()
    {
        return health;
    }
}
-Turret
import greenfoot.*;
public class GreenT extends Turret
{
    private GreenC chasisG;
    private int rotation;
    private int reload;
    public GreenT( GreenC chasisG )
    {
        this.chasisG = chasisG;
    }
    public void act() 
    {
        setLocation( chasisG.getX(), chasisG.getY() ); 
        setRotation( chasisG.getRotation() + rotation );
        if ( chasisG.getHealth() > 0 )
        {
            if ( Greenfoot.isKeyDown( "q" ) )
            {
                rotation -= 2;
            }
            if ( Greenfoot.isKeyDown( "e" ) )
            {
                rotation += 2;
            }
            if ( reload > 0 )
            {
                reload--;
            }
            if ( reload <= 0 )
            {
                if ( Greenfoot.isKeyDown( "c" ) )
                {
                    Shell shellG = new Shell(); 
                    shellG.setImage( "GreenS.png" );
                    shellG.setRotation( getRotation() );  
                    getWorld().addObject( shellG, getX(), getY() );  
                    shellG.move( 10 );
                    reload = 200;
                }
            }
        }
    }
}    
Health-
import greenfoot.*;
public class GreenH extends Health
{
    private GreenC chasisG;
    public GreenH( GreenC chasisG )
    {
        this.chasisG = chasisG;
    }
    public void act() 
    {
        setLocation( chasisG.getX(), chasisG.getY() - 70 );
        if ( chasisG.getHealth() == 1 )
        {
            setImage( "balloon2.png" );
        }
        if ( chasisG.getHealth() == 0 )
        {
            setImage( "balloon1.png" );
        }
    }    
}
danpost danpost

2017/5/25

#
It is possible to adjust the code and move it into the Tank class to be usable for all four types (so the four subclasses can be removed). Is this what you want to do?
JeSuisFish JeSuisFish

2017/5/26

#
Yes, I also ran into a problem with my shell class just now, I spawn a shell in on the location and rotation of the turret and then it moves forward, but since the shell class has no subclasses, the tank cannot tell the difference between its shell and another tanks shell being fired.
danpost danpost

2017/5/26

#
You just need an identifier in the combined classes (Tank, Turret and Shell) -- probably an int field to distinguish one from another (using values from zero to three). Add methods to the classes to return the identification number so that other objects can determine which they are.
You need to login to post a reply.