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

2014/2/23

Populating Objects in specific locations randomly

sobia sobia

2014/2/23

#
Hello, This is my first time using Greenfoot. I would like to populate cars objects (blue,red,green) at 4 different locations only - VerticallyUp, VerticallyDown, HorizontallyUp, HorizontallyDown. All I can think of is creating 4 functions for each location inside the World class, then randomly populate these objects(randomly too) But I think this solution is not efficient maintaining wise. Because I'm basically copying the same function 4 times, then changing the locations manually and chanting the movement using turn(). In VerticalUp it will be turn(90) VerticalDown(-90) and so on Do you guys have any better idea? Snippet:
    public void createVerticallyDown(int k)
    {
        switch(k)
        {
            case 1:
                Vehicle blue = new Blue();
                blue.getImage().scale(50,50); //optional  
                blue.turn(-90);
                addObject(blue, 450, 590);
                break;
            case 2:
                Vehicle red = new Red();
                red.getImage().scale(50,50); //optional  
                red.turn(-90);
                addObject(red, 450, 590);
                break;
        }            
    }
    

}
danpost danpost

2014/2/23

#
I do not think a single snippet will be enough. Please post the code for the world class so we get a better idea of what you are trying to do.
sobia sobia

2014/2/23

#
Hmmm,, What about if I create 4 actors called VerticalUpActors, VerticalDownActors, HorizontalRightActors, HorizontalLeftActors and create different classes for these Vehicle Blue,Red,whatever, Then create 4 objects of these Vehicles inside each VerticalUpActors, VerticalDownActors, HorizontalRightActors, HorizontalLeftActors, and move all these logics in the main class to each of these classes? Later I want to stop the movement of all the vehicles that are moving horizontally only with key press. Do you think this would work?
sobia sobia

2014/2/23

#
Please read the above reply after this one Hello Danpost, do you know a game called Traffic Rush on iOS? I'm basically testing out how to build something similar but different in functionality. Heres my world code for now: (Note: There is only 2 objects Red and Blue, didnt add the rest yet)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class RacingWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class RacingWorld extends World
{
    private int objectIdVertical = 1;
    private int objectIdHorizontal = 1;    
    private int delay = 0;
    /**
     * Constructor for objects of class RacingWorld.
     * 
     */
    public RacingWorld()
    {    
        super(800, 600, 1);         
    }
    
    /**
     * Constructor for objects of class RacingWorld.
     * 
     */
    public void act()
    {    
        if (delay != 0){
            delay--;
        }else{
            int i = Greenfoot.getRandomNumber(2) + 1;
            int j = Greenfoot.getRandomNumber(2) + 1;            
            while (i == objectIdVertical){
                i = Greenfoot.getRandomNumber(2) + 1;        
            }
            while (j == objectIdHorizontal){
                j = Greenfoot.getRandomNumber(2);        
            }               
            objectIdVertical = i;
            objectIdHorizontal = j;            
            delay = 600;
            this.createVerticallyUp(i);
            this.createVerticallyDown(i);            
            this.createHorizontalLeft(j);            
         
        }
    }
        

    public void createVerticallyUp(int i)
    {
        switch(i)
        {
            case 1:
                Vehicle blue = new Blue();
                Blue.getImage().scale(50,50); //optional  
                blue.turn(90);
                addObject(blue, 350, 10);
                break;
            case 2:
                Vehicle red = new Red();
                //Greenfoot.getRandomNumber();
                red.getImage().scale(50,50); //optional  
                red.turn(90);
                addObject(red, 350, 10);
                break;
        }           
    }
    
    public void createHorizontalLeft(int j)
    {
        switch(j)
        {
            case 1:
                Vehicle blue = new Blue();
                blue.getImage().scale(50,50); //optional  
                addObject(blue, 10, 350);
                break;
            case 2:
                Vehicle red = new Red();
                //Greenfoot.getRandomNumber();
                red.getImage().scale(50,50); //optional  
                addObject(red, 10, 350);
                break;
        }           
    }    
    
    public void createVerticallyDown(int k)
    {
        switch(k)
        {
            case 1:
                Vehicle blue = new Blue();
                blue.getImage().scale(50,50); //optional  
                blue.turn(-90);
                addObject(blue, 450, 590);
                break;
            case 2:
                Vehicle red = new Red();
                //Greenfoot.getRandomNumber();
                red.getImage().scale(50,50); //optional  
                red.turn(-90);
                addObject(red, 450, 590);
                break;
        }            
    }
    

}
danpost danpost

2014/2/23

#
No. You do not want to complicate matters. In fact, you probably do not need but the one Vehicle class (no subclasses of it). I am not familiar with Traffic Rush on iOS; however, I do believe that I now understand what you are trying to do. I am quite sure that you can do what you want in a single method. The only thing I am not sure of is what your act method is trying to do. You start with both 'objectId...' fields set to one and it appears your act method will set 'i' to two and 'j' to zero one time, and then back to both being one the next. What are the 'objectIdVertical' and 'objectIdHorizontal' fields actually tracking?
danpost danpost

2014/2/23

#
Will the vehicles only travel in one direction (each, vertically or horizontally) across the screen and then disappear?
sobia sobia

2014/2/23

#
danpost wrote...
No. You do not want to complicate matters. In fact, you probably do not need but the one Vehicle class (no subclasses of it). I am not familiar with Traffic Rush on iOS; however, I do believe that I now understand what you are trying to do. I am quite sure that you can do what you want in a single method. The only thing I am not sure of is what your act method is trying to do. You start with both 'objectId...' fields set to one and it appears your act method will set 'i' to two and 'j' to zero one time, and then back to both being one the next. What are the 'objectIdVertical' and 'objectIdHorizontal' fields actually tracking?
Here is a video of the game: http://www.youtube.com/watch?v=su1Dp7zQtY8 All I want to do is populate these Vehicles like the game randomly, then stop the one moving horizontally by a key press. I used these two variables to avoid having the same Vehicles being generated again. I may remove it when I have more Vehicles, which the probability of having the same Vehicles will be less. Appreciate your help here Danpost!
sobia sobia

2014/2/23

#
danpost wrote...
Will the vehicles only travel in one direction (each, vertically or horizontally) across the screen and then disappear?
I will stop the horizontally moving ones with the key press, once clicked, all horizontally are stopped. if not then they will just disappear at the world edge.
danpost danpost

2014/2/23

#
Something similar to the following should work:
import greenfoot.*;

public class RacingWorld extends World
{
    private int delay;

    public RacingWorld()
    {
        super(800, 600, 1);
    }

    public void act()
    {
        if (delay == 0)
        {
            int rand = Greenfoot.getRandomNumber(12); // #colors * #locations
            int color = rand%3, turn = rand/3;
            Vehicle car = new Vehicle();
            GreenfootImage image = null;
            switch(color)
            {
                case 0: image = new GreenfootImage("car01.png"); break;
                case 1: image = new GreenfootImage("car02.png"); break;
                case 2: image = new GreenfootImage("car03.png"); break;
            }
            car.setImage(image);
            car.setRotation(90*turn);
            int[] xs = { 10, 350, 450, 590 }, ys = { 350, 10, 250, 590 };
            addObject(car, xs[turn], ys[turn]);
        }
    }
}
sobia sobia

2014/2/24

#
Liked the approach a lot, didn't know about the setrotation method. If I'm not wrong , to stop all these moving horizontally cars I would get these objects height let's say 350 And make them all stop moving. I'm not sure if there is a green foot method that actually does forcing all objects with the same width to stop. I will check in some hours. Much appreciated!
danpost danpost

2014/2/24

#
'turn' would have worked also (being the object was just created and its initial rotation is zero). To move only vertically moving cars, use something like this:
if (getRotation()%180 == 90) move(5);
To move only horizontally moving cars:
if (getRotation()%180 == 0) move(5);
You could set up an instance field to replace the '90' and '0' and change the fields value from zero to ninety and back to regulate which direction is currently moving.
You need to login to post a reply.