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

2013/6/13

Can't get my background to show.

Alby Alby

2013/6/13

#
This is pretty much my first Greenfoot project that I didn't use the lesson book. I had no problems programming the other three classes, and found them to work perfectly. I them moved on to the background and even though it compiled, it didn't show on the screen. The image for the background is one of the images that Greenfoot gives you. Here's the source code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class BackgroundBrick here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BackgroundBrick extends World
{
    //Arrays that assist the creation of
    //little fishies.
    /**
     * Constructor for objects of class BackgroundBrick.
     * 
     */
    public BackgroundBrick()
    {
        super(600, 600, 1);
        populateWorld();
    }
    
    /**
     * Add the necesary components to the world.
     */
    public void populateWorld()
    {
        int i = 0;
        addObject(new Seal(), (Greenfoot.getRandomNumber(600)), (Greenfoot.getRandomNumber(600)));
        while (i < 3)
        {
            addObject(new PolarBear(), (Greenfoot.getRandomNumber(600)), (Greenfoot.getRandomNumber(600)));
            i = i + 1;
        }
        i = 0;
        int x = (Greenfoot.getRandomNumber(20) + 1);
        while (i < x)
        {
        }
    }
}
Please help, because I can't do anything otherwise :P Thank you!!!
Alby Alby

2013/6/13

#
oh sorry i didn't change the class description.
Gevater_Tod4711 Gevater_Tod4711

2013/6/13

#
I don't realy get what is the problem: - What is the world doing? - What should it be doing?
Alby Alby

2013/6/13

#
The world just isn't there. This image shows all of the classes compiled, and no background.[Disallowed URL]
Alby Alby

2013/6/13

#
wait that didn't work. Either way, it compiles with no errors, but shows nothing for the background
Gevater_Tod4711 Gevater_Tod4711

2013/6/13

#
Hmm thats strange. I think with this problem only the greenfoot team can help you.
Busch2207 Busch2207

2013/6/13

#
Well, if it don't show the world, try to klick right at your BackgroundBrick - class and then select 'new BackgroundBrick()'!
tollpatch tollpatch

2013/6/13

#
This are the last lines of your code:
i = 0; int x = (Greenfoot.getRandomNumber(20) + 1); while (i < x) { }
from my point of view: This is an endless loop, because x > 1 and i = 0. may be there is missing code within the methode brackets?
Gevater_Tod4711 Gevater_Tod4711

2013/6/13

#
Oh yes that is an endless loop. That probably is the problem because in Greenfoot you shouldn't use endless loops. If you don't need to execute any code in the while loop why do you use it? You can either delete it or add i++; into the brackets.
Alby Alby

2013/6/14

#
I fixed the endless loop, and removed the method call for it from the constructor, so it means nothing. Here is the constructor, and it doesn't work:
/**
     * Constructor for objects of class BackgroundBrick.
     * 
     */
    public BackgroundBrick()
    {
        super(600, 600, 1);
    }
I also checked in the file, and everything is in place, with nothing missing
danpost danpost

2013/6/14

#
If you do not see the 'brick' image next to the name of your world in the 'World' tree to the right of your scenario window, then you have not properly set the image of that class. Right click on the icon (the rectangle with 'BackgroundBrick' in it) and select 'set image...'; click on 'backgrounds...' under 'Image categories:' and click on the image you want under 'Library images:'; then, click the 'OK' button.
Alby Alby

2013/6/15

#
I know how to set the image and have already done that. I will now copy my method for populating the world and reset the source code to see if that helps because this is crazy :P
Alby Alby

2013/6/15

#
Ok. You guys have been awesome about helping me, but I reset the background entirely and it still won't work, so here's the source code of my other classes. NPC: -------------------------------------------
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class NPC here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class NCP extends Actor
{
    /**
     * Act - do whatever the NPC wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    }
    
    /**
     * Check to see if you are at the world's
     * edge. Return true of false.
     */
    public boolean atWorldEdge()
    {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20)
            return true;
        if(getY() < 20 || getY() > getWorld().getHeight() - 20)
            return true;
        else
            return false;
    }
    
    /**
     * Check to see if there's something
     * to munch on.
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;
    }
    
    /**
     * Munch on something
     */
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null)
        {
            getWorld().removeObject(actor);
        }
    }
}
Seal: -------------------------------
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class defines a seal. The seal will search out
 * fish to eat and follow your directions. If a fish
 * interacts with a shark it will be eaten.
 * 
 * Use W, A, S, D to control and hold down space
 * and press Q to increase speed and E to decrease
 * it.
 * 
 * Author: A. Wilcox III
 * Date created: 6/12/2013
 */
public class Seal extends NCP
{
    private int speed;
    private boolean isQDown;
    private boolean isEDown;
    /**
     * Act - do whatever the Seal wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        moveAndTurn();
        changeSpeed();
        lookForFish();
    }
    
    /**
     * Creation of zee seal
     */
    public Seal()
    {
        isQDown = false;
        isEDown = false;
        speed = 4;
    }
    
    /**
     * Control the seal.
     */
    public void moveAndTurn()
    {
        //move Down
        if (Greenfoot.isKeyDown("s"))
        {
            setRotation(90);
            move(speed);
        }
        //move Up
        if (Greenfoot.isKeyDown("w"))
        {
            setRotation(270);
            move(speed);
        }
        //move Left
        if (Greenfoot.isKeyDown("a"))
        {
            setRotation(180);
            move(speed);
        }
        //move Right
        if (Greenfoot.isKeyDown("d"))
        {
            setRotation(0);
            move(speed);
        }
    }
    
    /**
     * Adjust the seal's speed
     */
    public void changeSpeed()
    {
        if(Greenfoot.isKeyDown("space"))
        {
            //Increase the speed
            if (!isQDown && Greenfoot.isKeyDown("q"))
            {
               speed = speed + 1;
               isQDown = true;
            }
            if (isQDown && !Greenfoot.isKeyDown("q"))
            {
                isQDown = false;
            }
            //Decrease the speed
            if (!isEDown && Greenfoot.isKeyDown("e"))
            {
                speed = speed - 1;
                isEDown = true;
            }
            if (isEDown && !Greenfoot.isKeyDown("e"))
            {
                isEDown = false;
            }
        }
    }
    
    /**
     * Check to see if there is a
     * fish to munch on, and if
     * there is, eat it.
     */
    public void lookForFish()
    {
        if(canSee(Fish.class))
        {
            eat(Fish.class);
            Greenfoot.playSound("slurp.wav");
        }
    }
}
Fish: ------------------------------------------
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Fishes randomly spawn with random coloration.
 * The fish's colorationd determines its
 * movement patterns.
 * 
 * Author: A. Wilcox III 
 * Date created: 6/12/2013
 */
public class Fish extends NCP
{
    private String fishLook;
    private int fishBehavior;
    /**
     * Act - do whatever the Fish wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (fishBehavior == 1)
        {
            slowMoving();
        }
        if (fishBehavior == 2)
        {
            fastMoving();
        }
    }
    
    public Fish(String fishLookFile, int fishBehavior2)
    {
        fishLook = fishLookFile;
        fishBehavior = fishBehavior2;
        setImage(fishLook);
    }
    
    /**
     * The slow-moving fish
     */
    public void slowMoving()
    {
        move(2);
        if (Greenfoot.getRandomNumber(5) < 1)
        {
            turn(Greenfoot.getRandomNumber(90) - 45);
        }
        if (atWorldEdge())
        {
            turn(45);
        }
    }
    
    /**
     * The fast-moving fish
     */
    public void fastMoving()
    {
        move(8);
        if (Greenfoot.getRandomNumber(5) < 1)
        {
            turn(Greenfoot.getRandomNumber(90) - 45);
        }
        if (atWorldEdge())
        {
            turn(45);
        }
    }
}
Polar Bear: ----------------------------------------------------
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class PolarBear here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class PolarBear extends NCP
{
    /**
     * Act - do whatever the PolarBear wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(3);
        randomTurn();
        lookForSeal();
        turnAtEdge();
    }
    
    /**
     * Randomly turn
     */
    public void randomTurn()
    {
        if (Greenfoot.getRandomNumber(5) < 1)
        {
            turn(Greenfoot.getRandomNumber(90) - 45);
        }
    }
    
    /**
     * Check to see if there's a seal to
     * eat, and if there is, eat it!
     */
    public void lookForSeal()
    {
        if (canSee(Seal.class))
        {
            eat(Seal.class);
            Greenfoot.playSound("au.wav");
        }
    }
    
    /**
     * Turn if the bear is touching the
     * edge of the world
     */
    public void turnAtEdge()
    {
        if (atWorldEdge())
        {
            turn(45);
        }
    }
}
------------------------------------------------ you guys have really helped alot, and if this doesn't make me helpable, nothing will, so thanx
danpost danpost

2013/6/15

#
Try this for your world class code:
import greenfoot.*;

/**
 * Write a description of class BackgroundBrick here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BackgroundBrick extends World
{
    /**
     * Constructor for objects of class BackgroundBrick.
     */
    public BackgroundBrick()
    {
        super(600, 600, 1);
        populateWorld();
    }
    
    /**
     * Add the necessary components to the world.
     */
    public void populateWorld()
    {
        addObject(new Seal(), (Greenfoot.getRandomNumber(600)), (Greenfoot.getRandomNumber(600)));
        int i = 0;
        while (i < 3)
        {
            addObject(new PolarBear(), (Greenfoot.getRandomNumber(600)), (Greenfoot.getRandomNumber(600)));
            i = i + 1;
        }
        int x = (Greenfoot.getRandomNumber(20) + 1);
        i = 0;
        while (i < x)
        {
            // I think you might have wanted to do something here (maybe adding some fish)
            i = i + 1;
        }
    }
}
Alby Alby

2013/6/19

#
:O it works thnx man!!!!!!!!!!!!
You need to login to post a reply.