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

#
Hey. So i got my flags to appear and everytime the user clicks on the correct flag the car speed increases. What would i have to write in order to get the next questions without resetting the world or the cars speed. in other words how can i put another question when i click the right answer.
danpost danpost

2015/1/21

#
Impossible to say without knowing how the world, flags, questions, answering and car are all coded.
MrGigglez MrGigglez

2015/1/21

#
This is the code for the world 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; /** * 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(); 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 ); } if (x > 30 && x < 40) { addObject(new Q5(),x1, y ); addObject(new A0(), 99, 464 ); addObject(new A17(), 363, 468 ); addObject(new A6(), 648, 467 ); } if (x > 40 && x < 50) { addObject(new Q6(),x1, y ); addObject(new A13(), 99, 464 ); addObject(new A49(), 363, 468 ); addObject(new A4(), 648, 467 ); } if (x > 50 && x < 60) { addObject(new Q7(), x1, y ); addObject(new A14(), 99, 464 ); addObject(new A51(), 363, 468 ); addObject(new A49(), 648, 467); } if (x > 60 && x < 70) { addObject(new Q8(),x1, y ); addObject(new A25(), 99, 464 ); addObject(new A12(), 363, 468 ); addObject(new A33(), 648, 467 ); } if (x > 70 && x < 80) { addObject(new Q9(), x1, y ); addObject(new A27(), 99, 464 ); addObject(new A12(), 363, 468 ); addObject(new A16(), 648, 467 ); } if (x > 80 && x < 90) { addObject(new Q10(), x1, y ); addObject(new A28(), 99, 464 ); addObject(new A3(), 363, 468 ); addObject(new A4(), 648, 467 ); } if (x > 90 && x < 100) { addObject(new Q11(), x1, y ); addObject(new A5(), 99, 464 ); addObject(new A3(), 363, 468 ); addObject(new A4(), 648, 467 ); } if (x > 100 && x < 110) { addObject(new Q12(), x1, y ); addObject(new A20(), 99, 464 ); addObject(new A60(), 363, 468 ); addObject(new A52(), 648, 467 ); } if (x > 110 && x < 120) { addObject(new Q13(), x1, y ); addObject(new A2(), 99, 464 ); addObject(new A6(), 363, 468 ); addObject(new A7(), 648, 467 ); } if (x > 120 && x < 130) { addObject(new Q14(), x1, y ); addObject(new A1(), 99, 464 ); addObject(new A3(), 363, 468); addObject(new A12(), 648, 467 ); } if (x > 130 && x < 140) { addObject(new Q15(), x1, y ); addObject(new A11(), 99, 464 ); addObject(new A60(), 363, 468 ); addObject(new A12(), 648, 467 ); } if (x > 140 && x < 150) { addObject(new Q16(), x1, y ); addObject(new A19(), 99, 464 ); addObject(new A12(), 363, 468 ); addObject(new A4(), 648, 467 ); } if (x > 150 && x < 160) { addObject(new Q17(), x1, y ); addObject(new A9(),99, 464 ); addObject(new A3(), 363, 468 ); addObject(new A4(), 648, 467 ); } if (x > 160 && x < 170) { addObject(new Q18(), x1, y ); addObject(new A22(), 99, 464 ); addObject(new A12(), 363, 468 ); addObject(new A3(), 648, 467 ); } if (x > 170 && x < 180) { addObject(new Q19(), x1, y ); addObject(new A17(), 99, 464 ); addObject(new A14(), 363, 468 ); addObject(new A4(), 648, 467 ); } if (x > 180 && x < 190) { addObject(new Q20(), x1, y ); addObject(new A8(), 99, 464 ); addObject(new A6(), 363, 468 ); addObject(new A7(), 648, 467 ); } if (x > 190 && x < 200) { addObject(new Q21(), x1, y ); addObject(new A36(), 99, 464 ); addObject(new A16(), 363, 468 ); addObject(new A12(), 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); } } This is the code of one flag import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class A15 here. * * @author (your name) * @version (a version number or a date) */ public class A15 extends Actor { /** * Act - do whatever the A15 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if(Greenfoot.mouseClicked(this)) { Car1.speed++; } } } this is the code for the car1 import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Car1 here. * * @author (your name) * @version (a version number or a date) */ public class Car1 extends Animal { public static int speed = 1; /** * Act - do whatever the Car1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setLocation (getX() + speed, getY()); } }
danpost danpost

2015/1/21

#
You first need to take all the code in the world constructor beyond the 'prepare' call and put it in a separate method that is called after the 'prepare' call. Add lines at the beginning of the new method to remove any previous questions and answers. Unfortunately, this would mean a lot of code unless you have all your A## classes subclass an Answer class and all your Q## classes subclass a Question class. But there must be an easier way -- one that does not require so much coding; one where all Questions can be created from a single class and all Answers can be created from a single class as well. Please provide a short list of the image filenames used for the some of the questions and another short one for some of the answers. Hopefully, this will not be too difficult to simplify.
MrGigglez MrGigglez

2015/1/21

#
Image files names are correspond to the numbers in them for example answers: A1 A2 A3 A5 etc Questions Q1 Q2 Q3 Q4 etc
danpost danpost

2015/1/21

#
Ok -- not bad. You can code the Answer and Question classes similar to this:
import greenfoot.*;

public class Answer extends Actor
{
    private boolean correctAnswer;

    public Answer(int answerNumber, boolean realAnswer)
    {
        setImage("A"+answerNumber+".png");
        correctAnswer = realAnswer;
    }

    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            if (correctAnswer)
            {
                Car1.speed++;
                ((Background)getWorld()).setQuestion();
            }
            else
            {
                // incorrect response actions
            }
        }
    }
}
Of course, your Question class will not need an act method or the 'correctAnswer' field. With this Answer class, you can do something like this in the world class:
public Background()
{
    super(734, 580, 1);
    prepare();
    setQuestion();
}

public void setQuestion()
{
    removeObjects(getObjects(Question.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 Question(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);
}
With this, you must have 20 questions with images 'Q1.png' to 'Q20.png'. 'a1', 'a2' and 'a3' are the answers given for question 'x' (ranged 1 to 20) and 'a' is the number of the correct answer (ranged 1 to 3).
MrGigglez MrGigglez

2015/1/21

#
So would i put all the A as subclasses for Answers?
danpost danpost

2015/1/21

#
MrGigglez wrote...
So would i put all the A as subclasses for Answers?
No. You would eliminate all your A and Q classes.
MrGigglez MrGigglez

2015/1/21

#
So if i put the code you gave me into my game. it should be good to go?
danpost danpost

2015/1/21

#
Putting the Answer class above into your game with a Question class (which would be a more simplified version of the Answer class since the only thing it will do is set the image of the actor created from it) and changing the code in your world class as shown above, you should be good to go.
MrGigglez MrGigglez

2015/1/21

#
It says it cannot find symbol - class question. Whhat would i put in class question?
danpost danpost

2015/1/21

#
You need to create a new subclass of Actor called Question and work the image in the same manner as in the Answer class (add a parameter to the constructor to get the question number and use it within the constructor for setting the appropriate image).
MrGigglez MrGigglez

2015/1/21

#
I wrote this import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Questions here. * * @author (your name) * @version (a version number or a date) */ public class Questions extends Actor { private boolean correctAnswer; public Questions(int questionNumber) { setImage("Q"+questionNumber+".png"); } /** * Act - do whatever the Questions wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } }
danpost danpost

2015/1/21

#
That should work; but it is a bit much. This is all that is needed:
import greenfoot.*;

public class Question extends Actor
{
    public Question(int questionNumber)
    {
        setImage("Q"+questionNumber+".png");
    }
}
MrGigglez MrGigglez

2015/1/21

#
Never mind i fixed it. Now when it runs all the answers are 0??
There are more replies on the next page.
1
2