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

2022/1/5

using getObjects with specific index

nax_s nax_s

2022/1/5

#
Hi there, so i'm making tower defense game, i want to add a tower to platform. I have 6 platform instances and 3 tower, when i click 1 of the platform and click one of the tower, the tower will be added to the platform that i clicked earlier. i can't figure that, here some of my code.
public class MyWorld extends World
{
        Platform platform = new Platform();
        addObject(platform,90,355);
        Platform platform2 = new Platform();
        addObject(platform2,90,215);
        Platform platform3 = new Platform();
        addObject(platform3,300,215);
        Platform platform4 = new Platform();
        addObject(platform4,300,355);
        Platform platform5 = new Platform();
        addObject(platform5,550,355);
        Platform platform6 = new Platform();
        addObject(platform6,550,215);
}
public class Platform extends Actor
{
public int ind;
public void checkInd() {
        if(!getWorld().getObjects(Platform.class).isEmpty()) {
            if(getX() == 550 && getY() == 215) {
                 ind = 5;
            }
            else if(getX() == 550 && getY() == 355) {
                ind = 4;
            }
            else if(getX() == 300 && getY() == 355) {
                ind = 3; 
            }
            else if (getX() == 300 && getY()== 215) {
                ind = 2; 
            }
            else if (getX() == 90 && getY() == 215) {
                ind = 1;
            }
            else if (getX() == 90 && getY() == 355) {
                ind = 0;
            }
        }
    }
    public int getInd() {
        return ind;
    }
}
public class BuyArcher extends Platform
{
    public BuyArcher() {
        GreenfootImage myImage = getImage(); 
        int myWidth = (int)myImage.getWidth(); 
        int myHeight = (int)myImage.getHeight();
        myImage.scale(myWidth, myHeight);
    }
    public void act()
    {
        checkClicked();
    }
    public void checkClicked() {
        if (Greenfoot.mouseClicked(this)) {
            if(!getWorld().getObjects(Platform.class).isEmpty()) {
                Platform plat = (Platform) getWorld().getObjects(Platform.class).get(ind); 
                int platX = plat.getX(); 
                int platY = plat.getY(); 
                Archer archer = new Archer(); 
                getWorld().addObject(archer,platX, platY);
                
            }
        }
    }
}
Spock47 Spock47

2022/1/5

#
Sorry, but your source codes will not work, please completely remove your current source code from Platform and BuyArcher before adding the source code given in this post. As I understand, you want to click on a platform and after that you want to click on a button ("BuyArcher") and then a tower has to appear on the platform that was clicked earlier. That means that the button somehow needs the information which platform has been clicked. This information has to be stored somewhere, so that it can be used later. Currently, there is nothing that does this in your source code. 1. A suitable place to remember the currently selected platform is the world:
public class PlatformWorld extends World
{
    ....
    
    private Platform selectedPlatform = null;
    
    public Platform getSelectedPlatform() {
        return selectedPlatform;
    }
    
    public void setSelectedPlatform(final Platform platform) {
        selectedPlatform = platform;
    }

    ....
    
}
2. Now, you can use this attribute whenever a platform is clicked:
public class Platform extends Actor
{
    @Override
    public void act()
    {
        final PlatformWorld platformWorld = (PlatformWorld)getWorld();
        if (platformWorld != null && Greenfoot.mouseClicked(this)) {
            platformWorld.setSelectedPlatform(this);
        }
    }
}
3. Now, you need the buy-buttons and the actual towers. Let's start with the towers, a super class and a subclass for each type (I only will show the archer):
public class Tower extends Actor
{
    // add whatever common behavior all towers have after being installed on the platform.
}

public class Archer extends Tower
{
    // add whatever the archer tower does after it has been installed on the platform.
}
4. Finally, you can add the buy buttons that uses the information which platform is selected and add the tower to the platform, if clicked. I again use a super-class and subclasses for each tower-type:
public abstract class BuyTower extends Actor
{

    @Override
    public void act()
    {
        final PlatformWorld platformWorld = (PlatformWorld)getWorld();
        if (platformWorld != null) {
            final Platform platform = platformWorld.getSelectedPlatform();           
            if (platform != null && Greenfoot.mouseClicked(this)) {
                platformWorld.addObject(getTower(), platform.getX(), platform.getY());
                platformWorld.setSelectedPlatform(null);
            }
        } 
    }
    
    protected abstract Tower getTower();
    
}
public class BuyArcher extends BuyTower
{
    @Override
    public Tower getTower() {
        return new Archer();
    }
}
Note that I don't remove the platform from the world, I only add the tower on top. If you want to remove the platform, you can do that immediately after the addObject call for the new tower in BuyTower class. Live long and prosper, Spock47
You need to login to post a reply.