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

2014/3/17

Mouse click while not running?

Cubinator Cubinator

2014/3/17

#
I can ask for mouse location and mouse buttons while running. But I'm wondering, if there is a way to check for mouse events while the scenario isn't running?
Pointifix Pointifix

2014/3/17

#
the act method does not run if the scenario is stopped so how would you implement anything that is not done while stopped? Just my solution
Cubinator Cubinator

2014/3/17

#
I thought, it could be possible, that Greenfoot invokes a object's method, when the object is clicked. Or something similar...
danpost danpost

2014/3/17

#
You can invoke a method on an object while the scenario is stopped. Right-click on the object to get a pop-up menu of options to select from. Any public methods within its class should appear among the options. If there are arguments that are needed, a pop-up dialog will appear to enter the appropriate values; then, click the 'OK' button. You can add objects into the world manually, invoke methods manually, and then 'Save the world' (in the Controls option of the menubar) to add (save) those changes to the normal initializing process of the world.
Cubinator Cubinator

2014/3/17

#
I know. But it is very very laborious, if I have to right-click on every cell I want to change, click on setState (function of Cell (my class)), type in "true" and click OK. This is the reason, why I asked for it. I simply want to click a cell and the cell should change its state. When I press on run, the "Game of Life" (http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) begins.
danpost danpost

2014/3/17

#
You could add a method called 'changeState' whereby if it is true, it becomes false; and if it is false, it becomes true. Then you will not get the dialog and have to type 'true' or 'false'. Something like:
public void changeState()
{
    state = !state;
}
danpost danpost

2014/3/17

#
Or, you could have a 'running' and 'stopped' state within the scenario itself and allow clicks on the cells to change their states while the scenario is running but in a 'stopped' state. Have a key like 'enter' or 's' (for start/stop) control the 'running' state within the running scenario.
danpost danpost

2014/3/17

#
I am familiar with programming a 'Game of Life' scenario. Let me just say that your Cell class is best off without an 'act' method (just saying, whether you have one there or not). The world should control the changing of the cells. It is a two-part process (1) determining what all cells are to become and (2) after the new state of all cells has been determined, change them all to their determined new state. If you try to let each cell determine its new state and then change to that state, you will be changing the parameters by which others cells would/should be changing to. Imagine this: x x x x x x On the next frame, the result should be: x o x // outer cells had 3 neighbors each -- they stay on x o x // inner cells had 5 neighbors each -- they go off But, if you change them one at a time, (1) after the first cell x x x // 3 neighbors -- stays on x x x (2) after the second x o x // 5 neighbors -- goes off x x x (3) after the third x o x // 2 neighbors -- stays on x x x (4) after the fourth x o x x x x // 2 neighbors -- stays on (5) after the fifth x o x x o x // 4 neighbors -- goes off (6) the last cell x o x x o o // 1 neighbor -- goes off !! This last step should show what happens if you do not calculate the next state for all cells before changing any.
Cubinator Cubinator

2014/3/18

#
I had the idea with a start/stop within the running scenario before, but I didn't like it, because I though, that there have to be another way. The way that the world should process the states, was my first thought and I implemented it this way, but thanks for helping anyway. I didn't had problems creating this scenario. My only problem is, that Greenfoot doesn't let me trace events or something similar, while the scenario isn't running.
danpost danpost

2014/3/19

#
I had a different idea, but after trying it (although I was able to click multiple cells and have them all change at once), I found that there were too many drawbacks. Keeping proper focus was one issue and having to tab multiple times between clicks on cells was another. It involved a method, like I suggested above, but one that allowed any number of cells to be changed.
public void changeCellStates(Cell... cell)
You need to login to post a reply.