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

2014/8/19

how do i make something only appear out of the bush once

murraycod murraycod

2014/8/19

#
I am making a safari like project like Pokemon when the player walks into the bush (there are 30 bushes in a 5 by 6 grid) and then a random number is generated and if it is between certain numbers that pokemon occurs, but my problem is it my player sits in the bush more then 1 pokemon occur depending on how long i sit in it for. Is there a way that i can make it so it only generates one number and wont repeat until after i walk out of the bush and into it again? here is my current coding for the pokemon numbers: if(getOneIntersectingObject(Grass.class) != null) { Actor collision = getOneIntersectingObject(Grass.class); int rng=Greenfoot.getRandomNumber(40); if (rng<=46 && rng >= 50 ){rng= 1 ;} //checking if the mark is between 100% and 90% else if (rng<=40 && rng >= 45 ){rng= 2 ;}//checking if the mark is between 89% and 80% else if (rng<=29 && rng >= 39 ){rng= 3 ;}//checking if the mark is between 79% and 60% else if (rng<=16 && rng >= 28 ){rng= 4 ;}//checking if the mark is between 59% and 50% else if (rng<=1 && rng >= 15 ){rng= 5 ;}//checking if the mark is between 49% and 0% switch (rng) { case 1: getWorld().addObject( new Tyranitar(), 120, 20 ); break; //stops the case case 2: getWorld().addObject( new Salamence(), 170, 20 ); break; //stops the case case 3: getWorld().addObject( new Dragonite(), 220, 20 ); break; //stops the case case 4: getWorld().addObject( new Donphan(), 70, 20 ); break; //stops the case case 5: getWorld().addObject( new Turtwig(), 20, 20 ); break; //stops the case }
Draymothisk Draymothisk

2014/8/19

#
I use timers for things like that, although I'm sure that there's a better way. Do something like this maybe?
int timer = 0; 

 if(getOneIntersectingObject(Grass.class) != null)
{
    timer++;
    if(timer == 1)
    {*put code to run once here*}
}
else
{
    timer = 0;
}
murraycod murraycod

2014/8/19

#
That didnt work.
GreenGoo GreenGoo

2014/8/19

#
boolean pokemonPresent =false; 

if(getOneIntersectingObject(Grass.class) != null && pokemonPresent = false)
        	{
Actor collision = getOneIntersectingObject(Grass.class);

int rng=Greenfoot.getRandomNumber(40);

if (rng<=46 && rng >= 50 ){rng= 1 ;} //checking if the mark is between 100% and 90% 
else if (rng<=40 && rng >= 45 ){rng= 2 ;}//checking if the mark is between 89% and 80% 
else if (rng<=29 && rng >= 39 ){rng= 3 ;}//checking if the mark is between 79% and 60% 
else if (rng<=16 && rng >= 28 ){rng= 4 ;}//checking if the mark is between 59% and 50% 
else if (rng<=1 && rng >=  15 ){rng= 5 ;}//checking if the mark is between 49% and 0% 
        switch (rng) {
   case 1:
 getWorld().addObject( new Tyranitar(), 120, 20 ); 
 pokemonPresent = true; 
 break; //stops the case
   
   case 2:
   getWorld().addObject( new Salamence(), 170, 20 );
   
break; //stops the case
   
   case 3:
   getWorld().addObject( new Dragonite(), 220, 20 );
  pokemonPresent = true; 
   break;  //stops the case
  
   case 4:
   getWorld().addObject( new Donphan(), 70, 20 ); 
   break; //stops the case
   
   case 5:
pokemonPresent = true; 
   getWorld().addObject( new Turtwig(), 20, 20 ); 
   break; //stops the case
}
GreenGoo GreenGoo

2014/8/19

#
Heck, missed a bit. You get the idea though. Then once the pokemon has been killed/ knocked out, set it to false again. Just make sure to set pokemonPresent to true in each case.
You need to login to post a reply.