A method stopping the loop from another method

2

I have four objects (already instantiated) triangle, square, circle and a last picture, responsible for invoking both methods of each class of the respective objects, in order to draw a house, a sun, a chimney, a window and a roof, and place them in an orderly fashion to give life to the image.

Before you forget to mention, for teaching purposes, my teacher is using blueJ - according to him the tool provides better understanding in the IOO. So far everything is working perfectly.

While we are beginning to object orientation, we have gone through disciplines of primitive languages, so may deduce that I still know little about the subject.

My difficulties consist of:

  • I need to call a method " turnOnFireplace " as the name already says to light the chimney, causing smoke to escape from the chimney in the animation;

  • Create another "turnOffFireplace" method to extinguish the chimney, causing it to stop smoking.

  • What I did:

    I thought of creating a boolean variable to represent the state of the chimney (on / off), I loop do-while to create the circle (representing the smoke), make it move as if it were coming out of the chimney, disappear and return the starting point, doing everything again until the turnOffFireplace method is invoked to change the value of the variable from on to false by giving a " break " in the loop. is stopping the execution of the animation, however when I invoke the 'turnOffFireplace' method, by which, I would change the value of the variable on to false, and stop loop, breaking it, this does not happen ... the loop continues.

    I will represent better with the following code.

     public void turnOnFireplace()
    {
        smoke = new Circle();
    
        on = true;
    
        do
        {
            smoke.changeColor("black");
            smoke.slowMoveVertical(100);
            smoke.makeInvisible();
            smoke.move(-100);
        }while(on = true);
    }
    public void turnOffFireplace()
    {   
        on = false;
    }
    

    The variable I mentioned was declared at the beginning of the class Picture .

    What happens is that the loop does not stop. I tried other variations of this logic but it did not solve. How do I resolve this?

        
    asked by anonymous 06.04.2016 / 20:32

    1 answer

    3

    For the little of the code that was put, it is not possible to say if it has an engineering error of the whole class. I will consider that there is something that changes the state of the variable on to false at some point. This will probably be done by calling the turnOffFireplace() method. Whether this occurs concomitantly or is not an issue that you can not know by what is exposed in the code. If the method is not called to change the state of the variable on into another thread , I safely hope, then the only way to change the state of the variable inside the loop is by calling the direct method or indirectly.

    Directly not being called. You can see that. I think it is unlikely that he is being called indirectly. All called methods do not seem to be suitable for calling turnOffFireplace() , after all they are methods that should only circle.

    If you do not resolve this, you have no solution. When you must call I do not know. There is nothing in the question that makes this clear. Even if BlueJ does this, it's not clear how.

    An alternative is to have an event system controlling the state of the variable and the smoke display should occur by event controlled by the main loop in the game. But there's an engineering job that goes beyond the scope of the question.

    Typing error

    There is another serious problem in the code and even if it solves this engineering issue, it still will not work. The code:

    }while(on = true);
    

    is wrong, what you are doing there is assigning the value true to the variable on , so the while will always be true, even if the on value has been changed elsewhere, it will be wrong every time they go through the loop. This line should be:

    } while(on);
    

    If BlueJ turns to change the state of the variable in some way, then this should solve the problem.

    That's why I say it sucks to use variavel == true . There are people who argue that this is more readable. For me it is less readable and induces typographical errors, such as what happened.

        
    06.04.2016 / 21:14