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

2015/11/9

Chain factor

IsabelSchouten IsabelSchouten

2015/11/9

#
Hello everyone, We have got another problem with our game... When a ball falls with for example the number three, the ball needs to find out if he has fallen in a line with three ba
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bal here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bal extends Actor
{    
    /**
     * Act - do whatever the Bal wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public boolean active = true;

    public Bal()
    {
        spawn();
    }

    public void spawn()
    {
        switch (Greenfoot.getRandomNumber(7))
        {
            case 0: setImage("one.png"); break;
            case 1: setImage("two.png"); break;
            case 2: setImage("three.png"); break;
            case 3: setImage("four.png"); break;
            case 4: setImage("five.png"); break;
            case 5: setImage("six.png"); break;
            case 6: setImage("seven.png"); break;
        }
    }

    public void bestuurNet()
    {
        //move the ball to the left or right
        int dx=0;
        if (Greenfoot.isKeyDown("left")) dx--;
        if (Greenfoot.isKeyDown("right")) dx++;
        setLocation(getX()+dx, getY());
        //move back if there is already another ball or we reached the end of the world
        //if (isTouching(Bal.class) || getX() < 1 || getX() > getWorld().getWidth() - 2) setLocation(getX()-dx, getY());

        if (Greenfoot.isKeyDown("down"))
        {
            //fall down
            while (!isTouching(Bal.class) && getY() < getWorld().getHeight() - 2)
            {
                setLocation(getX(), getY()+1);
            }
            setLocation(getX(), getY()-1);
            active = false;
            ((SpeelVeld) getWorld()).spawnNewBal();
        }
        //         else
        //         {
        //             //move down slowly
        //             setLocation(getX(), getY()+1);
        //             if (isTouching(Bal.class) || getY() > getWorld().getHeight() - 2)
        //             {
        //                 setLocation(getX(), getY()-1);
        //                 active = false;
        //                 ((SpeelVeld) getWorld()).spawnNewBal();
        //             }
        //         }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Isabel Schouten & Dexter Demmink
 */

public class SpeelVeld extends World
{
    // Hoogtes van ballen
    public int x1 = 8;
    public int x2 = 8;
    public int x3 = 8;
    public int x4 = 8;
    public int x5 = 8;
    public int x6 = 8;
    public int x7 = 8;

    /**
     * Constructor for objects of class speelVeld.
     * Code in dit bestand is zelf bedacht.
     */
    public void act()
    {
        for (Object obj : getObjects(Bal.class))
        {
            Bal b = (Bal) obj;
            if (b.active)
            {
                b.bestuurNet();
                break;
            }
        }
    }

    public void spawnNewBal()
    {
        addObject(new Bal(), 1+Greenfoot.getRandomNumber(getWidth()-1), 1);
    }

    public SpeelVeld() {    
        // Create a new world with 7x9 cells with a cell size of 60x60 pixels.
        super(7, 9, 60); 
        addObject(new ScoreBoard(), 3, 0);
        addObject(new Bal(), Greenfoot.getRandomNumber(6), 1);

        for (int i = 0; i < 15; i++) {

            int nummer = Greenfoot.getRandomNumber(9); // Welk nummer wordt gespawned?

            if (nummer == 0) { // Één wordt gespawned
                int x = Greenfoot.getRandomNumber(7); // X-waarde van bal

                // Zorg voor de juiste hoogte
                if (x == 0) { 
                    addObject(new One(), 0, x1);
                    x1--;
                }

                if (x == 1) { 
                    addObject(new One(), 1, x2);
                    x2--;
                }

                if (x == 2) { 
                    addObject(new One(), 2, x3);
                    x3--;
                }

                if (x == 3) { 
                    addObject(new One(), 3, x4);
                    x4--;
                }

                if (x == 4) { 
                    addObject(new One(), 4, x5);
                    x5--;
                }

                if (x == 5) { 
                    addObject(new One(), 5, x6);
                    x6--;
                }

                if (x == 6) { 
                    addObject(new One(), 6, x7);
                    x7--;
                }

            }

            if (nummer == 1) { // Twee wordt gespawned
                int x = Greenfoot.getRandomNumber(7);

                if (x == 0) { 
                    addObject(new Two(), 0, x1);
                    x1--;
                }

                if (x == 1) { 
                    addObject(new Two(), 1, x2);
                    x2--;
                }

                if (x == 2) { 
                    addObject(new Two(), 2, x3);
                    x3--;
                }

                if (x == 3) { 
                    addObject(new Two(), 3, x4);
                    x4--;
                }

                if (x == 4) { 
                    addObject(new Two(), 4, x5);
                    x5--;
                }

                if (x == 5) { 
                    addObject(new Two(), 5, x6);
                    x6--;
                }

                if (x == 6) { 
                    addObject(new Two(), 6, x7);
                    x7--;
                }
            }

            if (nummer == 2) { // Drie wordt gespawned
                int x = Greenfoot.getRandomNumber(7);

                if (x == 0) { 
                    addObject(new Three(), 0, x1);
                    x1--;
                }

                if (x == 1) { 
                    addObject(new Three(), 1, x2);
                    x2--;
                }

                if (x == 2) { 
                    addObject(new Three(), 2, x3);
                    x3--;
                }

                if (x == 3) { 
                    addObject(new Three(), 3, x4);
                    x4--;
                }

                if (x == 4) { 
                    addObject(new Three(), 4, x5);
                    x5--;
                }

                if (x == 5) { 
                    addObject(new Three(), 5, x6);
                    x6--;
                }

                if (x == 6) { 
                    addObject(new Three(), 6, x7);
                    x7--;
                }
            }

            if (nummer == 3) { // Vier wordt gespawned
                int x = Greenfoot.getRandomNumber(7);

                if (x == 0) { 
                    addObject(new Four(), 0, x1);
                    x1--;
                }

                if (x == 1) { 
                    addObject(new Four(), 1, x2);
                    x2--;
                }

                if (x == 2) { 
                    addObject(new Four(), 2, x3);
                    x3--;
                }

                if (x == 3) { 
                    addObject(new Four(), 3, x4);
                    x4--;
                }

                if (x == 4) { 
                    addObject(new Four(), 4, x5);
                    x5--;
                }

                if (x == 5) { 
                    addObject(new Four(), 5, x6);
                    x6--;
                }

                if (x == 6) { 
                    addObject(new Four(), 6, x7);
                    x7--;
                }
            }

            if (nummer == 4) { // Vijf wordt gespawned
                int x = Greenfoot.getRandomNumber(7);

                if (x == 0) { 
                    addObject(new Five(), 0, x1);
                    x1--;
                }

                if (x == 1) { 
                    addObject(new Five(), 1, x2);
                    x2--;
                }

                if (x == 2) { 
                    addObject(new Five(), 2, x3);
                    x3--;
                }

                if (x == 3) { 
                    addObject(new Five(), 3, x4);
                    x4--;
                }

                if (x == 4) { 
                    addObject(new Five(), 4, x5);
                    x5--;
                }

                if (x == 5) { 
                    addObject(new Five(), 5, x6);
                    x6--;
                }

                if (x == 6) { 
                    addObject(new Five(), 6, x7);
                    x7--;
                }
            }

            if (nummer == 5) { // Zes wordt gespawned
                int x = Greenfoot.getRandomNumber(7);

                if (x == 0) { 
                    addObject(new Six(), 0, x1);
                    x1--;
                }

                if (x == 1) { 
                    addObject(new Six(), 1, x2);
                    x2--;
                }

                if (x == 2) { 
                    addObject(new Six(), 2, x3);
                    x3--;
                }

                if (x == 3) { 
                    addObject(new Six(), 3, x4);
                    x4--;
                }

                if (x == 4) { 
                    addObject(new Six(), 4, x5);
                    x5--;
                }

                if (x == 5) { 
                    addObject(new Six(), 5, x6);
                    x6--;
                }

                if (x == 6) { 
                    addObject(new Six(), 6, x7);
                    x7--;
                }
            }

            if (nummer == 6) { // Zeven wordt gespawned
                int x = Greenfoot.getRandomNumber(7);

                if (x == 0) { 
                    addObject(new Seven(), 0, x1);
                    x1--;
                }

                if (x == 1) { 
                    addObject(new Seven(), 1, x2);
                    x2--;
                }

                if (x == 2) { 
                    addObject(new Seven(), 2, x3);
                    x3--;
                }

                if (x == 3) { 
                    addObject(new Seven(), 3, x4);
                    x4--;
                }

                if (x == 4) { 
                    addObject(new Seven(), 4, x5);
                    x5--;
                }

                if (x == 5) { 
                    addObject(new Seven(), 5, x6);
                    x6--;
                }

                if (x == 6) { 
                    addObject(new Seven(), 6, x7);
                    x7--;
                }
            }

            if (nummer == 7) { // Grijs wordt gespawned
                int x = Greenfoot.getRandomNumber(7);

                if (x == 0) { 
                    addObject(new Grijs(), 0, x1);
                    x1--;
                }

                if (x == 1) { 
                    addObject(new Grijs(), 1, x2);
                    x2--;
                }

                if (x == 2) { 
                    addObject(new Grijs(), 2, x3);
                    x3--;
                }

                if (x == 3) { 
                    addObject(new Grijs(), 3, x4);
                    x4--;
                }

                if (x == 4) { 
                    addObject(new Grijs(), 4, x5);
                    x5--;
                }

                if (x == 5) { 
                    addObject(new Grijs(), 5, x6);
                    x6--;
                }

                if (x == 6) { 
                    addObject(new Grijs(), 6, x7);
                    x7--;
                }
            }

            if (nummer == 8) { // Cracked wordt gespawned
                int x = Greenfoot.getRandomNumber(7);

                if (x == 0) { 
                    addObject(new Cracked(), 0, x1);
                    x1--;
                }

                if (x == 1) { 
                    addObject(new Cracked(), 1, x2);
                    x2--;
                }

                if (x == 2) { 
                    addObject(new Cracked(), 2, x3);
                    x3--;
                }

                if (x == 3) { 
                    addObject(new Cracked(), 3, x4);
                    x4--;
                }

                if (x == 4) { 
                    addObject(new Cracked(), 4, x5);
                    x5--;
                }

                if (x == 5) { 
                    addObject(new Cracked(), 5, x6);
                    x6--;
                }

                if (x == 6) { 
                    addObject(new Cracked(), 6, x7);
                    x7--;
                }
            }

        }
    }
}
lls and if this is the case, the ball has to dissapear. We don't know how to let this happen. Can someone help us?
IsabelSchouten IsabelSchouten

2015/11/9

#
Im sorry, something went wrong with the text but this is the text: Hello everyone, We have got another problem with our game... When a ball falls with for example the number three, the ball needs to find out if he has fallen in a line with three balls and if this is the case, the ball has to dissapear. We don't know how to let this happen. Can someone help us?
danpost danpost

2015/11/9

#
At the moment, you do not have any code in your Bal class that lets it (1) retain the numeric value given it; or (2), return that value. The random number acquired at line 25 needs to be retained. First, we need a reference to that value. Change the spawn method to this:
private void spawn()
{
    int id = Greenfoot.getRandomNumber(7);
    setImage((new String[] { "one", "two", "three", "four", "five", "six", "seven" })[id]+".png");
}
Now we can turn the 'id' variable into a field by declaring it outside the method instead of inside:
// instance field
private int id;
// the 'spawn' method
private void spawn()
{
    id = Greenfoot.getRandomNumber(7);
    setImage((new String[] { "one", "two", "three", "four", "five", "six", "seven" })[id]+".png");
}
Now, you need a way for a Bal object to determine the 'id' value of another. Add a 'public' getter method that returns the value:
public int getID()
{
    return id;
}
IsabelSchouten IsabelSchouten

2015/11/11

#
Thanks danpost, for helping! Unfortunately, it still does not work....
danpost danpost

2015/11/11

#
IsabelSchouten wrote...
Thanks danpost, for helping! Unfortunately, it still does not work....
Please post the revised Bal class code.
IsabelSchouten IsabelSchouten

2015/11/12

#
Hi Danpost, here is the revised bal class code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bal here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bal extends Actor
{    
    /**
     * Act - do whatever the Bal wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */

    public boolean active = true;
    private int id;

    public Bal()
    {
        spawn();
    }

    private void spawn()
    {
         id = Greenfoot.getRandomNumber(7);
        setImage((new String[] { "one", "two", "three", "four", "five", "six", "seven" })[id]+".png");
    }

    public int getID()
    {
        return id;
    }

    public void bestuurBal()
    {
        //move the ball to the left or right
        int dx=0;
        if (Greenfoot.isKeyDown("left")) dx--;
        if (Greenfoot.isKeyDown("right")) dx++;
        setLocation(getX()+dx, getY());

        if (Greenfoot.isKeyDown("down"))
        {
            //fall down
            while (!isTouching(Bal.class) && getY() < getWorld().getHeight() - 1) // Werkt niet, moet while one.class, two.class enzo ook zijn
            {
                setLocation(getX(), getY()+1);
            }
            setLocation(getX(), getY()-1);
            active = false;
            ((SpeelVeld) getWorld()).spawnNewBal();
        }
    }
}
danpost danpost

2015/11/12

#
IsabelSchouten wrote...
Thanks danpost, for helping! Unfortunately, it still does not work....
How is it now not working? How exactly is it supposed to work? Where is the 'betuurBal' method called from? The action taken when the 'down' key is down should be in this order: * move down * check for limit * if limit reached, set inactive, move up slightly, and call 'spawnNewBal' in world subclass
You need to login to post a reply.