Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8391075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:09:52+00:00 2026-06-09T19:09:52+00:00

I have written a JavaScript program that generates a solution to a NxN sliding

  • 0

I have written a JavaScript program that generates a solution to a NxN sliding tile puzzle. It finds a correct solution but when it comes to animating between the different puzzle instances to get to the solution things get messy. At first (3-4 steps) the tiles moves like they should but then some tiles start moving diagonally and above other tiles. The solution is finally reached but the inbetween animations goes wrong. Here is the code for my animation loop:

var steps = solution.length;
for (i=0; i<steps; i++) {


    next = solution.pop();
    // ANIMATE TILE
    var delay = speed*i;
        // sp is an array where the position (y, x) of the free spot is
    var spA = current.spacePos;     
    var spB = next.spacePos;
    var movingTile = current.box[spB[0]][spB[1]];
    var leftOffset = size*(spA[1]-spB[1]);
    var topOffset = size*(spA[0]-spB[0]);
    var thisTile = $('#tile'+movingTile);
    thisTile.delay(delay).animate({
        opacity: 1.0,
        top: "+=" + topOffset,
        left: "+=" + leftOffset
        }, speed);
    // Update for next round
    current = next;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:09:53+00:00Added an answer on June 9, 2026 at 7:09 pm

    If you want the timing of the next animation to exactly coincide with the finish of the previous animation (which you do), then you cannot use a delay() to schedule the next animation. Instead, you HAVE to use the completion function from one animation to trigger the next animation. Only then will the timing be perfect, even after hundreds of consecutive animations.

    You could do that something like this:

    var current = xxx;   // this has to get initalized (I'm not sure how in your code)
    function goNext() {
        if (!solution.length) {
            return;
        }
    
        var next = solution.pop();
        // ANIMATE TILE
        // sp is an array where the position (y, x) of the free spot is
        var spA = current.spacePos;     
        var spB = next.spacePos;
        var movingTile = current.box[spB[0]][spB[1]];
        var leftOffset = size*(spA[1]-spB[1]);
        var topOffset = size*(spA[0]-spB[0]);
        var thisTile = $('#tile'+movingTile);
        var current = next;
        thisTile.animate({
            opacity: 1.0,
            top: "+=" + topOffset,
            left: "+=" + leftOffset
            }, speed, goNext);
    }
    
    goNext();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have written a snake program using javascript.. the problem is that the snake
HI, I have a JavaScript program that's written with object literal syntax: var MyJsProgram
I have written a javascript function that uses setInterval to manipulate a string every
I have written some JavaScript and jQuery code that accepts only numeric input in
I have written a program in Javascript which reads numbers from a file ,sorts
I have written some javascript code that's supposed to put options into a select
I have written some JavaScript that opens an element when an element is clicked.
I have written a set of javascript functions that allow me to validate user
I have written a javascript that works well in all browser except Internet Explorer
I have written some JavaScript code that will read from the Google Maps API

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.