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

2019/3/29

Adding in objects randomly

coreypants03 coreypants03

2019/3/29

#
Hi, i;m trying to add in an object randomly but my code isn't working
 public void act()
    {
    if (--enemySpawnTimer==0)
        {
      
        addObject(newEnemy(), Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
    }
}
This is it. What am I doing wrong. It says that it can't find the variable when I try to compile it.
plsFast plsFast

2019/3/29

#
Which variable cant be found?
coreypants03 coreypants03

2019/3/29

#
i changed it a bit to this:
public void act()
    {
    if (curTime >= lastAdded + 5000)
    {
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject(new alligator(), getWidth(), y);
        lastAdded - curTime;
    }
}
it said the minus sign is not a statement but I don't know what to add instead to make the code work.
plsFast plsFast

2019/3/29

#
where do u want to safe the sulution? lastAdded = lastAdded - curTime;?
plsFast plsFast

2019/3/29

#
where do u want to safe the sulution? lastAdded = lastAdded - curTime;?
danpost danpost

2019/3/29

#
Shouldn't the minus sign be an equals sign?
coreypants03 coreypants03

2019/4/1

#
when I added an equal sign more errors showed up. I also changed curTime it to cutTime to see if that was where the problem was but it didn't fix it.
public void act()
    {
    if (cutTime >= lastAdded + 5000)
    {
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject(new Enemy(), getWidth(), y);
        lastAdded = cutTime;
    }
}
when this is my code, "cutTime", and "lastAdded" have a red line under them and it says that it cannot find variable
coreypants03 coreypants03

2019/4/1

#
I was able to fix it for the most part.
 public void act()
    {
        if(Greenfoot.getRandomNumber(100) == 50)
        {
            int spawnX = Greenfoot.getRandomNumber (600);
            int spawnY = Greenfoot.getRandomNumber (400);
            addObject (new Enemy(), spawnX, spawnY);
        }
        if(Greenfoot.getRandomNumber(100) == 50)
        {
            int spawnX = Greenfoot.getRandomNumber (600);
            int spawnY = Greenfoot.getRandomNumber (400);
            addObject (new Enemy2(), spawnX, spawnY);
        }
        if(Greenfoot.getRandomNumber(100) == 50)
        {
            int spawnX = Greenfoot.getRandomNumber (600);
            int spawnY = Greenfoot.getRandomNumber (400);
            addObject (new Enemy3(), spawnX, spawnY);
        }

    }
I have 3 enemies in my game so that's why there are multiple enemies. Now the question I have is how do i get the enemies to add in along the right part of the screen. Right now they're just adding in randomly
danpost danpost

2019/4/1

#
The field name should probably actually be curTime, meaning the current time. It, as well as lastAdded, whose value is the actual time that an enemy was last added into the world, should both be declared somewhere. Since the lastAdded field needs to be retained through multiple act steps, it must be declared outside the method:
private long lastAdded;

public void act()
{
    long curTime = System.currentTimeMillis();
    if (curTime >= lastAdded+5000)
    {
        int y = Greenfoot.getRandomNumber(getHeight());
        addObject(new Enemy(), getWidth(), y);
        lastAdded = curTime;
    }
}
You need to login to post a reply.