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

2012/1/17

Using mousePressed

Mikeson Mikeson

2012/1/17

#
Hi Im trying make an event occur when an object is clicked on by the mouse so far I have put doesn't work. public void act() { if(Greenfoot.mousePressed("button1")) { triger (); } } public void triger() { { Unarmed unarmed = new Unarmed(); getWorld().addObject(new Unarmed(), getX(), getY()+5); getWorld().removeObject(this); } Thou if I chabge it to isKeyDown("space") it seems to work. Also If I have two events that occuer on space or mouse press should they occuer at the same time as in another class I have, public void triger() { life--; if(life==0) { placeDebris(getX(),getY(),NUM_FRAGMENTS); getWorld().removeObject(this); } } private void placeDebris(int x, int y, int NUM_FRAGMENTS) { for(int i = 0; i < NUM_FRAGMENTS; i++) { getWorld().addObject(new Debris(), x, y); } } public void act() { if(Greenfoot.mousePressed("button1")) { triger(); } // Add your action code here. } also this seems to work when the mouse pressed is changed to isKeyDown space thou it seems not to work on one space press. ie on first space press the first block of code on this page works and then on the second one works so does the next section of code as I was trying to intnate thes two objects at the same time. Thanks in advance for your responce.
marzukr marzukr

2012/1/17

#
try
   if(Greenfoot.mouseClicked("button1"))
Mikeson Mikeson

2012/1/17

#
Ive tried mouse click to and it doesn't work ether.
Builderboy2005 Builderboy2005

2012/1/17

#
mouseClicked doesn't take a string as its argument, it takes an Actor. Try this instead:
if(Greenfoot.mousePressed(this))
The word 'this' in java refers to the current object, so that statement will be true whenever the user clicks on that particular object.
Mikeson Mikeson

2012/1/18

#
Thats Builder boy that the trick. and thanks for the refernce as Im only using greenfoot to learn java.
You need to login to post a reply.