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

2015/1/21

Putting another question

1
2
MrGigglez MrGigglez

2015/1/21

#
Yepp that works too, but the answers are still 0
danpost danpost

2015/1/21

#
Please show your newly revised code for your world subclass. Use the 'code' link below the 'Post a reply' box to insert codes into your posts.
MrGigglez MrGigglez

2015/1/21

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Background here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{
    public int x = Greenfoot.getRandomNumber (200);
    private int y = 300;
    private int x1 = 380;
    public static int var = 1;

    /**
     * Constructor for objects of class Background.
     * 
     */
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(734, 580, 1); 

        prepare();
        setQuestion();

    }
    public void setQuestion()
    {
        removeObjects(getObjects(Questions.class));
        removeObjects(getObjects(Answer.class));
        x = 1+Greenfoot.getRandomNumber(200)/10;
        int a1 = 0, a2 = 0, a3 = 0, a = 0;
        switch(x)
        {
            case 1:  a1 = 15; a2 = 6; a3 = 4; a = 3; break;
            case 2:  a1 = 10; a2 = 15; a3 = 16; a = 1; break;
            case 3:  a1 = 12; a2 = 16; a3 = 37; a = 2; break;
            // etc.
        }
        addObject(new Questions(x), 380, 300);
        addObject(new Answer(a1, a == 1), 99, 464);
        addObject(new Answer(a2, a == 2), 363, 468);
        addObject(new Answer(a3, a == 3), 648, 467);
    }


    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Car1 car1 = new Car1();
        addObject(car1, 92, 67);
        Car2 car2 = new Car2();
        addObject(car2, 96, 167);
        car2.setLocation(95, 163);
        car2.setLocation(61, 160);
        car1.setLocation(65, 66);

    }
}
danpost danpost

2015/1/21

#
You need to complete the code within the 'switch' block (see line 40). Also, the values set to 'a' in lines 37 through 39 may need corrected.
MrGigglez MrGigglez

2015/1/21

#
Ohh okay. So ill write 20 cases? each case with the corresponding answer and flags i want ?
danpost danpost

2015/1/21

#
MrGigglez wrote...
Ohh okay. So ill write 20 cases? each case with the corresponding answer and flags i want ?
Yes. The first three were already done except I put sample values for setting 'a'.
MrGigglez MrGigglez

2015/1/21

#
Does case 1 mean question 1 how do I know which values to put where?
danpost danpost

2015/1/21

#
Yes, case 1 means question one (as per 'x' in line 35). Put the values of 'a1', 'a2' and 'a2' in the same order you had them. That is, you had the following for the first three:
if (x> 10 && x<20)
{
    addObject(new Q2(), x1, y );
    addObject(new A15(), 99, 464 );
    addObject(new A6(), 363, 468 );
    addObject(new A4(), 648, 467 );
}
if (x>10 && x<20) 
{
    addObject(new Q3(), x1, y  ) ;
    addObject(new A10(), 99, 464 );
    addObject(new A15(), 363, 468 );
    addObject(new A16(), 648, 467 );
}
if (x > 20 && x < 30)
{
    addObject(new Q4(), x1, y );
    addObject(new A12(), 99, 464 );
    addObject(new A16(), 363, 468);
    addObject(new A37(), 648, 467 );
}
I just took the A numbers in each block for the values of 'a1', 'a2' and 'a3'. However, I was not able to tell which answers were the correct ones. Sidenote: I guess my cases were off as you started your questions with '2' and not '1'.
MrGigglez MrGigglez

2015/1/21

#
Okay i got them to show the correct numbers, but now when I click the right answer the car doesnt change speed or the question chages. Should i put my code?
MrGigglez MrGigglez

2015/1/21

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Background here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Background extends World
{
    public int x = Greenfoot.getRandomNumber (200);
    private int y = 300;
    private int x1 = 380;
    public static int var = 1;

    /**
     * Constructor for objects of class Background.
     * 
     */
    public Background()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(734, 580, 1); 

        prepare();
        setQuestion();

    }

    public void setQuestion()
    {
        removeObjects(getObjects(Questions.class));
        removeObjects(getObjects(Answer.class));
        x = 1+Greenfoot.getRandomNumber(200)/10;
        int a1 = 0, a2 = 0, a3 = 0, a = 0;
        switch(x)
        {
            case 2:  a1 = 15; a2 = 6; a3 = 4; a = 15; break;
            case 3:  a1 = 10; a2 = 15; a3 = 16; a = 10; break;
            case 4:  a1 = 12; a2 = 16; a3 = 37; a = 12; break;
            case 5:  a1 = 0; a2 = 17; a3 = 6; a = 0; break;
            case 6:  a1 = 13; a2 = 49; a3 = 4; a = 13; break;
            case 7:  a1 = 14; a2 = 51; a3 = 49; a = 14; break;
            case 8:  a1 = 25; a2 = 16; a3 = 33; a = 25; break;
            case 9:  a1 = 27; a2 = 17; a3 = 16; a = 27; break;
            case 10:  a1 = 28; a2 = 3; a3 = 4; a = 28; break;
            case 11:  a1 = 5; a2 = 3; a3 = 4; a = 5; break;
            case 12:  a1 = 20; a2 = 60; a3 = 52; a = 20; break;
            case 13:  a1 = 2; a2 = 6; a3 = 7; a = 2; break;
             case 14:  a1 = 1; a2 = 3; a3 = 33; a = 1; break;
            case 15:  a1 = 11; a2 = 60; a3 = 12; a = 10; break;
            case 16:  a1 = 19; a2 = 60; a3 = 4; a = 19; break;
            case 17:  a1 = 9; a2 = 3; a3 = 4; a = 9; break;
            case 18:  a1 = 22; a2 = 14; a3 = 3; a = 22; break;
            case 19:  a1 = 17; a2 = 14; a3 = 4; a = 17; break;
            case 20:  a1 = 8; a2 = 6; a3 = 4; a = 8; break;
            case 21:  a1 = 36; a2 = 16; a3 = 7; a = 36; break;
          
            // etc.
        }
        addObject(new Questions(x), 380, 300);
        addObject(new Answer(a1, a == 1), 99, 464);
        addObject(new Answer(a2, a == 2), 363, 468);
        addObject(new Answer(a3, a == 3), 648, 467);
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Car1 car1 = new Car1();
        addObject(car1, 92, 67);
        Car2 car2 = new Car2();
        addObject(car2, 96, 167);
        car2.setLocation(95, 163);
        car2.setLocation(61, 160);
        car1.setLocation(65, 66);

    }
}
davmac davmac

2015/1/21

#
Okay i got them to show the correct numbers, but now when I click the right answer the car doesnt change speed or the question chages. Should i put my code?
You need to post the code which is supposed to change the speed of the car and the question. Since that is the code that isn't working, it is probably the code that we need to see.
danpost danpost

2015/1/21

#
The values assigned to 'a' should be either '1' if 'a1' is correct, '2' if 'a2' is correct or '3' if 'a3' is correct.
danpost danpost

2015/1/21

#
For the value of 'x' in line 34, you will need to add '2' instead of '1' or case 21 will never be used and nothing will show for case 1. As far as the fields in the Background class, lines 12 and 13 can be removed (not used anymore). Line 11 can also be removed; but, line 34 will need to be adjusted to this:
int x = 2+Greenfoot.getRandomNumber(200)/10;
As I do not know what 'var' (line 14) is for, I cannot say whether you need it or not.
You need to login to post a reply.
1
2