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

2025/1/20

Error with transferring methods between classes.

oscarm oscarm

2025/1/20

#
Im trying to get the nextWave function to run in the Myworld class. However it doesn't seem to like it :(
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;


public class Wave_management extends Actor
{
    private WaveManager waveManager; // Reference to WaveManager

    
    public Wave_management(World world) {
        waveManager = new WaveManager(world, Enemy.class); // Instantiate WaveManager with the world
    }
    public void nextWave() {
        waveManager.nextWave();
    }

    public class WaveManager {
        
        private int currentWave; //current wave number 
        private int enemiesToSpawn; //Number of enemies left to spawn in the wave
        private int enemiesAlive; 
        private int spawnInterval; 
        private int spawnTimer;
        private int waveSize;
        private int wavePos;
        private World world;
        private WaveManager waveManager;

        
        public void nextWave() {
            waveManager.nextWave(); // Delegate the call to WaveManager
        }

        public WaveManager(World world, Class<? extends Actor> enemyClass){
            currentWave = 0;
            this.world = world; 
            enemiesAlive = 0;
            spawnInterval = 50; //gonna leave it at 50 may change later.
            spawnTimer = 0;
            waveSize = 5;
            
        
        }
        public void nextWave(){
            currentWave = currentWave+1;
            if(currentWave <10){
                enemiesToSpawn = waveSize*5;
                if(enemiesToSpawn>0){
                    spawnEnemy();
                    spawnEnemy(); // spawn double in the final 3 to increase diff at end of waves 
                }
                if(enemiesToSpawn>3){
                    spawnEnemy();
                }
                waveSize = waveSize+2;
                
            }
            
            
        }
        
        public void spawnEnemy(){
            int x, y;
        
            //Randomly decide which border the enemy will spawn on
            int border = Greenfoot.getRandomNumber(4); // 0-Top, 1-Bottom, 2-Left, 3-Right
        
            if (border == 0) { 
                x = Greenfoot.getRandomNumber(650 - 150) + 150; // Range: 150 to 650
                y = 150; // Fixed at the top boundary
            } else if (border == 1) { 
                x = Greenfoot.getRandomNumber(650 - 150) + 150; // Range: 150 to 650
                y = 650; 
            } else if (border == 2) { 
                x = 150; 
                y = Greenfoot.getRandomNumber(650 - 150) + 150; // Range: 150 to 650
            } else { // Right border
                x = 650; // Fixed at the right boundary
                y = Greenfoot.getRandomNumber(650 - 150) + 150; // Range: 150 to 650
            }
        
            //Spawn the enemy at the position
            Enemy enemy = new Enemy(); 
            world.addObject(enemy, x, y);
        }
    }
    
    //Need a nextwave function, which includes spawning enemies 
    
    
    public void act()
    {
        
    }
}
Here is the MyWorld code, im struggling to get to grips with the whole OOP thing. Hopefully someone can help.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class MyWorld extends World
{
    private Wave_management waveManager;
    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 800, 1); 
        waveManager = new Wave_management(); 
        
        addObject(waveManager, 0, 0);
        prepare();
        
    }
    
    public void act() {
        waveManager.nextWave();
    }
    private void prepare()
    {
        Main_Character main_Character = new Main_Character();
        addObject(main_Character,312,197);
        Enemy enemy = new Enemy();
        addObject(enemy,431,49);
        
        
        
        
        
    }
    
}
danpost danpost

2025/1/20

#
oscarm wrote...
Im trying to get the nextWave function to run in the Myworld class. However it doesn't seem to like it :( << Codes Omitted >>
In your Wave_management class, remove line 28. Maybe you can see why.
You need to login to post a reply.