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

2013/5/10

Using getUnivX() (danpost's SWorld)

I'm not sure if I'm using getUnviX() method correctly. I have a method that creates ground along the bottom of the world, it should start from the beginning to the end of the scrolling world. For some reason it produces this: https://www.dropbox.com/s/17t5s0cdnpkvkz9/Capture.png Here's my ground adding code:
protected void addGround()
    {
        int x = getUnivX(24); //Am I using this correctly?

        for(Ground g : newGround()) //new Ground() makes an ArrayList of Ground
        {
            addObject(g, x, getHeight() - 24, true);
            x += 48;
        }
    }
danpost danpost

2013/5/11

#
No; you are not. When adding an object to the world, you need world coordinates; not universal coordinates. It may not seem correct, but you can do the following with the method you have:
// change
int x = getUnivX(24); 
// to
int  x = 24-getUnivX(0);
// or
int x = -getUnivX(-24);
To explain: (1) when using the 'addObject' method, the int parameters take world coordinates (2) a world coordinate off the left side of the world will be a negative number (3) the scrolling x-coordinate of the world left edge is returned by 'getUnivX(0)' (this value is never negative) (4) the world x-coordinate of the scrolling left edge is returned by '-getUnivX(0)' (5) adding 24 to (4) will give the first world location to place a ground object Another alternate you may wish to consider, is creating one ground object to cover the whole scrolling width. You can refer to the Ground class of the Scrolling SuperWorld scenario.
Thank you. That worked perfectly! I was thinking of doing your alternate way (having the one ground object) but the way I'm writing my game requires separate ground objects. (Each ground object changes color, and I'd like each block to be different)
Alright, now that I have that working, how would I get the right edge of the world? Do I use getUnivX for that? Or is there a different way?
danpost danpost

2013/5/11

#
I added the following methods to the 'SWorld' class to help in that matter. They are
public int getScrollingWidth()
{
    return scrollingWidth;
}
    
public int getScrollingHeight()
{
    return scrollingHeight;
}
danpost danpost

2013/5/11

#
To get the x value in world coordinates of the far right side of the scrolling area, then with the new methods, you can use:
int x = getScrollingWidth()-getUnivX(0);
// or
int x = -getUnivX(-getScrollingWidth());
You need to login to post a reply.