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

2018/3/23

Why doesn't setLocation work?

Recorsi Recorsi

2018/3/23

#
I can shoot actors with lasers. If i hit them the lasers should disappear. I can't remove them completely because then i get NullPointerException errors, so i tried moving them out of sight with "setLocation". But this somehow doesn't work. Do you know why? this is the code i used: - in the laser class:
public void destroyLaser()
    {
        this.setLocation(899,899);
        //getWorld().removeObject(this);
    }
- in the actor class:
private void getHit()
    {
        if (isTouching(Laser.class))
        {
            ((Spielfeld)getWorld()).getLaser().destroyLaser();
            health--;
        }
    }
Thanks :)
Yehuda Yehuda

2018/3/23

#
If you want to remove the laser from the getHit method then place the following instead of line 5:
removeTouching(Laser.class);
It's probably possible to fix the NullPointerException problem, but that's unnecessary. As for the reason for the setLocation problem, you didn't give enough information regarding it for the issue to be resolved.
Recorsi Recorsi

2018/3/23

#
Thank you, now the lasers get removed, but the "health--;" thing doesn't get called.
Yehuda Yehuda

2018/3/23

#
It probably does get called, just depending on what class this code is in, and where you're supposed to see a difference can change what you think about it. It would be easier for others to help you, if you showed the full class code that the getHit method is in.
Recorsi Recorsi

2018/3/23

#
Ok sorry i didn't check it correctly. Now it somehow takes more hits for the health to go down, so i just reduced it. Thanks for your help :)
Yehuda Yehuda

2018/3/23

#
Before we fixed the problem, every act cycle that a laser was touching the Actor that the getHit method is in, 1 would be subtracted from ‘health’. Now that each bullet only remains for one cycle, it only subtracts 1 from ‘health’ in its lifetime.
You need to login to post a reply.