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

2014/12/9

how to setLocation of a different class?

mullac51 mullac51

2014/12/9

#
I know of setLocation(100,100); but how do i say setLocation of a different class? setLocation(player.class 940,70)??? I hope i've explained it well enough. Thanks.
davmac davmac

2014/12/9

#
You don't set the location of a class - you set the location of an object. You can set the location of any object that you have a reference to by qualifying the setLocation method call. For instance if you have a variable 'otherActor' containing a reference to the object you want to set the location of, you can call:
1
otherActor.setLocation(100,100);
If you don't know how to obtain a reference to the other actor, start with tutorial #6 and then ask further questions here if necessary.
mullac51 mullac51

2014/12/9

#
so far i've got this? but i get a terminal window popping up with red java errors.
1
2
3
4
5
6
7
8
public void hit()
    {
        Actor ball;
        ball=getOneObjectAtOffset(0,0,GoldenBall.class);
        {
            ball.setLocation(100,100);
             
        }
danpost danpost

2014/12/9

#
The good ol' NullPointerException throw. If no GoldenBall object is intersecting the location at the offset given, 'ball' will be set to 'null'. You cannot set the location of a 'null' object reference. You must ensure that 'ball' is not 'null' before using 'setLocation' on it:
1
2
3
4
if (ball != null)
{
    ball.setLocation(100, 100);
}
You need to login to post a reply.