Gambai
Gambai

Reputation: 690

javascript animation sometimes works

I'm trying to make pacman move without using jquery.animation because I want more control. so I'm using setInterval, but it only works sometimes. if you refresh enough, it will eventually "click on" and work fine, but if you refresh again, it won't work, it's here http://pacman.townsendwebdd.com if you want to look at it, thank you

//earlier in the code
this.moveInterval = setInterval(_this.move, 40, _this);

move: function(_this)
{
    if(_this.pause)//set to true for now
        return false;

    var horz = 0;
    var vert = 0;

    var dir = _this.dir;

    //set horizontal and vertical directions
    if(dir % 2 == 0)
        horz = dir - 1;
    else
        vert = dir - 2;

    _this.top += vert;
    _this.left += horz;

    $('#pacman').css('top', _this.top);
    $('#pacman').css('left', _this.left);
},

Upvotes: 0

Views: 146

Answers (1)

Griffork
Griffork

Reputation: 682

I'm not sure if this will fix the problem, but I would recommend using request animation frame instead of setInterval.

The other thing I think would be the problem (and I've been stung by this too) is that you're possibly trying to start the animation before the page has fully loaded. Try putting your code into a function and calling it with the onload attribute of the body tag.

Good luck!

Griffork.

Upvotes: 1

Related Questions