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

2015/9/30

Chain Factor

IsabelSchouten IsabelSchouten

2015/9/30

#
Hello everybody, For school we need to make a game with Greenfoot, Chain Factor. To understand what game we are talking about here is a link to play the game. http://jayisgames.com/games/chain-factor/. We have some problems making the game. We need to have fifteen balls in the world but the balls can't lay on top of each other but they lay underneath each other on the bottom row. Does anyone know how to get the balls on the other layers as well?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class speelVeld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class speelVeld extends World
{

    /**
     * Constructor for objects of class speelVeld.
     * 
     */
    public speelVeld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(7, 7, 60); 
        
        
        for (int i = 0; i < 15; i++) {
            
            int nummer = Greenfoot.getRandomNumber(9);
            
            if (nummer == 0) {
                int x = Greenfoot.getRandomNumber(8);
                addObject(new one(), x, 6);
            }
        
            if (nummer == 1) {
                int x = Greenfoot.getRandomNumber(8);
                addObject(new two(), x, 6);
            }
        
            if (nummer == 2) {
                int x = Greenfoot.getRandomNumber(8);
                addObject(new three(), x, 6);
            }
        
            if (nummer == 3) {
                int x = Greenfoot.getRandomNumber(8);
                addObject(new four(), x, 6);
            }
        
            if (nummer == 4) {
                int x = Greenfoot.getRandomNumber(8);
                addObject(new five(), x, 6);
            }
        
            if (nummer == 5) {
                int x = Greenfoot.getRandomNumber(8);
                addObject(new six(), x, 6);
            }
        
            if (nummer == 6) {
                int x = Greenfoot.getRandomNumber(8);
                addObject(new seven(), x, 6);
            }
        
            if (nummer == 7) {
                int x = Greenfoot.getRandomNumber(8);
                addObject(new grijs(), x, 6);
            }
        
            if (nummer == 8) {
                int x = Greenfoot.getRandomNumber(8);
                addObject(new cracked(), x, 6);
            }
        }
    }
}
danpost danpost

2015/9/30

#
You could use the 'addedToWorld' method in each Actor subclass (or if all 9 types of balls are subclasses of a 'ball' class, for example, just in it) to determine if (and move, if so) there is already an object at its location; or, and this may be a better way, create a method in your speelVeld class to return the y-coordinate of the first unoccupied location along the vertical along which you want to place the object:
private int nextY(int x)
{
    int y = 6;
    while (y >= 0 && !getObjectsAt(x, y, null).isEmpty()) y--;
    if (y == -1) Greenfoot.setWorld(new speelVeld());
    return y;
}
This method should only be used during the construction of the world. I added line 5 just as a precaution. The condition probably never will be true, but since the chance, however slight, is there in your scenario, it should be added. This is good programming in that all possible situations are accounted for.
You need to login to post a reply.