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

2015/2/5

CLICKING a button and creating an actor

KiwiMC1482 KiwiMC1482

2015/2/5

#
Hi, okay so i have a button and a minim, What i want to do is when the button is clicked (either by a button on the keyboard or by the mouse) the minim appears on the screen. Now i have managed to get it so that when a certain key is pressed on the keyboard the button is activated and the minim appears but i am having trouble working out the syntax for when the button is clicked by the mouse. My code in the world class :- public class Background extends World { private boolean clicked; public Background() { super(600, 400, 1); addObject( new Button(), 300, 200); } public void act() { String key = Greenfoot.getKey(); if("g".equals(key)) addObject(new Minim(), 200, 100); } public boolean gotClicked() { if(Greenfoot.mouseClicked(this)) clicked=true; boolean wasClicked=clicked; clicked=false; return wasClicked; } }
danpost danpost

2015/2/5

#
By using 'Greenfoot.mouseClicked(this)' in your Background class (subclass of World), you are checking for any clicks in the world, not on the button. You need to change 'this' to a variable (or field) reference to the button. It was a nice attempt to add the 'clicked' field and the 'gotClicked' method; however, it is a bit round-about (going out of the way). Rather, have a field for the button itself and use it in the 'mouseClicked' method. That is, for example, with the following field:
1
private Actor button;
You can assign the Button object to the field in the constructor when you create it; and then use:
1
if (Greenfoot.mouseClicked(button))
for checking for clicks on the button. In fact, you can combine the two checks in the act method with something like this:
1
if ("g".equals(Greenfoot.getKey()) || Greenfoot.mouseClicked(button))
KiwiMC1482 KiwiMC1482

2015/2/6

#
Ah okay that makes sense, I'll give that a try
KiwiMC1482 KiwiMC1482

2015/2/18

#
Is there a way to make it so only 8 actors are created??? I tried different loops but I don't think I used them correctly....
danpost danpost

2015/2/18

#
Do you want all 8 to be create with one "g" key release? or, do you want "g" to create one at a time and limit the number of Minim objects in the world to 8? If one at a time, then I fail to see where a loop would be needed. You would just add another condition to the 'if' statement:
1
if ( ( "g".equals(Greenfoot.getKey()) || Greenfoot.mouseClicked(button) ) && getObjects(Minim.class).size()<8 )
KiwiMC1482 KiwiMC1482

2015/2/18

#
Yeah one at time, I'm kind of more used to programming in python hence why I thought a loop would be useful. Thanks your help is really appreciated
KiwiMC1482 KiwiMC1482

2015/2/25

#
I added it to my code and do i put addObject( etc at the end of the if statement like i did before or somewhere else???
danpost danpost

2015/2/26

#
KiwiMC1482 wrote...
I added it to my code and do i put addObject( etc at the end of the if statement like i did before or somewhere else???
Yes, of course. That is what is being regulated.
KiwiMC1482 KiwiMC1482

2015/3/5

#
I got that working and i'm now working on a way to delete the most recent minim created but i'm not exactly having much luck on them actually deleting even though my program complies....
danpost danpost

2015/3/5

#
KiwiMC1482 wrote...
I got that working and i'm now working on a way to delete the most recent minim created but i'm not exactly having much luck on them actually deleting even though my program complies....
It sounds like you are asking for help -- without asking for help and without any code to help with.
KiwiMC1482 KiwiMC1482

2015/3/5

#
Okay so far i have
1
2
3
4
if( Greenfoot.getKey().equals("backspace"))
   {
       removeObject( new Mnim());
   }
which complies fine but doesn't actually delete the last minim created and I don't really know what i'm doing wrong
You need to login to post a reply.