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

2015/7/20

random falling

Jeph Jeph

2015/7/20

#
hey am trying to make an actor fall from the top screen randomly but within a time limit this is my code and i don't see it falling any help thnxx
 public void random()
    {
        int delay=5;
        int ranx = Greenfoot.getRandomNumber(377);
        int rany= Greenfoot.getRandomNumber(266);
        if (delay>0)
        {
            delay--;  
        }
        else if(delay==0)
        {
            addObject(new other(),ranx,rany);
            delay=5;
        }
    }
danpost danpost

2015/7/20

#
The code given is for randomly placing an 'other' object into the world at constant intervals of time. It has nothing to do with the actions of those objects after they are inserted into the world -- that code should be in the 'other' class.
Jeph Jeph

2015/7/21

#
i have many worlds and would like it to be the same for each world
danpost danpost

2015/7/21

#
danpost wrote...
The code given is for randomly placing an 'other' object into the world at constant intervals of time. It has nothing to do with the actions of those objects after they are inserted into the world -- that code should be in the 'other' class.
What I meant by this is that you need to show the code of your 'other' class (which should have something in it that is at least an attempt at making these 'other' objects fall).
Jeph Jeph

2015/7/24

#
i just use setLocation(getX(),getY()+3); to make it fall
davmac davmac

2015/7/24

#
Are you saying that the objects do get added to the world, but they don't fall? (Because that's what your original post suggests). In that case, you should post the full code for the 'other' class as danpost suggested. Or, if that's not the case, please explain carefully what the issue is.
Jeph Jeph

2015/7/24

#
ok i just want to add this actor to the world within an interval(time) but i want a falling effect .and is added @random positions at the top before falling i got the falling part right. "just the adding the actor to the world within an interval(time) .and is added @random positions"
Jeph Jeph

2015/7/24

#
sorry for the confusion
Super_Hippo Super_Hippo

2015/7/24

#
So the objects are added the world, but they don't fall? Show the act method of the 'other' class. Make sure they have a visible image, so you see them.
davmac davmac

2015/7/25

#
Super_Hippo wrote...
So the objects are added the world, but they don't fall? Show the act method of the 'other' class. Make sure they have a visible image, so you see them.
That was my original interpretation, but I think it's now been clarified to be the other way around:
Jeph wrote...
i got the falling part right. ... sorry for the confusion
Super_Hippo Super_Hippo

2015/7/25

#
Ah ok. You need to call the 'random' method from the 'act' method of the world. Place line 3 outside of the method. It can look like this:
private int delay = 5; //5 is maybe a bit low

public void act()
{
    random();
}

public void random()
{
    if (delay > 0) delay--;
    else 
    {
        //I guess the y-coordinate shouldn't be random if it should start at the top and fall down
        addObject(new other(), Greenfoot.getRandomNumber(377), 0);
        delay = 5;
    }
}
danpost danpost

2015/7/25

#
To explain: the 'delay' variable needs to be declared outside the method for it to be maintained beyond a single run of the method. That is to say, if the variable is declared (and set) within the method, then each time the method is called a new 'declare' variable will be created with that initial setting.
Jeph Jeph

2015/7/26

#
thank your"ll its working fine @danpost @Super_Hippo @davmac lol sorry for the confusion before
You need to login to post a reply.