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

2021/11/1

So I have a game that I don't get how it works

DavidSaguna DavidSaguna

2021/11/1

#
I made a game in a class, but I have a piece of code which should spawn the ground, under the character to make it look like it's moving but I'm not sure why the boolean is there if it only is true once. Can anyone please explain? Code snippet:
 
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Ground here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Ground extends Actor
{
    boolean canCreate = true;
    public void act()
    {
        move(-4);
        if(getX() + getImage().getWidth()/2 < getWorld().getWidth() && canCreate == true) {
            World world = getWorld();
            Ground ground = new Ground();
            world.addObject(ground, getX()+getImage().getWidth() ,getY());
            canCreate = false;
        }
        if (getX()+getImage().getWidth()/2 < 0){
            getWorld().removeObject(this);
        }
    }
}

If you need the full game, tell me and I will post it. Thanks!
danpost danpost

2021/11/1

#
Line 17 creates a new Ground object whose boolean will be true until another Ground object is created (by this new one). So, each Ground object will create the next one when needed.
DavidSaguna DavidSaguna

2021/11/1

#
danpost wrote...
Line 17 creates a new Ground object whose boolean will be true until another Ground object is created (by this new one). So, each Ground object will create the next one when needed.
Thanks! Have a nice day!
You need to login to post a reply.