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

2013/5/10

Null Pointer Exception with danpost's ScrollWorld

I'm trying to use danpost's ScrollWorld and when I call getUnivX() I get a null pointer exception. I'm not sure why. Here's my code where I use it:
protected void addGround()
    {
        int x = getUnivX(24); //Where I get the error

        for(Ground g : newGround()) //newGround() makes an ArrayList of Ground based on the width of the world (yes it works)
        {
            addObject(g, x, getHeight() - 24, true);
            x += 48;
        }
    }
Thanks for any help!
Other info: I have tried using this when you call the world's constructor, and also if after the first frame, to see if it was something with the constructor, and it was the same result.
I've also just tried making a new working world and trying the method from there, and I've got a null pointer exception. I think it's just something with his code, and not mine (hopefully).
Nevermind, I just found the error, I don't have a background image, the background image is required for the univX.
How would I fix getUnivX() to work without a background image (if possible)?
danpost danpost

2013/5/10

#
I have not tried it, but since the background is scaled to the scrollingWidth and scrollingHeight, both multiplied by the cellsize, you should be able to change the methods to the following:
public int getUnivX(int worldX)
{
    return (scrollingWidth-getWidth())/2+worldX+scrolledX;
}
    
public int getUnivY(int worldY)
{
    return (scrollingHeight-getHeight())/2+worldY+scrolledY;
}
danpost danpost

2013/5/10

#
Yes. I have tried it, and it works. I have updated my scenario to allow those methods to be used without setting a background image.
Thanks!
You need to login to post a reply.