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

2018/4/2

getObjects functions

Lavenger Lavenger

2018/4/2

#
I want to check if the location that the code randomly chose is randomly chosen or not, but it seems like i cant get it working... can someone teach me what did i do wrong and what's the correct way to do it? here's the entire world code:
import greenfoot.*;
public class Minigame1 extends World
{
    private Actor Walls = new SimpleActor();
    private Actor MiniTaihei = new SimpleActor();
    SimpleActor walls = new SimpleActor();
    public Minigame1()
    {
        super(20, 10, 30);
        // setBackground
        GreenfootImage bg = new GreenfootImage("sand2.jpg");
        bg.scale(getCellSize(), getCellSize());
        setBackground(bg);
        // add actor to world
        addObject(MiniTaihei, 0, 9);
    }
    public void act()
    {
        for (int i = 0; i <= 15;)
        {
            int RandX = (1 + Greenfoot.getRandomNumber(19));
            int RandY = (Greenfoot.getRandomNumber(9));
            while (getObject(RandX, RandY)!=null)
            {
                int RandX = (1 + Greenfoot.getRandomNumber(19));
                int RandY = (Greenfoot.getRandomNumber(9));
            }
            addObject(walls, RandX, RandY);
            i++;
        }
    }
}
danpost danpost

2018/4/2

#
First off, there is no getObject method (see line 23). Secondly, it looks like you are trying to add the same exact actor into the world (line 28) over and over again (15 times as per for parameters -- line 19). What is the while condition supposed to be checking (what is to be true -- line 23)? Also what image is walls or Walls supposed to take on? and why are these two, walls and Walls, special that you need fields for them?
Lavenger Lavenger

2018/4/2

#
danpost wrote...
First off, there is no getObject method (see line 23). Secondly, it looks like you are trying to add the same exact actor into the world (line 28) over and over again (15 times as per for parameters -- line 19). What is the while condition supposed to be checking (what is to be true -- line 23)? Also what image is walls or Walls supposed to take on? and why are these two, walls and Walls, special that you need fields for them?
I just want to add 15 walls into a 20 by 10 cells world at random locations, the for loop is supposed to do that and the while loop is supposed to get a new random location if the location that was chosen randomly is not empty... as for the SimpleActor walls, it doesn't let me use Walls so I tried using walls and it seemed to work so i left it in there
danpost danpost

2018/4/3

#
Lavenger wrote...
I just want to add 15 walls into a 20 by 10 cells world at random locations, the for loop is supposed to do that and the while loop is supposed to get a new random location if the location that was chosen randomly is not empty... as for the SimpleActor walls, it doesn't let me use Walls so I tried using walls and it seemed to work so i left it in there
Well, I currently do not see a need for any of the fields -- remove lines 4 through 6. You will probably want a class specifically for your wall objects. It may be necessary to be able to tell they are actually walls later. So, add a Wall class:
public class Wall extends greenfoot.Actor {}
and assign a default image to it. I do not know what your MiniTaihei object is, but it probably deserves a class of its own also. Then your Minigame1 class can be:
import greenfoot.*;

public class Minigame1 extends World
{
    public Minigame1()
    {
        super(20, 10, 30);
        // setBackground
        GreenfootImage bg = new GreenfootImage("sand2.jpg");
        bg.scale(getCellSize(), getCellSize());
        setBackground(bg);
        // add actor to world
        addObject(MiniTaihei, 0, 9);
        int randX = 0, randY = 0;
        for (int i=0; i<=15; i++)
        {
            while (randX == 0 || !getObjectsAt(randX, randY, Actor.class).isEmpty())
            {
                randX = (1 + Greenfoot.getRandomNumber(19));
                randY = (Greenfoot.getRandomNumber(9));
           }
            addObject(new Wall(), randX, randY);
        }
    }
}
Lavenger Lavenger

2018/4/3

#
danpost wrote...
Lavenger wrote...
I do not know what your MiniTaihei object is, but it probably deserves a class of its own also.
The MiniTaihei is an actor the the player is supposed to move around the world to play the game. The objective of the game is to move MiniTaihei around the map and catch MiniKirie. The walls are added to add difficulty to the minigame
danpost danpost

2018/4/3

#
My line 13 should read:
addObject(new MiniTaihei(), 0, 9);
Lavenger Lavenger

2018/4/4

#
ahh i thought so ^^
Lavenger Lavenger

2018/4/4

#
what do I do if I want to clear the world of actors? (clear the world when the game ends)
danpost danpost

2018/4/4

#
Lavenger wrote...
what do I do if I want to clear the world of actors? (clear the world when the game ends)
Use getObjects with removeObjects.
You need to login to post a reply.