0

I've got aplication 'self-sliding-puzzle' with AI on it etc. I will use it to make some cool portfolio gallery.

I've got one method that just animates cube:

this.animateSelection = function(direction,time)
{   
    if (time===undefined) this.time=150;    
    this.moveDistance=this.cubeSize+this.spacing;
    switch (direction)
    {   
        //move right
        case 'r': $('#' + selectionName).transition({ x: '+=' +this.moveDistance +'px' },this.time);
        break;

        //move down         
        case 'd': $('#' + selectionName).transition({ y: '+=' +this.moveDistance +'px' },this.time);            
        break;

        //move left         
        case 'l': $('#' + selectionName).transition({ x: '-=' +this.moveDistance +'px' },this.time);    
        break;

        //move up           
        case 'u': $('#' + selectionName).transition({ y: '-=' +this.moveDistance +'px' },this.time);        
        break;

        //if wrong direction is given
        default:  return false;
    }
    return true;        
}

Everything works cool when I call it single time.

But problem is:

when i do

myClass.animateSelection('d');
alert('bla bla bla');

alert is show before animation is done

My main AI loop executes in 50ms and manage about 200 moves, so calls method. I want that method to hold executing of loop till ANIMATION is done.

Callback dont make it. animation Method is used in many different situation and by various other methods. Its impossible to make it fit that way IMO.

1 Answer 1

1

If you use myClass.animateSelection the execution of the script won't stop when it hit $().transition. Instead transition will set up a queue of some simple transition effects which will be called in the event loop.

This is called asynchronous behavior and has the advantage that your script doesn't stop. However, most asynchronous functions will take a callback as optional parameter, which will be called after the functions desired effect has either ended or failed.

In your case, transition supports a callback as additional parameter:

$.fn.transition(options, [duration], [easing], [callback]);

You would have to change your animateSelection to take a callback argument:

this.animateSelection = function(direction,time,callback)
{   
    if (time===undefined) this.time=150;
    this.moveDistance=this.cubeSize+this.spacing;
    switch (direction)
    {   
        //move right
        case 'r': $('#' + selectionName).transition({ x: '+=' +this.moveDistance +'px' },this.time,callback);
        break;

        //move right            
        case 'd': $('#' + selectionName).transition({ y: '+=' +this.moveDistance +'px' },this.time,callback);
        break;

        //move right            
        case 'l': $('#' + selectionName).transition({ x: '-=' +this.moveDistance +'px' },this.time,callback);    
        break;

        //move right            
        case 'u': $('#' + selectionName).transition({ y: '-=' +this.moveDistance +'px' },this.time,callback);        
        break;

        //if wrong direction is given
        default:  return false;
    }
    return true;        
}

and use

myClass.animateSelection('d',undefined,function(){alert('bla bla bla');});

instead.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, but method is called once by 'findEscapePath' method, once by 'findNeighbourFreeField' once by 'moveBack' method, and many many more. Is there no way to change animateSelection ITSELF and dont change it on every place that it's called. Becouse it would be freaking complicated.
If you add a callback parameter, your other calls will still work as it could be optional, then you could add the callback reference where you need the function to be called after.
@user1576183: What box said. Most jQuery animations wont stop the execution of your code, so this is basically the only way to keep track of your functions' status.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.