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

2019/11/6

Problem with Limiting the use of a function

PatrikBurris PatrikBurris

2019/11/6

#
Im fairly new to programming and seem not to find the solution to this simple problem and I am despairing at it. I am trying to recreate the game Battleships. I have a bunch of "fields" created from the class "Field" by the world "blue". Once they are all in place they are clickable. When clicked they get exchanged by an Object created by the class "redField". Nothing more. The step I seem not to get is: I want only three "Fields" to be clicked, then they should not interact when clicked anymore. Here the Code:
import greenfoot.*;
public class Field extends Actor
{
    public Field(){
        getImage().scale(getImage().getWidth()*4, getImage().getHeight()*4);
    }

    public void act() 
    {        
        replace();
    }    

    public void replace(){
        if (Greenfoot.mouseClicked(this)){
            int fieldX = this.getX();
            int fieldY = this.getY();
            redField fieldRed = new redField();
            getWorld().addObject(fieldRed,fieldX,fieldY);
            getWorld().removeObject(this);
        }
    }
}
I already tried adding a Variable that gets incremented every time the function replace() is called and then simply checking in the act() function if this variable is below 3. Didnt work. (To be clear: The Variable did not increase) Then I tried to add the variable to the world. My toughts were that maybe the variable was exclusive to every object and when the object gets deleted the Progress gets lost. Did not work either. The Internet was not helpful either. Maybe someone out here can finally help me
Super_Hippo Super_Hippo

2019/11/6

#
I am not exactly sure why you want to limit it to three clicks, but this should do that:
private static int clickedFields;
public static void resetClickedFields() {clickedFields = 0;} //make sure you call this method from the constructor of your world by using “Field.resetClickedFields();”

public void replace()
{
    if (clickedFields < 4 && Greenfoot.mouseClicked(this))
    {
        getWorld().addObject(new redField(), getX(), getY());
        clickedFields++;
    }
}
danpost danpost

2019/11/6

#
PatrikBurris wrote...
My toughts were that maybe the variable was exclusive to every object and when the object gets deleted the Progress gets lost. Did not work either.
That would be correct. The field, as you had it, was an instance variable. Each instance of Field will have its own independent variable to use. Since you are dealing with multiple instances and you want one field (one value) to be used for ALL instances, you can use a class field (as opposed to an instance field). You declare a class field by adding the static modifier to the variable:
public static int numReplaced;
PatrikBurris PatrikBurris

2019/11/6

#
Im not sure why and how but im very thankful for your fast and working replay. Edit: Thank you fo the Info. In the future this will definetly help me
danpost danpost

2019/11/6

#
Another way might be the following check (without having to add a field or anything extra):
if (Greenfoot.mouseClicked(this) && getWorld().getObjects(redField.class).size() < 3)
PatrikBurris PatrikBurris

2019/11/6

#
Does the function size() describe the amount of Objects?
danpost danpost

2019/11/6

#
PatrikBurris wrote...
Does the function size() describe the amount of Objects?
It returns the number of objects that the returned list contains (the number of redField objects currently in the world, in this case).
PatrikBurris PatrikBurris

2019/11/6

#
Thank you very much for all the help
You need to login to post a reply.