Call a method only once from a class

I have a form class that has a ( hitTest(int,int)

) method that constantly checks if the mouse is within its bounds or not. In another method, I keep checking if the mouse remains there for more than 1 second.
If it has, triggers a function (via a notification / event) that triggers the animation
This is not the case, then do not start the animation
If it has already activated the animation and the animation is running, but the mouse leaves the area during this, triggers the interrupt function (via notification / event)

//OnLoad _initHover = false;

void update() //called continously in the application per frame
{
if(hitTest(getMouseX(), getMouseY())){
                if(!_initHover){
                    _initHover = true;
                    _hoverStartTime = getCurrentTime(); //start hover time
                    cout<<"Start hist test\n";
                }

                //If it has hovered over the video for 1.0 sec
                if((ofGetElapsedTimef() - _hoverStartTime) > 1.0){
                    cout<<"Hitting continously for 1 sec\n";
                    notificationCenter->postNotification(new AnimationStartNotification);
                }
            }
            else{
                    _initHover = false;
                    notificationCenter->postNotification(new AnimationInterruptNotification);
            }
}

      

The above code works fine, but there is a problem I am running into when trying to use. There are multiple instances of the above Shape class, and each class has its own method update()

. The mouse cursor has animationStarthandler

and animationStophandlers

is one class throughout the application.

Problem 1: So even if one of the shapes only reports an error animationStarthandler

, the other shape classes that have hit criterion set the animation to interrupt

and the animation doesn't work.
Problem 2: When the luck test succeeds and the cursor is in an area larger than 1 sec

, the success test will continue to send a notification to start the animation (animation duration 1.5s approx.) How to limit the hit test to start the animation only once and continue repeat the same animation over and over?

If, in the main method of my application, I directly try to start the animation by calling a method playAnimation

on the pointer class, I get the desired result. But I want this time and hover animation setting to be the ShapeClass itself. Any suggestions?

+3


source to share


1 answer


I think you should add a new boolean value that contains information about the animation start (called in the code example _animationTriggered

). This prevents shapes that did not trigger the animation from appearing to stop it, and the animation that caused it to do it multiple times.

if(hitTest(getMouseX(), getMouseY()))
{
    if(!_initHover)
    {
        _initHover = true;
        _hoverStartTime = getCurrentTime();
        cout<<"Start hist test\n";
    }

    if((ofGetElapsedTimef() - _hoverStartTime) > 1.0)
    {
        if (!_animationTriggered)
        {
            cout<<"Hitting continously for 1 sec\n";
            notificationCenter->postNotification(new AnimationStartNotification);
            _animationTriggered = true;
        }
    }
}
else
{
    if ( _animationTriggered )
    {
        _initHover = false;
        notificationCenter->postNotification(new AnimationInterruptNotification);
        _animationTriggered = false;
    }
}

      



Don't forget to initialize this new boolean value in the same place as _initHover

+2


source







All Articles