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

2024/1/7

Mouse clicked object

Odiak Odiak

2024/1/7

#
I want to have multiple levels. I am using a switch statement for that. My problem is the selection of the levels. I created objects as buttons for the level selection. They were supposed to be selected with a mouse click. I am kind stuck. The other all parts work. The mistake has to be within these lines and I'm not sure where. If anyone could help I would be thankful.
//adds object levelOneButton
Actor buttonOne = new levelOneButton();
addObject(buttonOne, 250,400);

//clicking the button is supposed change the level variable
if(Greenfoot.mouseClicked(buttonOne))
{
removeObjects(getObjects(null));
level = 2;
}
danpost danpost

2024/1/8

#
Odiak wrote...
I want to have multiple levels. I am using a switch statement for that. My problem is the selection of the levels. I created objects as buttons for the level selection. They were supposed to be selected with a mouse click. I am kind stuck. The other all parts work. The mistake has to be within these lines and I'm not sure where. If anyone could help I would be thankful. << Code Omitted >>
Line 1 must not be in the same block as line 2. It must be outside the block that line 2 is in. Specifically, it must be at top level within the class. The reason: if it is the line before line 2, the reference, buttoneOne, does not extend beyond the block those lines are in; and if line 1 is outside the constructor, the reference remains for as long as this World object exists.
You need to login to post a reply.