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

2020/3/3

How to add an object to a different world from an Actor class?

ThatAsianPixel ThatAsianPixel

2020/3/3

#
Hello there I am doing a project for school right now, and I need some help with the code. I currently have an Actor in a world, and I want to do it where when the Actor gets clicked, it adds an object to another world. Could I get some help with this?
danpost danpost

2020/3/3

#
ThatAsianPixel wrote...
Hello there I am doing a project for school right now, and I need some help with the code. I currently have an Actor in a world, and I want to do it where when the Actor gets clicked, it adds an object to another world. Could I get some help with this?
Too vague without relevant attempted codes.
ThatAsianPixel ThatAsianPixel

2020/3/4

#
danpost wrote...
Too vague without relevant attempted codes.
public class Blastoise_Selection_CPU extends Blastoise_Selection
{
    private boolean CPU;
    /**
     * Act - do whatever the Charizard_Selection_CPU wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.mouseClicked(this) && CPU == true)
        {
            World battle = new BattlePage();
            battle.addObject(this,300,200);
        }
    }
Here we go sorry; when it runs, it deletes the image from the current World and then when I move to the next world, it's not there.
danpost danpost

2020/3/4

#
ThatAsianPixel wrote...
when I move to the next world,
Where's that?
ThatAsianPixel ThatAsianPixel

2020/3/4

#
danpost wrote...
ThatAsianPixel wrote...
when I move to the next world,
Where's that?
I have a button that allows me to move to a new world. character picture:
public class Blastoise_Selection_CPU extends Blastoise_Selection
{
    private boolean CPU;
    /**
     * Act - do whatever the Charizard_Selection_CPU wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.mouseClicked(this) && CPU == true)
        {
            World battle = new BattlePage();
            battle.addObject(this,300,200);
        }
    }
Here's the current world:
public selectionPage()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        setBackground("selectionBackground.jpg");
        
        Actor playerSelect = new playerSelect();
        addObject(playerSelect, 250, 25);
        
        Actor p1_CPU = new selection();
        addObject(p1_CPU, 250, 55);
        
        Actor charizard1 = new Charizard_Selection();
        addObject(charizard1,175,125);
        
        Actor charizard2 = new Charizard_Selection_CPU();
        addObject (charizard2,375, 125);
        
        Actor venusaur1 = new Venusaur_Selection();
        addObject(venusaur1, 175, 235);
        
        Actor venusaur2 = new Venusaur_Selection_CPU();
        addObject(venusaur2, 375, 235);
        
        Actor blastoise1 = new Blastoise_Selection();
        addObject(blastoise1, 175, 345);
        
        Actor blastoise2 = new Blastoise_Selection_CPU();
        addObject(blastoise2, 375, 345);
        
        Actor selectStart = new selectStartButton();
        addObject(selectStart, 500, 345);
    }
World I want to switch to:
public class BattlePage extends World
{

    /**
     * Constructor for objects of class BattlePage.
     * 
     */
    public BattlePage()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        setBackground("battle_background.jpg");
    }
}
And the button to do it:
public class selectStartButton extends startButton
{
    /**
     * Act - do whatever the selectStartButton wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.mouseClicked(this))
        {
           Greenfoot.setWorld(new BattlePage()); 
        }
    }    
}
danpost danpost

2020/3/4

#
You are adding the button to a world that is created at line 12 in the Blastoise_Selection_CPU class; yet, you transfer to a different BattlePage world that is created at line 11 in the selectStartButton class.
ThatAsianPixel ThatAsianPixel

2020/3/4

#
danpost wrote...
You are adding the button to a world that is created at line 12 in the Blastoise_Selection_CPU class; yet, you transfer to a different BattlePage world that is created at line 11 in the selectStartButton class.
So how should I code it so that it adds an Actor ahead of time onto the BattlePage world and then when I've chosen everything I need to, I can press the start button and go to the world with the Actors on it?
danpost danpost

2020/3/4

#
Maybe have a static Actor selectedActor field in the BattlePage class and have the BattlePage constructor add the actor into its world.
You need to login to post a reply.