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

2017/4/1

Random Background

Fifilein Fifilein

2017/4/1

#
Hi! I have 8 different backgroundimages , and I want a random one of them to change when the game starts and when the score of the Counter goes up.I do not have any ideas to fixe it.Can you help me please? Thank you, Fifilein
Super_Hippo Super_Hippo

2017/4/1

#
private GreenfootImage[] backgroundImages =
{
    new GreenfootImage("bg1.png");
    new GreenfootImage("bg2.png");
    new GreenfootImage("bg3.png");
    new GreenfootImage("bg4.png");
    new GreenfootImage("bg5.png");
    new GreenfootImage("bg6.png");
    new GreenfootImage("bg7.png");
    new GreenfootImage("bg8.png");
}

//when you want a random one
setBackground(backgroundImages(Greenfoot.getRandomNumber(backgroundImages.length)));
Fifilein Fifilein

2017/4/2

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.Font;
//( family=Baskerville Old Face,name=Bakerville Old Face,style=italic,size=22);    
/**
 * Write a description of class StartMenu here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class StartMenu extends World
{
     StartGame sg = new StartGame();
    private GreenfootImage[] backgroundImages =
    {                                    new GreenfootImage("Hintergrund1.png");
                                        new GreenfootImage("Hintergrund2.png");
                                        new GreenfootImage("Hintergrund3.png");
                                        new GreenfootImage("Hintergrund4.png");
                                        new GreenfootImage("Hintergrund5.png");
                                        new GreenfootImage("Hintergrund6.png");
                                        new GreenfootImage("Hintergrund7.png");
                                        new GreenfootImage("Hintergrund8.png");}
    /**
     * Constructor for objects of class StartMenu.
     * 
     */
    public StartMenu()    //level frogfly
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
         setBackground(backgroundImages(Greenfoot.getRandomNumber(backgroundImages.length)));


        getBackground().setFont(new Font("", 0, 60)); 
        getBackground().setColor(Color.black);
        getBackground().drawString("frogfly-Level 1", 140, 50); 
        getBackground().setFont(new Font("",0, 28));
        getBackground().setColor(Color.black);
        getBackground().drawString("Du bist der Frosch und drücke die", 20, 100);
        getBackground().drawString("Pfeiltasten um dich zu bewegen.", 20, 140);
        getBackground().drawString("Sammel alle Fliegen ein um zu gewinnen.", 20, 180);
        getBackground().drawString("Aber pass vor der Schlange auf!", 20, 220);
        {
            addObject(sg, 300, 300 );
         }
       
    }
    
   
}
Fifilein Fifilein

2017/4/2

#
My Problem is now that there are a lot of errormessages. In the beginning of line 15-22 there is illegal start of type In each of this lines at that snip
e("H
I get illegal start of type and <identifier expected. And in line 31-45 I get class, interface or enum expected. What did I do false?
Super_Hippo Super_Hippo

2017/4/2

#
Sorry, it should be this:
private GreenfootImage[] backgroundImages =
{
    new GreenfootImage("Hintergrund1.png"),
    new GreenfootImage("Hintergrund2.png"),
    new GreenfootImage("Hintergrund3.png"),
    new GreenfootImage("Hintergrund4.png"),
    new GreenfootImage("Hintergrund5.png"),
    new GreenfootImage("Hintergrund6.png"),
    new GreenfootImage("Hintergrund7.png"),
    new GreenfootImage("Hintergrund8.png")
};
Fifilein Fifilein

2017/4/2

#
Thank you now I get an errormessage at this line:
 setBackground(backgroundImages(Greenfoot.getRandomNumber(backgroundImages.length)));
It says at the first backgroundImages cannot find symbol-methodbackgroundImages(int)
Super_Hippo Super_Hippo

2017/4/2

#
setBackground(backgroundImages[Greenfoot.getRandomNumber(backgroundImages.length)]);
I should think before posting... ;)
Fifilein Fifilein

2017/4/2

#
danke dir! Jetzt funktioniert es wie es sollte!!
You need to login to post a reply.