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

2019/5/6

Falling Objects

FortniteMaster69 FortniteMaster69

2019/5/6

#
I know how to make objects fall from the sky and how to get it to spawn in a random spot in the X axis. However, how do I make the same object fall at different times. For example I have balls at the top and I want one ball to fall first and the other to drop like 1 second later or any time frame after the first. The balls are also in the same class.
Scorcher Scorcher

2019/5/6

#
I made a basic program to help, here is the code for my ball.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import greenfoot.*;
 
public class Ball extends Actor
{
    public int fallTime=0;     //The time until the ball fall, set in the world constructor.
    private int actCount=0;
     
    public void act()
    {
        if(actCount>fallTime)
        {
            setLocation(getX(),getY()+1);      //This is where your falling code would go.
        }
        actCount++;
    }   
}
Code for the world, with 3 sample ball created that each fall at a different time.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import greenfoot.*;
 
public class MyWorld extends greenfoot.World
{
    public MyWorld()
    {   
        super(600, 400, 1);     //Set your own world size to here.
         
        Ball ball1 = new Ball();
        ball1.fallTime = 50;          //This sets the delay until the ball falls, I have mine going 50, 100, and 150, but you can customize it.
        addObject(ball1, 200, 0);
         
        Ball ball2 = new Ball();
        ball2.fallTime = 100;
        addObject(ball2, 300, 0);
         
        Ball ball3 = new Ball();
        ball3.fallTime = 150;
        addObject(ball3, 400, 0);
    }
}
You can copy and paste this or just make changes to your own code. Hope it helps!
FortniteMaster69 FortniteMaster69

2019/5/7

#
Yeap thank you very much it worked. Is there a way so that I can make it that my ball comes out from the sky because right now the ball is right at the top of the screen and the user can see the ball which I don't want them to?
Scorcher Scorcher

2019/5/8

#
Your Ball code would only need to have the falling speed, not anything else I wrote earlier:
1
2
3
4
5
6
7
public class Ball extends Actor
{
    public void act()
    {
        setLocation(getX(),getY()+1);     //To increase the falling speed, change 1 to a higher number.
    }   
}
You would need to add this method to your World code:
1
2
3
4
5
6
7
8
public void createBall(int fallTime)     //This method creates a ball after a delay of "fallTime".
{
    if(actCount>fallTime)
    {
        Ball ball = new Ball();
        addObject(ball, //x-coordinate you want, 0);
    }
}
Using the method- Example: You want to add 2 balls at different times.
1
2
3
4
5
6
7
int actCount = 0;
public void act()
{
    createBall(50);     //Ball is created after 50 act cycles.
    createBall(100);     //Ball is created after 100 act cycles.
    actCount++;     //This variable counts the act cycles by increasing itself by 1 every act cycle.
}
You need to login to post a reply.