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

2016/2/18

Looping Leaves Falling from a tree

Altyrn_The_Dreamer Altyrn_The_Dreamer

2016/2/18

#
Hey there, Fairly new to java and I'm trying to loop leaves falling from a tree. I have it so that the leaves randomly generate across the x,y axis and when they hit they ground they are removed. How do I get them to respawn after being removed so that they can sort of infinitely fall. Thanks!
Super_Hippo Super_Hippo

2016/2/18

#
Situation A: You always want to keep the same number of leaves on your screen. Solution A1: When a leaf reaches the ground, you use 'setLocation(getX(), 0)' or 'setLocation(Greenfoot.getRandomNumber(getWorld().getWidth()), 0)' to let the leaf start at the top again. Solution A2: When a leaf reaches the ground, it calls a world method to spawn a new leaf and removes itself.
1
2
((WorldName)getWorld()).addLeaf();
getWorld().removeObject(this);
1
2
3
4
public void addLeaf()
{
    addObject(new Leaf(), Greenfoot.getRandomNumber(getWidth()), 0);
}
Or just
1
2
addObject(new Leaf(), Greenfoot.getRandomNumber(getWorld().getWidth()), 0);
getWorld().removeObject(this);
Situation B: It doesn't matter how many leaves are on the screen. Solution B: Then you can randomly spawn leaves from the world's act-method. When a leaf reaches the ground, it is simply removed with 'getWorld().removeObject(this)'.
1
2
3
4
5
6
7
public void act()
{
    if (Greenfoot.getRandomNumber(100)==0) //about every two seconds → higher number=less leaves
    {
        addObject(new Leaf(), Greenfoot.getRandomNumber(getWidth()), 0);
    }
}
Altyrn_The_Dreamer Altyrn_The_Dreamer

2016/2/20

#
Great! I went with the option of having the leaf reset to the x,y coordinate! On another note I have my character side scrolling by holding down either the right or left arrow and I have a footstep .wav file. When I hold the key down it makes this god awful sound like its overlapping the sound over and over. I just want the sound to loop when walking not sound scary.
danpost danpost

2016/2/20

#
Altyrn_The_Dreamer wrote...
I have my character side scrolling by holding down either the right or left arrow and I have a footstep .wav file. When I hold the key down it makes this god awful sound like its overlapping the sound over and over. I just want the sound to loop when walking not sound scary.
Do not use the 'playSound' method of the Greenfoot class. There is no way to control or check the status of a sound created with that method. Create a GreenfootSound object and use the methods of the GreenfootSound class to control the sound.
You need to login to post a reply.