Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke a method only once from a class

I have a shape class in which there is a method (hitTest(int,int)) that continuously checks if the mouse is inside its bounds or not. In another method, I keep on checking if the mouse has stayed there for more than 1 sec.
If it has, trigger a function (by notification/event) that runs animation
It it has not, then don't trigger the animation
If it has already triggered the animation and the animation is running but the mouse leaves the area during this, trigger an interrupt function (by 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 runs fine but there's a logical issue I am facing while trying to use. There are multiple instances of the above Shape class and each class consequently has their update() method as well. The mouse cursor has which has animationStarthandler and animationStophandlers is a single class in the whole application.

Issue 1: So, even when one of the shape just notifies the animationStarthandler to fire, the other shape classes on which hit test is false set the animation to interrupt and the animation does not run.
Issue 2: When the hit test succeeds and the cursor has been in the area for more than 1 sec, the hit test will keep on sending the notification to start the animation (anim's duration 1.5 sec approx.) How do I restrict the hit test to fire the animation only once and keep on firing the same animation again and again?

If in the main method of my application, I directly try to fire the animation by calling the method playAnimation in the pointer class, I get the required result. But I want to give this hover timing and animation functionality to the ShapeClass itself. Any suggestions?

like image 834
user1240679 Avatar asked Dec 14 '25 19:12

user1240679


1 Answers

I think that you should consider adding a new boolean, which holds the information of the triggering of the animation (called in the code sample _animationTriggered). This prevents shapes that have not triggered the animation to stop it and the animation that triggered it to make it several 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 initialie this new boolean in the same place as _initHover

like image 54
Dr_Sam Avatar answered Dec 17 '25 11:12

Dr_Sam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!