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

2012/4/20

World/Class problem

al_griff al_griff

2012/4/20

#
Hey there, So for school I have to replicate the game Frogger, and I'm having trouble getting my vehicles to return to the other side of the world when they hit the edge. Would anyone be able to help me with that. Also another problem I'm having is that I'm trying to get the frog to be put in a spot when the game runs, but then when you go to move the frog I get massive glitch like in this
davmac davmac

2012/4/20

#
Nobody can help without seeing your code! So: post your code. (Not necessarily all of it - just the parts that aren't working how you want).
al_griff al_griff

2012/4/20

#
This is the code for my world
public class FrogWorld extends World
{

    
    public FrogWorld()
    {    
        super(550, 778, 1); 
    }
    
    public void populate()
    {
        Frog f1 = new Frog();
        addObject(f1, 275, 725);
    }
    
    public void act ()
    {
      populate();
      
    }
}
And then the code for my frog is
    public void act() 
    {
        checkKeys();
    }   
    
    private void checkKeys()
    {
        if (Greenfoot.isKeyDown("up")) {
            setLocation(getX(), getY() -15); //a negative value makes it go up along the y-axis
        }
        if (Greenfoot.isKeyDown("down")) {
            setLocation(getX(), getY() +15); //a positive value makes it go down along the y-axis
        }
        if (Greenfoot.isKeyDown("left")) {
            setLocation(getX() -5, getY() ); //a negative value makes it go left along the x-axis 
        }
        if (Greenfoot.isKeyDown("right")) {
            setLocation(getX() +5, getY() ); //a positive value makes it go right along the x-axis
        }
    }
al_griff al_griff

2012/4/20

#
Sorry about the triple post, I don't know why it did that
danpost danpost

2012/4/20

#
Look at your world class act() method and think: What happens when act() is called repeatedly? Do I want to populate() the world repeatedly? Can you see where the 'glitch' is? As far as the cars, you asked a question: ask yourself, how can I write that question in Java code (what code is available to execute what I want to happen)? You basically ask: if (hit the edge) changeLocation(otherSideOfWorld). What is true only at the edge, how can the location be changed, and what is the other side of the world? The correct answers to these questions should lead you to the code you want.
al_griff al_griff

2012/4/20

#
alright cheers man. Yeah I knew that running the populate method caused the glitch, but I was hoping that there would be a way to get that to happen without the glitch?
davmac davmac

2012/4/20

#
... but I was hoping that there would be a way to get that to happen without the glitch?
Get what to happen? The "glitch" is that you're calling populate() from act(). The act() method gets called repeatedly as the scenario runs (that's what it's meant for). So you're constantly adding a new frog into the world. You need to call populate() from a different place.
IsVarious IsVarious

2012/4/21

#
There are a few tips here, first I would put a call for your "populate(); inside your world's constructor. That way as the world is setup, it places one in there, and you can free up act for other important things. Also, I would check through the greenfoot book and look at the asteroids code. The meteors have a world wrap code which enables them to wrap the screen. I would modify that.
IsVarious IsVarious

2012/4/21

#
So by adding the populate method to the constructor, your code would look like this...
public class FrogWorld extends World  
{        
    public FrogWorld()  
    {      
        super(550, 778, 1);
        populate();
    }  
      
    public void populate()  
    {  
        Frog f1 = new Frog();  
        addObject(f1, 275, 725);  
    }  
      
    public void act ()  
    {       
        
    }  
}  
al_griff al_griff

2012/4/21

#
cheers man. Yeah that makes more sense now. I did have one question. With the glitch that was happening why was there always a constant line, shouldn't there just be the moving frog, and then the populated frog?
danpost danpost

2012/4/21

#
What was happening was this: you added a frog at start-up; when the program was started, frogs were already being replicated (one on top of another, appearing like just one frog still); then when you moved a frog, you would see its head above the other frogs, and a new frog would be created at that spot, then moving again, and another frog created, etc. Each act() cycle would allow movement AND create another frog, because you had populate in the act() method, which executes once every cycle.
al_griff al_griff

2012/4/22

#
Oh ok that makes a bit of sense. Thanks for that :)
You need to login to post a reply.