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

2014/11/17

Help with 'Greenfoot.mouseClicked(XXX)' command

EIGNERT EIGNERT

2014/11/17

#
Hi, I've been having trouble using the 'Greenfoot.mouseClicked(XXX)' command. Basically, within a world called 'PlayGame', I have defined a variable:
Player thePlayer = new Player();
'Player' is the name of the Actor from where I am defining the variable (not sure if this sentence makes sense but I think you would catch the gist of what I mean) I have placed the variable somewhere in my 'PlayGame' world, like so:
addObject(thePlayer,4,5);
Now, I am trying to check whether someone has clicked on 'thePlayer' from the 'PlayGame' world. This is the code that I tried inside the contructor:
if(Greenfoot.mouseClicked(thePlayer))
{
    //Action goes here
}
This code does not seem to be working. When I click on 'thePlayer', what I want to happen does not happen. Am I doing something wrong? I would really appreciate any help. Thanks
davmac davmac

2014/11/17

#
This is the code that I tried inside the contructor:
It's not clear whether you mean the constructor for Player or PlayGame, but in either case, this seems wrong. If the code is in the constructor it will only run when the instance is created. Usually you want to check for mouse input in the act method, so that the check runs over and over again.
EIGNERT EIGNERT

2014/11/17

#
Hi. Sorry, I didn't specify. I am talking about the 'PlayGame' world. By act method what do you mean? I have placed it where it says:
public PlayGame()
{
    //If statement is in here
}
Is that not correct? Thanks
EIGNERT EIGNERT

2014/11/17

#
There does not seem to be anywhere which says 'act' in the PlayGame world, I can only find that in actors.
danpost danpost

2014/11/17

#
The default World class template does not add the act method to the class as does the Actor class template when you first create the class. However, there is also an empty act method that can be overridden in the World class (just like as in the Actor class). So you can just add it in.
EIGNERT EIGNERT

2014/11/17

#
Ok great, so do I just hardcode it in like I would a method:
Public Void act
{
   //If statement goes here
}
Is that correct? Thanks
danpost danpost

2014/11/17

#
EIGNERT wrote...
Ok great, so do I just hardcode it in like I would a method < Code Omitted > Is that correct?
Precisely.
EIGNERT EIGNERT

2014/11/17

#
Thanks a lot for the help! I really appreciate it.
davmac davmac

2014/11/17

#
Just to clarify, it must be "public void" and not "Public Void" as you've written.
You need to login to post a reply.