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

2022/4/27

Changing actors

ADAM68801 ADAM68801

2022/4/27

#
Hello, I have a problem with changing actors in the world. I have 2 worlds in my game (Shop and Galaxy). In the shop there are 3 different spaceships. I want that when You click on one spaceship you will chose it and then you can play with it in world Galaxy. Thanks for answers.
ADAM68801 ADAM68801

2022/4/29

#
I really need it. So please can someone reply? Game is on my profile.
Super_Hippo Super_Hippo

2022/4/29

#
One way to do it would be to pass a parameter to the world you are changing to. For example:
Greenfoot.setWorld(new GameWorld(1));
public GameWorld(int shipType)
{
    super(100, 100, 1, false);
    addObject(new Ship(shipType), 50, 50);
}
private int type = 0;
public Ship(int type)
{
    this.type = type;
    updateImage();
}

private void updateImage()
{
    //update image based on ship type
}
Spock47 Spock47

2022/4/29

#
Here is another solution with a small working example that I created in parallel to Super_Hippo, but he was faster. :)
public class Ship extends Actor {}
public class BattleShip extends Ship {}
public class ResearchShip extends Ship {}
public class Hero extends Actor
{
    private Ship ship = new BattleShip();
    
    public void act()
    {
        if (Greenfoot.isKeyDown("w")) {
            setLocation(getX(), getY() - 1);
        }
    }
    
    public void setShip(final Ship newShip) {
        getWorld().removeObject(ship);
        ship = newShip;
        getWorld().addObject(ship, getX(), getY());
    }
    
    @Override
    public void addedToWorld(final World newWorld) {
        setShip(ship);
    }
    
    @Override
    public void setLocation(int x, int y) {
        super.setLocation(x, y);
        ship.setLocation(x, y);
    }
    
}
public class ShipButton extends Actor
{
    private final Hero hero;
    
    private final Ship ship;
    
    public ShipButton(final Hero theHero, final Ship shipToChoose) {
        hero = theHero;
        ship = shipToChoose;
        setImage(ship.getImage());
    }
    
    public void act()
    {
        if (Greenfoot.mouseClicked(this)) {
            hero.setShip(ship);
        }
    }
}
import java.util.ArrayList;
import java.util.List;

public class ShipChooserMenu extends Actor
{
    
    final List<ShipButton> buttons = new ArrayList<>();
    
    public ShipChooserMenu(final Hero hero) {
        buttons.add(new ShipButton(hero, new BattleShip()));
        buttons.add(new ShipButton(hero, new ResearchShip()));
    }
    
    @Override 
    public void addedToWorld(final World world) {
        for (int i = 0; i < buttons.size(); ++i) {
            world.addObject(buttons.get(i), getX() + 150 * i, getY());
        }
    }
    
}
public class MyWorld extends World
{

    public MyWorld()
    {
        super(600, 400, 1); 
        prepare();
    }
    
    public void prepare() {
        final Hero hero = new Hero();
        addObject(hero, 100, 300);
        final ShipChooserMenu choose = new ShipChooserMenu(hero);
        addObject(choose, 50, 50);
    }
}
In your example, you want to add the ShipChooserMenu into the Shop world class.
ADAM68801 ADAM68801

2022/4/30

#
Thank you. It worked
ADAM68801 ADAM68801

2022/4/30

#
But there is another problem. I have variable for coins, and if I buy something it will automaticly subtract price from coin variable. It is working but problem is that I dont see how the value on screen change (it change but I have to refresh game to see it). Any ideas?
ADAM68801 ADAM68801

2022/4/30

#
(https://www.greenfoot.org/scenarios/29467) This is a link to my scenario And the problem is in world Shop
Spock47 Spock47

2022/4/30

#
The if-statements that replace the buy-icon with the equip-icon are currently in the constructor of class Shop. That means, that they are only executed when the new Shop gets created. But you want them to be checked continuously while the shop is shown. The method that is executed continuously as long as the object is shown, is the act-method. So, if you move these statements into the act method, it should work. (Note that you will have to adjust the needed variables from being defined in the constructor to be private attributes of the class, so that the if-statement in the act method can still use them).
You need to login to post a reply.