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

2019/3/14

Need a solution to inefficient code

Andrew0739 Andrew0739

2019/3/14

#
I want to use this code to check if the player is touching an object. The problem is that i am going to have so many objects that creating a new method like this for ever object would be inefficient. I was thinking maybe i could use getObjectsInRange in place of the fence.class but i'm not sure how to implement this. If this doesn't work i am open to any to any suggestions. Thanks
public boolean bottomTouching()
    {
        Actor fenceCheck = getOneObjectAtOffset(0, getImage().getHeight()/2-20, fence.class);
        return fenceCheck != null;
    }
danpost danpost

2019/3/14

#
You could simply use Actor.class:
public boolean bottomTouching()
{
    return getOneObjectAtOffset(0, getImage(),getHeight()/2-20, Actor.class) != null;
}
Andrew0739 Andrew0739

2019/3/14

#
ok really? thanks!
You need to login to post a reply.