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

2017/4/25

Make Backgrounds change in order

AngelusNeko13 AngelusNeko13

2017/4/25

#
I've been working on a day/night cycle when I started this game but now I want to add different backgrounds, the Idea is to make my game look like the player is traveling around the world, the world only has two images it uses to change backgrounds or in other words two functions for the day and night time, well now i want to add a third background to change the image from the night time image to the next day time image. The idea is to switch background images in order. my issues is that when i do the cycling, two of the images are stuck together, these are night() and day2(), here's the coding for the timing of each image
 public boolean isDay()
    {
        return (time/10000)%2==0;
    }
and here's the if statements of switching the images
  if ( isDay() )
        { 
            day();
        }
        else if (isDay()) 
        {
            night();
        }
        else
        {
            day2();
        }
help plz
Super_Hippo Super_Hippo

2017/4/25

#
So you want to have the images day (for day 1), night (for night 1) and day2 (for day 2)? Is day3=day1, day4=day2 then and so on? Right now, line 1 and line 5 has the exact same condition, so either it is day (line 1 is true) and line 5 isn't executed or it is not day and both are condition are not met.
AngelusNeko13 AngelusNeko13

2017/4/25

#
exactly. Day= day1 night = night1 day 2 = day2 night 2= night2 day 3 =day3
AngelusNeko13 AngelusNeko13

2017/4/25

#
when it turns into night time, or night(), both night() and day() are happening at the same time, i need it to where i can add infinite amount of images and put them where they can go in the order i put them in
Super_Hippo Super_Hippo

2017/4/25

#
Maybe something like this would work:
private int time = 0;

private int currentImage = 0;

private GreenfootImage[] images =
{
    new GreenfootImage("day1.png");
    new GreenfootImage("night1.png");
    new GreenfootImage("day2.png");
    new GreenfootImage("night2.png");
    new GreenfootImage("day3.png");
    new GreenfootImage("night3.png");
    //....
};

public MyWorld()
{
    super(.....);
    setBackground(images[0]);
    //...
}

public void act()
{
    if (++time % 20000 == 0)
    {
        currentImage++;
        setBackground(images[currentImage]);
    }
}
AngelusNeko13 AngelusNeko13

2017/4/25

#
will it loop after it gets to the last image?
AngelusNeko13 AngelusNeko13

2017/4/25

#
oh wait, im sorry. i misspoke the function, the day and night aren just images, but they're also their own functions, the day and night cycle each have their own functions like location of actors, here's the coding of the day and night cycle
 public void night()

    {
        setBackground("roadBack7.png");
       
        if (time % 10 == 1 )
        {
            addObject(new Tree2(),Greenfoot.getRandomNumber(15)+474,0);
        }
        if (time % 10 == 1 )
        {
            addObject(new Tree2(),Greenfoot.getRandomNumber(5)+132,0);
        }
        if (time % 15 == 1 )
        {
            addObject(new House2(),Greenfoot.getRandomNumber(5)+30,0);
        }
        if (time % 20 == 1 )
        {
            addObject(new Stick2(),111, 0);
        }
       
    }  

    public void day()
    {
        setBackground("roadBack.png");
        
      
        if (time % 10 == 1 )
        {
            addObject(new Tree(),Greenfoot.getRandomNumber(15)+474,0);
        }
        if (time % 10 == 1 )
        {
            addObject(new Tree(),Greenfoot.getRandomNumber(5)+132,0);
        }
        if (time % 15 == 1 )
        {
            addObject(new House(),Greenfoot.getRandomNumber(5)+30,0);
        }
        if (time % 20 == 1 )
        {
            addObject(new Stick(),111, 0);
        }
        
    }    
    public void day2()
    {
        setBackground("roadBack3.png");
        
      
        if (time % 10 == 1 )
        {
            addObject(new Tree(),Greenfoot.getRandomNumber(15)+474,0);
        }
        if (time % 10 == 1 )
        {
            addObject(new Tree(),Greenfoot.getRandomNumber(5)+132,0);
        }
       
        
        
    }    
AngelusNeko13 AngelusNeko13

2017/4/26

#
is there a way i can make the function change in the order i want them to?
You need to login to post a reply.