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

2020/3/20

Victory Screen not showing

ezio4864 ezio4864

2020/3/20

#
The code is supposed to display the victory text after shooting down three enemy tanks. When a tank is shot down, the variable enemyCount whose value is 3 is deducted by one. This variable is in the world class. If the value of enemyCount is equal to 0, it will add the object VictoryScreen(). Below are the codes for each class: code of the world "test":
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class test here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class test extends World
{
    private PlayerTank thePlayerTank; //Creates an accessible PlayerTank actor.
    private TitleScreen theTitle;
    private InstructionsScreen theRules;
    public VictoryScreen victory = new VictoryScreen();
    /**
     * Constructor for objects of class test.
     * 
     */
    int enemyCount = 3;
    public test()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        thePlayerTank = new PlayerTank(); 
        addObject(thePlayerTank, 500, 340); 
        addObject(new AI1(), 100, 340); 
        addObject(new AI2(), 500, 60); 
        addObject(new AI3(), 100, 60);
        if (enemyCount == 0){
            addObject(new VictoryScreen(), getWidth()/2, getHeight()/2);
        }
    }

    public void deduct(){
        enemyCount = enemyCount - 1;
    }
}
code of the tanks:
import greenfoot.*;
public class AI1 extends Actor
{
    public void act()
    {
        shoot();
        moveAndTurn();
        if (isTouching(Shot.class)){
                test test = new test();
                test.deduct();
            }
    }//Declares all the methods to be used within the actor AI1.
    private int shotTimer = 0;
    //Creates a variable that will later on store the time it takes to reload the tank.
    public void shoot() 
    {
        if (shotTimer > 0) {
            shotTimer = shotTimer - 1;
        }//After a shot is fired, the if-statement runs as a timer for when you can shoot again.
        else if (Greenfoot.getRandomNumber (1000)<100) {
            getWorld().addObject(new ShotAI1(getRotation()), getX(), getY());
            shotTimer = 80; 
        }//Randomly generates a shot and starts the timer.
    }

    public void moveAndTurn()
    {
        if (Greenfoot.getRandomNumber(1000)<100){
            move (5);
        }//Randomly moves the tank.
        else {
            turn(5);
        }//If the tank is not moving forward, it is turning in place.
    }
}

Code of the player's shot class:
import greenfoot.*;  
public class Shot  extends Actor
{
    private PlayerTank myShip;
    private int direction, speed;
    //Declares variables to be used within the code.
    public Shot (int dir)
    {
        direction = dir;
        speed = 5;
        //Instanciates the variables direction and speed.
    }

    public Shot(PlayerTank myShip)
    {
        this.myShip = myShip;
    }//Transfers the value of local variable myShip to global variable myShip.
    public void act() 
    {
        shooting();
    }

    public void shooting (){
        if (isAtEdge() != true) {
            setRotation (direction);
            move (speed);
            setLocation(getX(), getY());
            int enemyCount = 3;
            //Sets the location of the shot to the same location as the tank and moves the shot forward in the same direction that the tank is facing.
            Actor AI1 = getOneIntersectingObject(AI1.class);
            Actor AI2 = getOneIntersectingObject(AI2.class);
            Actor AI3 = getOneIntersectingObject(AI3.class);
            //Checks whether the shot is intersecting with the AI Tanks.
            if (AI1 != null) {
                getWorld().removeObject(AI1);
                getWorld().removeObject(this);
            }//If the shot intersects the location of the first AI tank, then the shot and the AI tank disappear.
            if (AI2!= null) {
                getWorld().removeObject(AI2);
                getWorld().removeObject(this);
            }//If the shot intersects the location of the second AI tank, then the shot and the AI tank disappear.
            if (AI3 != null) {
                getWorld().removeObject(AI3);
                getWorld().removeObject(this);
            }//If the shot intersects the location of the third AI tank, then the shot and the AI tank disappear.

            /*if (enemyCount == 0){
                World world = getWorld();
                world.removeObjects(world.getObjects(null)); 
                world.addObject(new VictoryScreen(this), world.getWidth()/2, world.getHeight()/2);
            }*/
        }//If the location of the shot is not (x,0), then the if-statement will run.

        else{
            getWorld().removeObject(this);
        }
        //If the location of the shot is at the edge of the world then the shot will disappear.
        
    }
}
Help is much appreciated!
Super_Hippo Super_Hippo

2020/3/20

#
In line 9 of your AI1 class, you create a new test object. A new one. What you want to do is to reduce the variable in the current world, not in a new one. However, there is another problem. The Shot removes the AI1 from the world when they intersect while the AI1 tries to do something when they interact. So besides the fact that you decrease a variable in a new object, you can’t even be sure that this happens. So, keep the intersection code in one of the two classes. And use a reference to your test class and don’t create a new one every time. Let’s say you keep it in your Shot class:
if (AI1 != null)
{
    ((test) getWorld()).deduct();
    getWorld().removeObject(AI1);
    getWorld().removeObject(this);
}
danpost danpost

2020/3/20

#
Insert the following between lines 28 and 29 in your test class:
}

public void act()
{
(enemyCount is assigned a value of 3 and is never zero during the construction of a test world) Actually, instead of the above, better might be to just move lines 29 and 30 to after line 35. Also, you can probably remove lines 12, 13 and 14 from the class.
moraidai moraidai

2020/3/20

#
So, keep the intersection code in one of the two classes. And use a reference to your test class and don’t create a new one every time. Let’s say you keep it in your Shot class: ? 1 2 3 4 5 6 if (AI1 != fromage) { ((test)baguetted()).deduct(); getWorld().rfromaget(AI1); getWorld().croissantObject(this); }
You need to login to post a reply.