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

2012/6/15

help with the mouse

wahaj wahaj

2012/6/15

#
I think I'm doing everything right but the program doesn't do what I want it to do. what I want to do is that when the correct object is clicked I want it to clear the world so I can add another question. here is the code for my world. One thing to be noted, I cant seem to find the act method in my code. I'm sure I haven't used that method at all yet so that's the reason I just noted it.
public class The_World extends World
{
    public int counter = 0;
    int mousex;
    int mousey;

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

    }

    public void clearWorld()
    {
        removeObjects(getObjects(null) );
    }

    public void background()
    {
        setBackground("background.png");
        GreenfootImage background = getBackground();
        background.setTransparency(100);

    }

    public void questions()
    {

        if (counter == 0)
        {
            addObject(new Text("Click Circle"), 300, 50 );
            addObject(new Shape(2,50,50,102,0,0,255 ), 109,402); //answer
            addObject(new Shape(2,70,50,102,0,153,255),119,232);
            addObject(new Shape(1,50,50,204,0,105,255),456,202);
            addObject(new Shape(1,70,50,51,0,205,255),433,400);

            
            mouse();
            if ( (mousex > 59 && mousex < 159)&& (mousey > 350 && mousey < 452) )
            {
                counter = 1;

            }
            if ((mousex < 59 && mousex > 159)&& (mousey < 350 && mousey > 452) )
            {
                counter = -1;
            }

        }
        if (counter == 1 )
        {
            clearWorld();
        }
    }

    public void mouse()
    {
        if (Greenfoot.mouseClicked(null) )
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
            mousex = mouse.getX();
            mousey = mouse.getY();
        }

    }
}
all the Shape class does is make shapes with the specified perimeters, It doesn't have anything in its act method
danpost danpost

2012/6/15

#
The world class does not automatically add a blank 'act()' method. But, you can still add one yourself and move your 'questions();' method call into it. BTW, line 51 does not make any sense (will always be 'false'; both sides of the 'if' and the result).
wahaj wahaj

2012/6/16

#
oh I just changed the sign in the first if statement without paying much attention. but I didnt have line 51 originally. all it had there was an else but it still doesnt work. I've been experimenting for quite some time now but I cant seem to find a solution. here is the latest piece of code I've used
if (Greenfoot.mouseClicked(Shape.class) )
            {
                mouse();
                if ( (mousex > 59 && mousex < 159)&& (mousey > 350 && mousey < 452) )
                {
                    counter = 1;

                }
                else	
                {
                    counter = -1;
                }
            }
danpost danpost

2012/6/16

#
What exactly is the 'counter' variable supposed to be doing?
danpost danpost

2012/6/16

#
Line 1 in your latest code is flawed, in that you cannot use a 'class' as a parameter in the 'mouseClicked' method. It must be either 'null' or an 'Object' instance.
wahaj wahaj

2012/6/16

#
the counter variable just keeps track of which question I'm on. If the user get the answer right it is supposed to get a new value. the value in the counter decides which question to display. and I'm so confused, I just cant seem to figure out where to use .class or () after an actors name or what you are trying to say. I understand null but not Object instance. I cant remember anything explicitly telling me what right in the book.
danpost danpost

2012/6/16

#
What I meant by 'object' instance is: a reference to a specific world or actor. Every time use create a 'new' World object or a 'new' Actor object, you are creating an instance of the World or Actor sub-class. You could, in the Shape class, add an 'act' method as follows:
public void act()
{
    if (Greenfoot.mouseClicked(this))
    {
        // code to execute if 'this' particular Shape object was clicked on
    }
}
The 'this' keyword is a reference to the object that is being 'act'ed on at the moment.
wahaj wahaj

2012/6/16

#
well I tried to implement what you said but it didn't work out. here is what I did. since you used 'this' as the perimeter for 'mouseClicked' I assumed that this piece of code would have to be in the Shape class. So I put it there but when I tried to make the mouse method static it didn't work. now I had to move the entire code because I couldn't make the mouse method static. To keep things tidy I made a subclass of Shape and stuck the question method in there while the mouse method remained in the Shape class. That didn't work out either so I took everything back to the world class including the code you gave me. that still didn't work. So either I misunderstood what you are trying to say or I wasn't being very clear. since there will be multiple question I'll have to rely on coordinates as far as I see it. I'll have various shapes and the answer will be different for each question.
danpost danpost

2012/6/16

#
I totally understand what you are trying to do. My sample of code was just an example of using a reference to an object as the parameter in the 'mouseClicked' method (though, I was kind of trying to point you in the right direction). The code you have in the world class, right now, is far from what you actually want, and will only tend to get more difficult as you continue. By keeping things simple and to the point, things will work out much better. If I was doing this scenario, I would first set my questions in order. Each question will have one String, four Shape objects, and one answer (the String can be determined by the answer). I would make the question data easier to store by making them all integers (putting the answer first). My world variables would be as follows:
static final NONE = -1, CIRCLE = 0, OVAL = 1, SQUARE = 2, RECTANGLE = 3;
String[] txtShape = { "Circle", "Oval", "Square", "Rectangle" };
Shape[] shape = { new Shape(CIRCLE), new Shape(OVAL), new Shape(SQUARE), new Shape(RECTANGLE) };
int[][] question = { { CIRCLE, CIRCLE, OVAL, SQUARE, RECTANGLE }, 
                    { SQUARE, RECTANGLE, SQUARE, OVAL, CIRCLE } };
int response = NONE; // for users clicked object
boolean nextQuestion = true;
int qNum = -1; // your 'counter'
More shapes can be added by appending the first three lines; and more questions can be added with ease. My world act method would then be:
public void act()
{
    if (nextQuestion) showQuestion(++qNum);
    if (response == question[qNum][0]) nextQuestion = true;
}

private void showQuestion(int qnum)
{
    removeObjects(getObjects(null));
    addObject(new Text("Click " + txtShape[question[qnum][0]]), 300, 50);
    addObject(shape[question[qnum][1]], 200, 280);
    addObject(shape[question[qnum][2]], 400, 280);
    addObject(shape[question[qnum][3]], 200, 440);
    addObject(shape[question[qnum][4]], 400, 440);
    nextQuestion = false;
    response = NONE;
}
If you noticed, my Shape constructor calls only have one parameter. That is really all we need to pass to it; as the rest can be determined by the one. And the code to execute in the Shape class act, if clicked on, is
((The_World) getWorld).response = value;
where value is the shape value passed to the constructor and saved.
wahaj wahaj

2012/6/16

#
right of course. This will make my code a bit tidy but Its still kind of hard for me to read code and I dont wan't to copy yours. anyways I was thinking if I were to put my mouseClicked(null) method in the World class, then use getActor() and see if it equals the correct shape. that didnt work out so I tried this.
Shape shape = new Shape(2,50,50,102,0,0,255 );
            if (Greenfoot.mouseClicked(shape) )
            {
                counter = 1;  

            }  
it still doesn't work. at this point I've tried out all the idea I could think of. I'll be getting mouse input from various things not just shapes. including parts of a string rather than the entire thing. that's why I was hoping to use coordinates to solve this.I've checked a lot of places to see how I can work with coordinates but all I find is how to work with objects
wahaj wahaj

2012/6/16

#
oh man I'm so stupid. I kept putting all my code in the constructor of the world rather than putting in an act method. my code works fine now thanks for helping
You need to login to post a reply.