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

2017/4/21

how do i make meteors fall randomly

1
2
ChrisD ChrisD

2017/4/21

#
so im trying to make meteors fall from top to bottom but i want them to generate randomly. How do i do this?
danpost danpost

2017/4/21

#
To generate randomly as far as position across the top, supply an x-coordinate value of 'Greenfoot.getRandomNumber(getWidth())'. To generate randomly as far as time, use 'if (Greenfoot.getRandomNumber(1000) < n)'. The larger 'n' is, the more often they will spawn.
ChrisD ChrisD

2017/4/24

#
Thank you for that i will test it now.
ChrisD ChrisD

2017/4/24

#
I am a beginner so what class would this code go into?
ChrisD ChrisD

2017/4/24

#
I entered the first code into my Meteor class and there was an error which said "Incompatible types: int cannot be converted to boolean" what does this mean
danpost danpost

2017/4/24

#
Generating actors is generally done by the world. You can usually ask if these actors originate from a specific type actor in your world. If the answer is not readily apparent, then the world should create them. Apparent situations would be bullets from player, enemies or bosses, beams from light sourcees, smoke or some other type trail, debris, smaller pieces emanating from an actor, cloning or multiplying of actors, etc. Your meteor actors are not the source of the new meteors that will be placed in the world, so spawning of them should be in your world class. If you are getting errors, you not only need to say what kind, but also show the related codes. We cannot help fix codes we cannot see.
ChrisD ChrisD

2017/4/24

#
    public void act()
    {
        if(Greenfoot.getRandomNumber(200)<2)
        {
            addObject(new Meteor(),Greenfoot.getRandomNumber(600)+20,0);
        }
    }
this is the code which i entered in my level 1 class and it allows meteors to spawn randomly but i was wondering is their a way to drop a specific amount e.g. 20 for level 1 and progressively increasing until level 5
danpost danpost

2017/4/24

#
All the '+20' does in line 5 is restrict meteors from spawning along the left edge of the world. Maybe you wanted that; but it seems more than coincidental that you want 20 meteors to spawn in the first level. You will need a field to track the number of spawned meteors. You will need another field either for the level number or for the maximum number of meteors to spawn. Then, increment the value of the first field when you create a meteor. For a second condition for spawning, the number of meteors created must be less than the current maximum number to spawn. When they equal each other and all meteors have left the world, you can set a new maximum, zero the number created and possibly increase the spawn rate to start the next level.
ChrisD ChrisD

2017/4/24

#
how would this be done because i am a beginner
danpost danpost

2017/4/24

#
ChrisD wrote...
how would this be done because i am a beginner
Please refer to the Joy of Code videeos, specifically #13 and #14.
ChrisD ChrisD

2017/4/24

#
iv watched them but i still do not understand
danpost danpost

2017/4/24

#
ChrisD wrote...
iv watched them but i still do not understand
'leavesEaten' and 'meteorCount' should be done quite similarly. The field for maximum value, 'maxMeteors' is declared similarly, but given an initial value that is something other than zero.
ChrisD ChrisD

2017/4/25

#
with 'LettuceEaten' would it still work with dodging a specific amount of Meteors as the spaceship wont be trying to make contact but to actually avoid the Meteors
danpost danpost

2017/4/25

#
ChrisD wrote...
with 'LettuceEaten' would it still work with dodging a specific amount of Meteors as the spaceship wont be trying to make contact but to actually avoid the Meteors
The one counts when lettuce is eaten by an actor; the other counts when a meteor is spawned by the world. That is the only difference. The spaceship trying to avoid the meteors has nothing to do with the count. The count of spawned meteors will be used to regulate how many meteors are spawned each level and level end can be detected by count equal max and no meteors in world (all meteors were avoided).
ChrisD ChrisD

2017/4/25

#
public void MeteorDodged()
{
    MeteorDodged++;
    if(MeteorDodged == 20){
        MissionSuccessful();
    }   
}

from watching the video i think that this is the right code for a specific amount of meteors falling but when i enter in this code their is an error resulting in my other classes being wrong aswell.
There are more replies on the next page.
1
2