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

2016/2/17

If mouseClicked(!null)

RussellBlo RussellBlo

2016/2/17

#
1
2
3
4
5
6
if (Greenfoot.mouseClicked(!null))
{
     Bullet Bullet = new Bullet();
     getWorld().addObject(Bullet, getX(), getY());
     Bullet.setRotation(getRotation());
 }
I am aware that !null is not a piece of working coding because of the operand types, but I would like to know how to do mouseClicked(anywhere). I tried doing it by listing all of the objects used in my world so far (Very inefficient but I will do what I have to).
1
2
3
4
5
6
if (Greenfoot.mouseClicked(getWorld()) || Greenfoot.mouseClicked(Rock.class))
{
     Bullet Bullet = new Bullet();
     getWorld().addObject(Bullet, getX(), getY());
     Bullet.setRotation(getRotation());
 }
Which in this case happens to be world and the Rock object.
danpost danpost

2016/2/17

#
The documentation of the mouseClicked method of the Greenfoot class says this:
If the parameter is null, then true will be returned for any click, independent of the target clicked on.
By specifying null, you are saying that you do not care what is clicked on; you are just wanting to know if the mouse was clicked at all. By the way, null is used as a replacement value that represents the absence of an object and is not a boolean value that can be negated. Its type is whatever type of Object it is replacing. For example:
1
Actor actor = null; // here, null can be considered of type Actor
Consider the pair of methods in the Actor class, 'setImage(GreenfootImage image)' and 'setImage(String filename)'. If you wanted to make an Actor object invisible, you might try this in its constructor:
1
setImage(null);
The compiler will complain because it does not know which method to use. You would need to specify the type for null. For example:
1
setImage((GreenfootImage)null);
Actually, I hadn't tried using this:
1
setImage((String)null);
and cannot say whether the implementation of that method allows a null String.
You need to login to post a reply.