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

2020/5/10

Remove objects using world class

scrap69 scrap69

2020/5/10

#
I'm trying to enter code in the world class using the statement /** * This is the method that will be called to activate frenzy mode */ public void frenzyMode() { if(time<700) { myMusic.stop(); frenzyMusic.play(); showText("FRENZY!!!", 390, 25); } } And I want to have it that the class is removed from the world to be replace with another class that is used for the frenzy mode. What do I do?
danpost danpost

2020/5/10

#
Is it that you want to replace every instance of one class with another at the same location of the removed instances at one time?
scrap69 scrap69

2020/5/10

#
Basically in the game there is a character and there's a score count and timer, I want it that as it's trying to state above that when the timer reaches 700 the character "transforms" into another class for the rest of the timer until the game over happens.
danpost danpost

2020/5/10

#
So, it would be something along these lines:
// information needed
PlayerA playerA = << reference to current player character >>;
int x = playerA.getX();
int y = playerA.getY();
int r = playerA.getRotation();
// change players
removeObject(playerA);
PlayerB  playerB = new PlayerB();
addObject(playerB, x, y); // at (x, y) location
playerB.setRotation(r); // with r rotation
Of course, I couldn't be sure as you have provided minimal codes and what was provided is only helpful in knowing where these codes go, not what to use for the code.
scrap69 scrap69

2020/5/10

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The bloodstream is the setting for our White Blood Cell scenario. 
 * It's a place where blood cells, bacteria and viruses float around.
 * 
 * @author Michael Kölling
 * @version 0.1
 */
public class GreenGreens extends World
{

    /**
     * Constructor: Set up the starting objects.
     */
    public GreenGreens()
    {    
        super(780, 360, 1); 

        prepare();
    }

    /**
     * Create new floating objects at irregular intervals.
     */
    public void act()
    {
        if (Greenfoot.getRandomNumber(100) < 3)
        {
            addObject(new Tomato(), 779, Greenfoot.getRandomNumber(360));
        }
        //top();
        //bottom();
        addVirus();
        addRedCell();
        countTime();
        myMusic.play();
        frenzyMode();
    }

    /**
     * Excercise 5.8 New lining will appear on the right side on the top and bottom to make it seem as if the lining is infinite and continiously moving.
     */
    //public void top()
    {
        //if (Greenfoot.getRandomNumber(100) < 1)
        {
            //addObject(new Lining(), 779,0);
        }
    }

    //public void bottom()
    {
        //if (Greenfoot.getRandomNumber(100) < 1)
        {
            //addObject(new Lining(), 779,359);
        }
    }

    /**
     * Excercise 5.10 Adds new virus at right edge of the screen.
     */
    public void addVirus()
    {
        if (Greenfoot.getRandomNumber(100) < 2)
        {
            addObject(new Gordo(), 778, Greenfoot.getRandomNumber(360));
        }
    }

    /**
     * Excercise 5.23 Adds red blood cells into the bloodstream.
     */
    public void addRedCell()
    {
        if (Greenfoot.getRandomNumber(100) < 6)
        {
            addObject(new Kirb(), 778, Greenfoot.getRandomNumber(360));
        }
    }

    /**
     * Prepare the world for the start of the program. In this case: Create
     * a white blood cell and the lining at the edge of the blood stream.
     */
    private void prepare()
    {
        Dedede whitecell = new Dedede();
        /**
         * Excercise 5.27 The white cell's position was changed so that it wasn't so close to the border.
         */
        addObject(whitecell, 110, 179);
        //Lining lining = new Lining();
        //addObject(lining, 126, 1);
        //Lining lining2 = new Lining();
        //addObject(lining2, 342, 5);
        //Lining lining3 = new Lining();
        //addObject(lining3, 589, 2);
        //Lining lining4 = new Lining();
        //addObject(lining4, 695, 5);
        //Lining lining5 = new Lining();
        //addObject(lining5, 114, 359);
        //Lining lining6 = new Lining();
        //Lining lining7 = new Lining();
        //addObject(lining7, 295, 353);
        //Lining lining8 = new Lining();
        //Lining lining9 = new Lining();
        //Lining lining10 = new Lining();
        //addObject(lining10, 480, 358);
        //Lining lining11 = new Lining();
        //addObject(lining11, 596, 359);
        //Lining lining12 = new Lining();
        //addObject(lining12, 740, 354);
        Border border = new Border();
        addObject(border, 0, 180);
        Border border2 = new Border();
        addObject(border2, 770, 180);
        /**
         * Excercise 5.28 Code was added to make the border objects appear on top of other objects.
         */
        setPaintOrder(Border.class);
        /**
         * Excercise 5.39 Initial score is 0.
         */
        score = 0;
        /**
         * Excercise 5.47 showScore method is called to have score displayed on the start of the game.
         */
        showScore();
        /**
         * Excercise 5.52 Time set to be 2000.
         */
        time = 2000;
        showTime();
    }

    /**
     * Excercise 5.38 Score variable moved to Bloodstream.
     */
    private int score;
    /**
     * Excercise 5.40 New public method called addScore.
     */
    public void addScore(int points)
    {
        //score = score + 20;
        /**
         * Excercise 5.43 New method to expect a parameter for points.
         */
        score = score + points;
        //showText("Score:" + score, 80, 25);
        showScore();
        /**
         * Excercise 5.50 Game over function has been moved into the addScore  method and an if statemnt has been created so that whne the points are below 0, the game is over.
         */
        if(score < 0)
        {
            myMusic.stop();
            frenzyMusic.stop();
            Greenfoot.playSound("lose.wav");
            /**
             * Mod 2 Has a game over screen and plays lose music.
             */
            showText("GAME OVER",390, 180);
            Greenfoot.stop();
        }
    }

    /**
     * Excercise 5.46 New private method called showScore().
     */
    private void showScore()
    {
        showText("Score:" + score, 80, 25);
    }
    
    /**
     * Excercise 5.51 Added instance variable of int time
     */
    public int time;
    
    /**
     * Excercise 5.53 New method named showTime and will display time on the top right.
     */
    private void showTime()
    {
     showText("Time:" + time, 700, 25);   
    }
    
    /** 
     * Excercise 5.54 New method named countTime to have timer count down by 1.
     */
    public void countTime()
    {
        time = time - 1;
        showTime();
        /**
         * Excercise 5.55
         */
        if(time == 0)
        {
            frenzyMusic.stop();
            myMusic.stop();
            victoryMusic.play();
            Greenfoot.stop();
            showEndMessage();
        }
    }
    
    /**
     *  Excercise 5.56 Another private method that will display a message if we survive until the timer runs out.
     */
    private void showEndMessage()
    {
        
        
        
        showText("Time is up!", 390, 120);
        showText("YOU WIN!", 390, 150);
        showText("Your final score:" + score, 390, 180);
        
    }
    
    /**
     * Mod 1, new sound file made which is background music.
     */
    GreenfootSound myMusic = new GreenfootSound("start.wav");
    GreenfootSound victoryMusic = new GreenfootSound("victory.wav");
    GreenfootSound frenzyMusic = new GreenfootSound("frenzy.wav");
    /**
     * This is the method that will be called to activate frenzy mode
     */
    public void frenzyMode()
    {
        if(time<700)
        {
           

            
            myMusic.stop();
            frenzyMusic.play();
            showText("FRENZY!!!", 390, 25);
        }
    }
    
}
scrap69 scrap69

2020/5/10

#
Dedede is the original actor and with the statement labeled frenzyMode I want it so that when the timer reaches 700 the Dedede actor is removed and replaced with another actor I have in my project labeled as masked.
danpost danpost

2020/5/10

#
scrap69 wrote...
Dedede is the original actor and with the statement labeled frenzyMode I want it so that when the timer reaches 700 the Dedede actor is removed and replaced with another actor I have in my project labeled as masked.
Then replace all "PlayerA" with "Dedede" and replace all "PlayerB" with the name of the class whose instance is to take over. My line 1 can be updated to:
Actor whiteCell = (Actor)getObjects(Dedede.class).get(0);
scrap69 scrap69

2020/5/10

#
It almost worked but I got these two errors java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0 at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64) at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70) at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:248) at java.base/java.util.Objects.checkIndex(Objects.java:372) at java.base/java.util.ArrayList.get(ArrayList.java:458) //at GreenGreens.frenzyMode(GreenGreens.java:236) //at GreenGreens.act(GreenGreens.java:38) at greenfoot.core.Simulation.actWorld(Simulation.java:573) at greenfoot.core.Simulation.runOneLoop(Simulation.java:506) at greenfoot.core.Simulation.runContent(Simulation.java:193) at greenfoot.core.Simulation.run(Simulation.java:183) The ones with the slashes are the ones that we red.
scrap69 scrap69

2020/5/10

#
 /**
     * This is the method that will be called to activate frenzy mode
     */
    public void frenzyMode()
    {
        if(time<700)
        {
            Dedede dedede = (Dedede)getObjects(Dedede.class).get(0);
            int x = dedede.getX();
            int y = dedede.getY();
            int r = dedede.getRotation();
            // change players
            removeObject(dedede);
            MaskedDedede  masked = new MaskedDedede();
            addObject(masked, x, y); // at (x, y) location
            masked.setRotation(r); // with r rotation

            myMusic.stop();
            frenzyMusic.play();
            showText("FRENZY!!!", 390, 25);
        }
    }
This is the updated code with the modifications you gave.
danpost danpost

2020/5/10

#
scrap69 wrote...
It almost worked but I got these two errors << Error Trace Omitted >> The ones with the slashes are the ones that we red.
Try:
if (time == 700)
scrap69 scrap69

2020/5/10

#
It worked like a charm! Thank you so much! By any chance do you know if it's possible to have the showText method have text in different colors?
danpost danpost

2020/5/10

#
scrap69 wrote...
do you know if it's possible to have the showText method have text in different colors?
I do not believe you can alter how showText displays the text. Please refer to my Value Display Tutorial scenario.
You need to login to post a reply.