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

2021/5/3

Im trying to spawn a different monster in myWorld but i cant figure it out.

1
2
ambrokato ambrokato

2021/5/3

#
The game starts by killing the first type of enemy. I have a condition that when a number of enemies are dead it will switch to a different scenario(Shop) where they can purchase weapons, I have this "back button"(actor) that when it is clicked. It will switch back to the myWorld. but instead of killing the same type of enemies I want to spawn Golem(actor) instead. This is the code I have in myWorld ,, This is like my main level.
public void act()
    {
        count++;
        swtichToShop();
        if (currentLevel == 1)
        {
           spawnZombies(); 
        }
        else if ( currentLevel == 2)
        {
            spawnGolem();
        }
        
    } 
public void spawnZombies()
    {

        if( count % spawnSpeed == 0)
        {
            randomSpawn = Greenfoot.getRandomNumber(8);
            switch(randomSpawn){
                case  0 : addObject(new Ghost(hero, counter), 0, 0);break;
                case  1 : addObject(new Ghost(hero, counter), getWidth()/2, 0);break;
                case  2 : addObject(new Ghost(hero, counter), getWidth(), 0);break;
                case  3 : addObject(new Ghost(hero, counter), 0, getHeight()/2);break;
                case  4 : addObject(new Ghost(hero, counter), getWidth(), getHeight()/2);break;
                case  5 : addObject(new Ghost(hero, counter), 0, getHeight()); break;
                case  6 : addObject(new Ghost(hero, counter), getWidth()/2, getHeight());break;
                case  7 : addObject(new Ghost(hero, counter), getWidth(), getHeight()); break;

            }
        }

    }

    public void spawnGolem()
    {
        if( count % spawnSpeed == 0)
        {
            randomSpawn = Greenfoot.getRandomNumber(5);
            switch(randomSpawn){
                case  0 : addObject(new Golem(hero, counter), 0, 0);break;// top left
                case  1 : addObject(new Golem(hero, counter), getWidth(), 0);break;//top right
                case  2 : addObject(new Golem(hero, counter), 0, getHeight()); break;//bottom left
                case  3 : addObject(new Golem(hero, counter), getWidth(), getHeight()); break;//bottom right
            }
        }
    }
##### This is the code I have in my Shop class
public class Shop extends World
{
    Counter counter;
    MyWorld myWorld = new MyWorld();
    Back back = new Back();
    /**
     * Constructor for objects of class Shop.
     * 
     */
    public Shop()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
       addObject(back,130,550);
       

        getBackground().drawImage(new GreenfootImage("$5000", 20, Color.BLACK, null), 680, 80);
        getBackground().drawImage(new GreenfootImage("$3500", 20, Color.BLACK, null), 680, 200);
        getBackground().drawImage(new GreenfootImage("$500", 20, Color.BLACK, null), 680, 320);
        getBackground().drawImage(new GreenfootImage("+10hp, $500", 20, Color.BLACK, null), 660, 490);

        getBackground().drawImage(new GreenfootImage("Welcome hero! \n Come and spend your cash!", 20, Color.BLUE, null), 40, 300);

        prepare();
    }
    public void act()
    {
        next();
       
       
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Buy buy = new Buy();
        addObject(buy,693,535);
        Buy buy2 = new Buy();
        addObject(buy2,702,377);
        Buy buy3 = new Buy();
        addObject(buy3,703,249);
        Buy buy4 = new Buy();
        addObject(buy4,702,127);
    }
    public void next()
    { 
        back.nextLevel();
        myWorld.currentLevel = 2;
        
    }
}
danpost danpost

2021/5/3

#
ambrokato wrote...
I have this "back button"(actor) that when it is clicked. It will switch back to the myWorld
It will switch to a new MyWorld -- not back to the same one prior to going to shop (see line 4 in Shop). However, that may not be bad, since you set its current level to 2. Why did you exclude Back class codes from what you gave? Please provide.
ambrokato ambrokato

2021/5/3

#
danpost wrote...
ambrokato wrote...
I have this "back button"(actor) that when it is clicked. It will switch back to the myWorld
It will switch to a new MyWorld -- not back to the same one prior to going to shop (see line 4 in Shop). However, that may not be bad, since you set its current level to 2. Why did you exclude Back class codes from what you gave? Please provide.
Hey Dan it's me again. Here's the code for my Back(actor), Im also using this to go back to the main menu of the game. and the other use for this is with the Shop. What i have in my mind is the player will click this button to go back in the game, not with the same enemy but a new one. Basically like going to the next level.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**

public class Back extends Buttons
{
    Back back;
    public Back()
    {
        
        if (Greenfoot.mouseMoved(this))
        {
            setImage("back_hi.jpg");
        }
        if (Greenfoot.mouseMoved(getWorld()))
        {
            setImage("back.jpg");
        }
    }
    /**
     * Act - do whatever the Back wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //BacktoMain();
        //nextLevel();
    }
    public void nextLevel()
    {
         if (Greenfoot.mouseClicked(this))
        {
            Greenfoot.setWorld(new MyWorld());
            
        }
        if (Greenfoot.mouseMoved(this))
        {
            setImage("back_hi.jpg");
        }
        if (Greenfoot.mouseMoved(getWorld()))
        {
            setImage("back.jpg");
        }
        
    }
    public void BacktoMain()
    {
        
        if (Greenfoot.mouseClicked(this))
        {
            Greenfoot.setWorld(new Main());
            
        }
        if (Greenfoot.mouseMoved(this))
        {
            setImage("back_hi.jpg");
        }
        if (Greenfoot.mouseMoved(getWorld()))
        {
            setImage("back.jpg");
        }
    }
}
ambrokato ambrokato

2021/5/3

#
danpost wrote...
ambrokato wrote...
I have this "back button"(actor) that when it is clicked. It will switch back to the myWorld
It will switch to a new MyWorld -- not back to the same one prior to going to shop (see line 4 in Shop). However, that may not be bad, since you set its current level to 2. Why did you exclude Back class codes from what you gave? Please provide.
With the current code I have, It only spawns the ghost and not the golem, but in my code in the Shop, it should set the "currentLevel" to 2 and spawn the golem right? also a similar question, i have a class that holds my score and the money(Counter class). how can i create a new scenario without losing that value? I want to keep track of those through out the game.
danpost danpost

2021/5/3

#
ambrokato wrote...
how can i create a new scenario without losing that value?
By "new scenario", you mean "new world". Better would be to return to the same world. Maybe you should take a look at my World Changing Demo scenario.
ambrokato ambrokato

2021/5/4

#
danpost wrote...
ambrokato wrote...
how can i create a new scenario without losing that value?
By "new scenario", you mean "new world". Better would be to return to the same world. Maybe you should take a look at my World Changing Demo scenario.
Yes that I mean, Alright. and how about the first one? what should I do Dan?
danpost danpost

2021/5/4

#
ambrokato wrote...
how about the first one? what should I do Dan?
The world retains the current level. Increment it when level is complete (immediately -- before going to shop. ?? in switchToShop method ??).
ambrokato ambrokato

2021/5/5

#
danpost wrote...
ambrokato wrote...
how about the first one? what should I do Dan?
The world retains the current level. Increment it when level is complete (immediately -- before going to shop. ?? in switchToShop method ??).
Hey Dan just a follow-up, So I did the increment before going to the shop which is works perfectly. I can play the game form level 1 to the last level. But the problem now is when I reset greenfoot my world stays at the last level instead of going back to the beggenning.
    int count = 0;
    int spawnSpeed = 80; // lower value for faster spawnspeed 
    int randomSpawn = Greenfoot.getRandomNumber(8);
    HealthBar healthbar = new HealthBar();
    GameOver gameOver;
    MyWorld myWorld;
    public Player hero = new Player();
    Counter counter = new Counter();
    private Pistol gun1;
    private Ak47 ak47;
    //public static int currentScore;
    //public static int currentMoney;
    Ghost ghost;
    Golem golem;
    Boss boss = new Boss(hero, counter);
    private static int currentLevel = 1;
    /**
     * Constructor for objects of class MyWorld.
     */
    public MyWorld()
    {
        super(800, 600, 1);
        addObject(hero, 400, 300);
        gun1 = new Pistol();
        addObject(gun1, hero.getX(), hero.getY());
        addObject(healthbar, hero.getX()-5, hero.getY()-50);
        addObject(counter, 70, 35);
        counter.score = 0;
        counter.money = 0;
        //currentMoney = counter.money;
        golem.golemKilled = 0;
        ghost.ghostKilled = 0;

        Progression(); 
        

    }
##### Here's the code to increment the currentLevel, I could increment it or set the value directly
 
public void swtichToShop()
    {
        if (ghost.ghostKilled == 2)
        {
            Greenfoot.setWorld(new Shop());
            //currentLevel++; 
            currentLevel = 2;
        }
        else if (golem.golemKilled == 3)
        {
            Greenfoot.setWorld(new Shop());
            //currentLevel++;
           currentLevel = 3;
        }
    }
### here are the condition on how the level should progress
public void Progression()
    {
        if (currentLevel == 1)
        {
            spawnZombies(); 
        }
        else if (currentLevel == 2)
        {
            spawnGolem();
        }
        else if ( currentLevel == 3)
        {
            spawnBoss();
        }
    }
And this is my act()
   public void act()
    {
        count++;
        Progression();
        
        swtichToShop();
        addObject(counter, 70, 35);

    } 
danpost danpost

2021/5/5

#
ambrokato wrote...
when I reset greenfoot my world stays at the last level instead of going back to the beggenning.
The currentLevel field should NOT be static. Pass to and retain MyWorld object in a field in Shop so you can return to it.
ambrokato ambrokato

2021/5/5

#
danpost wrote...
ambrokato wrote...
when I reset greenfoot my world stays at the last level instead of going back to the beggenning.
The currentLevel field should NOT be static. Pass to and retain MyWorld object in a field in Shop so you can return to it.
I change it to "int currentLevel; what do mean by this
Pass to and retain MyWorld object in a field in Shop so you can return to it.
ambrokato ambrokato

2021/5/5

#
ambrokato wrote...
danpost wrote...
ambrokato wrote...
when I reset greenfoot my world stays at the last level instead of going back to the beggenning.
The currentLevel field should NOT be static. Pass to and retain MyWorld object in a field in Shop so you can return to it.
I change it to "int currentLevel; what do mean by this
Pass to and retain MyWorld object in a field in Shop so you can return to it.
#### THis is what I have inside my shop class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Shop here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shop extends World
{
    Counter counter;
    MyWorld myWorld;
    Back back = new Back();
    /**
     * Constructor for objects of class Shop.
     * 
     */
    public Shop()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
       addObject(back,130,550);
       

        getBackground().drawImage(new GreenfootImage("$5000", 20, Color.BLACK, null), 680, 80);
        getBackground().drawImage(new GreenfootImage("$3500", 20, Color.BLACK, null), 680, 200);
        getBackground().drawImage(new GreenfootImage("$500", 20, Color.BLACK, null), 680, 320);
        getBackground().drawImage(new GreenfootImage("+10hp, $500", 20, Color.BLACK, null), 660, 490);

        getBackground().drawImage(new GreenfootImage("Welcome hero! \n Come and spend your cash!", 20, Color.BLUE, null), 40, 300);

        prepare();
    }
    public void act()
    {
        next();
       
       
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Buy buy = new Buy();
        addObject(buy,693,535);
        Buy buy2 = new Buy();
        addObject(buy2,702,377);
        Buy buy3 = new Buy();
        addObject(buy3,703,249);
        Buy buy4 = new Buy();
        addObject(buy4,702,127);
    }
    public void next()
    { 
        back.nextLevel();
        
        
    }
}
danpost danpost

2021/5/6

#
ambrokato wrote...
what do mean by this
Pass to and retain MyWorld object in a field in Shop so you can return to it.
In Shop:
// add field
private World myWorld;

// change
public Shop() // to
public Shop(World world)
// with
myWorld = world; // in constructor
Then set 'myWorld' active when back button is clicked. To go to shop, use 'new Shop(this)' from world or 'new Shop(getWorld())' from actor.
ambrokato ambrokato

2021/5/6

#
Then set 'myWorld' active when back button is clicked. i renamed my back button object to previous is it like this?
 if (Greenfoot.mouseClicked(previous))
        {
            Greenfoot.setWorld(myWorld);
        }
but when this one, it just switches back to the shop right away.
danpost danpost

2021/5/6

#
Show creations of all buttons. Indicate which world each is created in.
ambrokato ambrokato

2021/5/6

#
danpost wrote...
Show creations of all buttons. Indicate which world each is created in.
I created a separate actor class,
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class backToWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class backToWorld extends Buttons
{
    
    
    public backToWorld()
    {
        if (Greenfoot.mouseMoved(this))
        {
            setImage("back_hi.jpg");
        }
        if (Greenfoot.mouseMoved(getWorld()))
        {
            setImage("back.jpg");
        }
        
    }
    /**
     * Act - do whatever the backToWorld wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
          if (Greenfoot.mouseMoved(this))
        {
            setImage("back_hi.jpg");
        }
        if (Greenfoot.mouseMoved(getWorld()))
        {
            setImage("back.jpg");
        }
        
    }
In my shop world class, The previousWorld(); is the method that will activate once the player clicked on it.
public class Shop extends World
{
    Counter counter;
    private World myWorld;
    WeaponClass weaponclass;
    Back back = new Back();
    backToWorld previous;
    MyWorld main;
    
    HealthBar hp;
    Buy buy;
    Buy buy1;
    Buy buy2;
    Buy buy3;
    Buy buy4;
    
    /**
     * Constructor for objects of class Shop.
     * 
     */
    public Shop(World world)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.

        super(800, 600, 1);
        previous = new backToWorld();
        addObject(previous,130,550);
        myWorld = world;

        getBackground().drawImage(new GreenfootImage("$5000", 20, Color.BLACK, null), 680, 80);
        getBackground().drawImage(new GreenfootImage("$3500", 20, Color.BLACK, null), 680, 200);
        getBackground().drawImage(new GreenfootImage("$500", 20, Color.BLACK, null), 680, 320);
        getBackground().drawImage(new GreenfootImage("+10hp, $500", 20, Color.BLACK, null), 660, 490);

        getBackground().drawImage(new GreenfootImage("Welcome hero! \n Come and spend your cash!", 20, Color.BLUE, null), 40, 300);
        
        prepare();
    }

    public void act()
    {
        previousWorld();
       

    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Buy buy = new Buy();
        addObject(buy,693,535); //Health potion  $500
        Buy buy2 = new Buy();
        addObject(buy2,702,377); // Ak47 $500
        Buy buy3 = new Buy();
        addObject(buy3,703,249); // MiniMachinegun $3500
        Buy buy4 = new Buy();
        addObject(buy4,702,127); // rpg $5000
    }

    public void previousWorld()
    { 
        
        if (Greenfoot.mouseClicked(previous))
        {
            Greenfoot.setWorld(myWorld);
            
        }
        
        //BuyingWeapon();

    }
There are more replies on the next page.
1
2