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:
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
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); } } }