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

2012/11/29

add random obstacle

Tezuka Tezuka

2012/11/29

#
Hi I need help. I have random Obstacle in my world but I want it to change location after a certain time what should I do?
danpost danpost

2012/11/30

#
Just add a counter in the classes of the obstacles you want to relocate after a certain amount of time.
// add instance field
int counter;
// add to 'act' method
// or call a method with the following code from the 'act' method
counter = (counter+1)%600; // adjust the 600 to suit (approx 10 secs)
if (counter == 0) setLocation(Greenfoot.getRandomNubmer(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight());
Tezuka Tezuka

2012/11/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
 * Write a description of class Food here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Food extends Actor
{
    int counter;
    /**
     * Act - do whatever the Food wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Food()
    {
        GreenfootImage image;
        image = new GreenfootImage(20, 20);
        image.setColor(Color.RED);
        image.fillRect(0, 0, image.getWidth(), image.getHeight());
        setImage(image);

    }
    
    public void act()
    {
        MyWorld world = (MyWorld)getWorld();
        counter = (counter +1)% 600;
        if( counter == 0)
        {
           
           
               setLocation(Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
            
        }
    }
}
I did it like this it didnt work
danpost danpost

2012/11/30

#
I copy/pasted the entire code into a new class and it seemed to work fine.
Tezuka Tezuka

2012/11/30

#
I think it has to do with the time or the size of my world. my world is 40,30,20
Tezuka Tezuka

2012/11/30

#
y it worked now i just have to change 600 to 60 but can u explain the code pls i dont get it at all
danpost danpost

2012/11/30

#
I think you just did not wait the full 10 seconds. Change the 600 to 250 so you will not have to wait so long. The size of your world (or the cell size) should not make any difference.
danpost danpost

2012/11/30

#
Alright, the counter needs to be an instance field because you need to retain its value between one call of the 'act' method and the next. It value starts at zero. Each act cycle, its value is incremented and if the value = 600 (or 60 now that you changed it), its value is reduced to zero by the % operation (it returns the remainder after division). Then the value is checked to see if it is zero (the counter has completed one set of 600 (or 60) act cycles) and the location of the object is changed if so.
Tezuka Tezuka

2012/11/30

#
well if u take 60 and divide it with 60 u get 1 not zero.
danpost danpost

2012/11/30

#
Yes, but the remainder is zero. n = 0 n+1 = 1 1/60 = 0 r1 n+1 = 2 2/60 = 0 r2 ... n+1 = 58 58/60 = 0 r58 n+1 = 59 59/60 = 0 r59 n+1 = 60 60/60 = 1 r0 n+1 = 1 repeating The value after the r is what count equals after each cycle.
Tezuka Tezuka

2012/11/30

#
ok thx now i got it
You need to login to post a reply.