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

2013/1/17

How to change the background if an actor hits the edge of the world?

1
2
bbwf bbwf

2013/1/18

#
This was the problem I was having. Where do I put line 52?
danpost danpost

2013/1/18

#
For what purpose did you add that line? what were you trying to accomplish? (be specific) NVM, its the main topic of the discussion, will post back momentarily.
bbwf bbwf

2013/1/18

#
This is what I'm trying to accomplish. When my actor (bob) hits either the right edge of the left edge of my world (called Background) I want bob to switch worlds into a new Background. My entire goal of this is to make bob walk from his house to the store, so he has to bassicaly switch sides when he hits the edge untill he gets to the store. Didn't see you edited it, here's a more deatiled idea anyways.
bbwf bbwf

2013/1/18

#
Anything?
danpost danpost

2013/1/18

#
Modifification of your 'atWorldEdge' method follows:
public boolean atWorldEdge()
{
    return (atHorizontalEdge() || atVerticalEdge());
}

private boolean atVerticalEdge()
{
    return (getX()<20 || getX()>getWorld().getWidth()-20);
}

private boolean atHorizontalEdge()
{
    return (getY()<20 || getY()>getWorld().getHeight()-20);
}
Now you will be able to check them individually (left and right; or, top and bottom). Then in your main code where you use 'if(atWorldEdge())', you can do something like
if(atWorldEdge())
{
    if(atSideEdge())
    {
        getWorld().setBackground("lol.png");
        int w=getWorld().getWidth();
        setLocation((getX()+w-60)%(w-40)+20, getY());
    }
    else { }// if you need one
}
The formula in the x value is simplified from this (using 'x' for 'getX()'): ((x-20)+((w-20)-20))%((w-20)-20)+20 If the limits of the actor where zero and 'getWidth()-1' (the world edges themselves), then it would look like this: (x+w)%w The '%' is the modulus operator, returning the remainder of taking what is on the left and dividing it by what is on the right. To built up to that logic: You should be able to see what happens with a simple range, let us say 0 to 10. If your value was -1, adding the upper limit to ensure the value is not negative gives 9, and the remainder of 9/10 is 9. If your value was 11, adding the upper limit this time gives 21, and the remainder of 21/10 is 1. This method basically connects the two ends of your range so that going off one end brings you back in at the other. If the lower limit is not zero, the we must first adjust everything so that it is zero and afterwards, adjust back.
bbwf bbwf

2013/1/19

#
Thanks, it worked. Now how would I have it change again if hit at world edge on the new world? I'm basically making levels kind of.
danpost danpost

2013/1/19

#
Since the code is in the class of the actor in question, it should work no matter what world it is in.
bbwf bbwf

2013/1/19

#
Yes but I want it to change worlds after it hits the second world, so after it goes into a new world I want it to change into a NEW world if it hits the edge of the second world
danpost danpost

2013/1/19

#
Then you need to add and pass a value that tracks which world you are in, and use that value to determine which world to go to.
You need to login to post a reply.
1
2