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

2019/12/9

Value of Variable won´t change

JohannesDerHeld JohannesDerHeld

2019/12/9

#
I got a bit of a weird problem here. I have a class GameManager and the subclasses Player and Flag. Both Player and Flag have a a variable health. In the act method its constantly checking if the health drops beneath 1. Then i want to call the method gameOver() from the GameManager class, which should stop the game and prevent further enemy-wave spawning. The problem is: when calling the gameOver() method from the Player everything works fine, but when calling it from Flag the value of waveCounter does not change. Now I am not sure if I am blind or this is some weird error of Greenfoot. Btw the waveCounter value does not get modified from any other class. GameManager:
 import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
        
        /**
         * the GameManager is responsible to coordinate the game procedure and make the different objects 'work' better together 
         */
        public abstract class GameManager extends Actor
        {
            
            //#################################################################################################
            //PROTECTED_GLOABAL_VARS (is used to get an faster & esier accsess in the inheriting classes to the objects in the world)
            protected Weapon weapon;
            protected Player player;
            protected CrossHair crossHair;
            protected Flag flag;
            protected HealthBar healthBarPlayer;
            protected boolean gameIsOver = false; //trigger that no further will get spawned in the updateGamemanger()
            
            //##################################################################################################
            //PRIVATE_GLOBAL_VARS
            private int waveCount = 0;
            private boolean spawnedWave = false;
            private boolean waitingForSpawn = false;
            private long compareTime;
            private int enemyHealth  = 4;   //is a counter for the waves in default case  --> look WaveManangement()
            
            
            //###################################################################################################
            //ALIBI-ACT-METHOD (frequently called)
            /**
             * equivalent to act, which wont work in our programm, so we decided to outsource it into the players act-method
             * and to call it from there in every act 
             */
            public void updateGameManager()
            {
                startGame();
                checkForWaveEnd();
                spawnNewWave();
                checkForResetGame();
            }
                     
            //###################################################################################################
            //OTHER_METHODS
            /**
             * method to set the reference from the objects in world to the protected objects in this class
             */
            protected void addedToWorld(World world)
            {
                player = ((MyWorld)world).getPlayer();
                weapon = ((MyWorld)world).getWeapon();
                crossHair = ((MyWorld)world).getCrossHair();
                flag = ((MyWorld)world).getFlag();
                healthBarPlayer = ((MyWorld)world).getHealthBarPlayer();
                updateBullets();
            }
           
            /**
             * tells the player when teh new wave is going to start 
             */
            private void checkForWaveEnd(){

                if(((MyWorld)getWorld()).checkForEnemies() && waveCount > 0 && !waitingForSpawn && !gameIsOver)
                {
                    waitingForSpawn= true;
                    compareTime = System.currentTimeMillis();
                    getWorld().showText("Wave " + waveCount, 300, 200);
                }
            }
            
            /**
             * after a timer delay of 3 secs the new wave gets spawned
             */
            private void spawnNewWave(){
                if(waitingForSpawn && System.currentTimeMillis() > compareTime + 3000 & !gameIsOver)
                {
                   WaveManagement(); 
                   ((MyWorld)getWorld()).removeBlood();
                   flag.regenerateHealth();
                   waitingForSpawn = false; 
                }    
            }
            /**
             * after pressing enter, a new game with "fresh" vars get created 
             */
            private void checkForResetGame(){
                if(gameIsOver && Greenfoot.isKeyDown("enter"))
                {
                    ((MyWorld)getWorld()).removeBlood();
                    
                    player.speed = 2;
                    player.health = 5;
                    player.setImage(new GreenfootImage("Player.png"));
                    
                    flag.regenerateHealth();
                    
                    waveCount = 0;
                    spawnedWave = false;
                    
                    getWorld().showText("                       ", 300, 200);
                    getWorld().showText("                       ", 300, 240);
                    getWorld().showText("                       ", 300, 280);
                    
                    gameIsOver = false;
                    //reset-Button anzeigen und abfragen --> wenn gedrückt, alles resetten und press space to start anzeigen 
                }
            }
            
            /**
             * is responsible for the first wave which is getting spawned and also for the "Press Space.." Text --> 
             * based on the global bool spawnedWave (represents the current condition)
             */
            public void startGame() 
            {   
                if(!spawnedWave && Greenfoot.isKeyDown("space"))
                {
                    spawnedWave = true;   
                    waveCount = 2;
                    getWorld().showText("                       ", 300, 200);
                    ((MyWorld)getWorld()).spawnEnemies(3,2,1);
                }
                
                
                if(!spawnedWave)
                {
                    getWorld().showText("Press space to start", 300, 200);
                }
            } 
        
            /**
             * manages the increasing difficulty by adjusting in each wave: --> waveCount equals the global condition 
             *              a. the weapon capacity 
             *              b. the spawned enemy waves --> rows and enemies in each row (until wave seven)
             *              c. the health of the enemies (unlimited; also after wave seven --> default)
             *              
             * the text witn teh showen wave gets overlayed with plane text 
             * max. spawning enemies = 9;
             * after wave 7 only health increses each round
             */
            public void WaveManagement()
            {
                switch(waveCount)
                {
                    case 2: 
                        weapon.capacity = 45;
                        weapon.bulletsInMag = weapon.capacity;
                        ((MyWorld)getWorld()).spawnEnemies(4,3,1);
                        waveCount++;
                        getWorld().showText("                       ", 300, 200);                
                        break;
                    case 3: 
                        weapon.capacity = 45;
                        weapon.bulletsInMag = weapon.capacity;
                        ((MyWorld)getWorld()).spawnEnemies(3,3,2);
                        waveCount++;
                        getWorld().showText("                       ", 300, 200);                
                        break;
                    case 4: 
                        weapon.capacity = 40;
                        weapon.bulletsInMag = weapon.capacity;
                        ((MyWorld)getWorld()).spawnEnemies(4, 3, 2);
                        waveCount++; 
                        getWorld().showText("                       ", 300, 200); 
                        break;
                    case 5: 
                        weapon.capacity = 35;
                        weapon.bulletsInMag = weapon.capacity;                
                        ((MyWorld)getWorld()).spawnEnemies(3, 4, 3);
                        waveCount++; 
                        getWorld().showText("                       ", 300, 200); 
                        break;
                    case 6: 
                        weapon.capacity = 30;
                        weapon.bulletsInMag = weapon.capacity;
                        ((MyWorld)getWorld()).spawnEnemies(3, 5, 3);
                        waveCount++; 
                        getWorld().showText("                       ", 300, 200); 
                        break;
                    case 7: 
                        weapon.capacity = 20;
                        weapon.bulletsInMag = weapon.capacity;
                        ((MyWorld)getWorld()).spawnEnemies(3, 6, 3);
                        waveCount++; 
                        getWorld().showText("                       ", 300, 200); 
                        break;
                    default:
                        
                        weapon.capacity = 20;
                        weapon.bulletsInMag = weapon.capacity;
                        ((MyWorld)getWorld()).spawnEnemies(3, enemyHealth + 3, 3);
                        waveCount++; 
                        enemyHealth++;
                        getWorld().showText("                       ", 300, 200); 
                        break;
                }
            }
            /**
             * magazine counter in the top left corner --> data from the global weapon obj 
             */
            public void updateBullets()
            {
                getWorld().showText(weapon.bulletsInMag + "/" + weapon.capacity, 25, 15);
            }
            
            public void gameOver()
            {
                this.gameIsOver = true; 
                ((MyWorld)getWorld()).removeEnemies();
                waitingForSpawn = false; 
                getWorld().showText("GameOver", 300, 200);
                getWorld().showText("überlebt bis Welle "  + (waveCount -1) , 300, 240);
                getWorld().showText("Press enter to restart", 300, 280);
                waveCount = 0;
            }
}
Player:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Player extends Mover
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */    
    int health = 5;
    int speed = 2;  
    
    public void act() 
    {
        updateGameManager();
        turnPlayer();
        playerMovement();
        
        if(health < 1 && !gameIsOver)
        {
            gameOver();
            speed = 0;
            setImage(new GreenfootImage("Transparent.png"));
            ((MyWorld)getWorld()).spawnBlood(getX(), getY());
        }
    }  
    
    void playerMovement()
    {
        if(Greenfoot.isKeyDown("w"))
        {
            move("up", speed);
        }
        if(Greenfoot.isKeyDown("s"))
        {
            move("down", speed);
        }
        if(Greenfoot.isKeyDown("a"))
        {
            move("left", speed);
        }
        if(Greenfoot.isKeyDown("d"))
        {
            if(getX() < getWorld().getWidth()/2)
            {
                move("right", speed);
            }
        }
    }
}
Flag:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Flag extends Mover
{
    //##################################################################################################################################
    //GLOBAL_VARS
    public int health = 10;
    
    
    //##################################################################################################################################
    //CONSTRUCTOR
    public Flag()
    {
        
    }
    
    
    //##################################################################################################################################
    //ACT-METHOD (frequently called)
    public void act()
    {
        if(health < 1 && !gameIsOver)
        {
            gameOver();
        }
    }
    
    
    //##################################################################################################################################
    //OTHER_METHODS
    public void regenerateHealth()
    {
        health = 10;
    }
    
}
danpost danpost

2019/12/9

#
JohannesDerHeld wrote...
I have a class GameManager and the subclasses Player and Flag. Both Player and Flag have a a variable health. In the act method its constantly checking if the health drops beneath 1. Then i want to call the method gameOver() from the GameManager class, which should stop the game and prevent further enemy-wave spawning. The problem is: when calling the gameOver() method from the Player everything works fine, but when calling it from Flag the value of waveCounter does not change. Now I am not sure if I am blind or this is some weird error of Greenfoot.
I think it may be a misunderstanding of how java works. You create both a player and a flag; therefore, you have TWO GameManager objects. Updating the player GameManager fields will not update the flag ones and vise versa. So, your class structure needs re-thinking. Usually, your world would be responsible for controlling the game -- not your actors.
JohannesDerHeld JohannesDerHeld

2019/12/9

#
Thank you, my fault. Fixed it by calling player.gameOver() instead of just gameOver() in Flag.
You need to login to post a reply.