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

2018/4/18

problem with setting image on click

TheGoldenProof TheGoldenProof

2018/4/18

#
So I'm making a crude version of minesweeper and I'm working on the part where you flag the tiles. I'm trying to make it so tiles need to be shift-clicked to be flagged. When I try to shift click the tiles, nothing happens. The flag counter doesn't even go up. This is my code in the tile class:
public class Tile extends Actor
{
    GreenfootImage sand = new GreenfootImage("sand.jpg");
    GreenfootImage flagged = new GreenfootImage("flagged.png");
    boolean isFlagged = false;
    public void act() 
    {
        Game game = (Game)getWorld();
        FlagCounter flagger = game.getFlagger();
        if (Greenfoot.mouseClicked(this) && Greenfoot.isKeyDown("shift")) {
            if (!isFlagged) {
                isFlagged = true;
                setImage(flagged);
                flagger.flag();
            } else if (isFlagged) {
                isFlagged = false;
                setImage(sand);
                flagger.unflag();
            }
        }
    }    
}
I don't think its dealing with the clicks properly because when I inspect a tile I've clicked on, it says "boolean isFlagged: false" The flagger's flag() and unflag() just add and subtract 1 from a displayed int. Help is greatly appreciated.
danpost danpost

2018/4/18

#
The code in the Tile class works (tested it out myself). So, something else is going on. Maybe you should show your wold class code for an overview. What type system are you using? Try a different key (maybe "f" instead of "shift"). In my Minesweeper, I just used right-click for flagging and left click for exposing.
TheGoldenProof TheGoldenProof

2018/4/18

#
I've used the return object in other games before and its worked just fine World:
public class Game extends World
{
    FlagCounter flagcounter = new FlagCounter();
    public Game()
    {
        super(20, 23, 20); 
        prepare();
        createTiles();
    }
    public void createTiles() {
        createBombs();
        for (int y = 3; y <=22; y++) {
            for (int x = 0; x <= 19; x++) {
                if (getObjectsAt(x, y, Bomb.class) != null) {
                    addObject(new Tile(), x, y);
                }
            }
        }
    }
    public void createBombs() {
        for (int i = 0; i <10; i++) {
            addObject(new Bomb(), Greenfoot.getRandomNumber(20), Greenfoot.getRandomNumber(20)+3);
        }
    }
    public FlagCounter getFlagger() {
        return flagcounter;
    }
    private void prepare()
    {
        menu menu = new menu();
        addObject(menu, 9, 1);
        Timer timer = new Timer();
        addObject(timer, 2, 1);
        addObject(flagcounter,17,1);
    }
}
Flagger:
public class FlagCounter extends Actor
{
    int flags = 0;
    public FlagCounter() {
        update();
    }
    public void act() {
        update();
    }
    public void update() {
        setImage(new GreenfootImage("  " + flags + "  ", 24, Color.RED, Color.BLACK));
    }
    public void flag() {
        flags++;
    }
    public void unflag() {
        flags--;
    }
}
danpost danpost

2018/4/18

#
danpost wrote...
What type system are you using? Try a different key (maybe "f" instead of "shift"). In my Minesweeper, I just used right-click for flagging and left-click for exposing.
TheGoldenProof TheGoldenProof

2018/4/18

#
I have tried f.
TheGoldenProof TheGoldenProof

2018/4/18

#
Ok I got it to work after realizing i made a very dumb mistake. originally my code was something like this:
public class Tile extends Actor
{
    GreenfootImage sand = new GreenfootImage("sand.jpg");
    GreenfootImage flagged = new GreenfootImage("flagged.png");
    public void act() 
    {
        Game game = (Game)getWorld();
        FlagCounter flagger = game.getFlagger();
        if (Greenfoot.mouseClicked(this) && Greenfoot.isKeyDown("shift")) {
            if (getImage() == sand) {
                setImage(flagged);
                flagger.flag();
            } else if (getImage() == flagged) {
                setImage(sand);
                flagger.unflag();
            }
        }
    }    
}
and I tried changing shift to f and still wasn't working so I figured greenfoot had a problem with checking whether images are the same so I created the variable instead. I came here and edited my post to the variable, which wasn't working (it actually was, I'm getting to it) and changed the key in the post back to shift too, however, I didn't change it back in greenfoot. It was working the whole time but I was pressing the wrong button. It works fine now.
TomazVDSN TomazVDSN

2018/4/19

#
I am still learning and understand the creation of an object game
Game game = new Game();
if possible could you please help me to understand the meaning of this line of code?
Game game = (Game)getWorld();
danpost danpost

2018/4/19

#
The Actor class has an Actor object (an instance of the class) retain the World object, in which is has been placed, in a field of type World (or as a World type object). It must retain it that way because it cannot possibly know every name you might give a subclass of World. If you need to access any member of your subclass of World (say, your Game class), then the compiler will need to know to look there for it. The class name, Game, in parenthesis before the World reference returned by getWorld is a type casting -- it casts the World object to be of type Game (it tells the compiler that the world is indeed a Game object. It can then be placed into a variable that retains a Game object.
TheGoldenProof TheGoldenProof

2018/4/20

#
Once again, it is not working. For whatever reason it mysteriously stopped working. I tried changing it to rightclick to flag a box and it still doesn't work. this is what I have:
public void act() 
    {
        Game game = (Game)getWorld();
        FlagCounter flagger = game.getFlagger();
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse != null) {
            if (Greenfoot.mouseClicked(this)) {
                int button = mouse.getButton();
                if (button == 2 && !isFlagged) {
                    isFlagged = true;
                    setImage(flagged);
                    flagger.flag();
                } else if (button == 2 && isFlagged) {
                    isFlagged = false;
                    setImage(sand);
                    flagger.unflag();
                }else if (button == 1) {
                    revealTiles();
                }
            }
        }
    }
The flagged bombs counter isn't going up so I know its a problem with the clicks. the left click works fine. why is this happening? **EDIT** right click is button 3. For some reason I thought it was button 2.
You need to login to post a reply.