0

Hi im making a mario like game for fun and i am stuck with making mario animate himself, as in make his arms/legs move. I have the code for it, and if i break it down it works, however I have so far failed to space it so that it is actually visible. So far I have tried setTimeout and .delay however neither has worked

here is my js code at the moment with the .delay function.

function animateMario() {
    // cycles through other 4 images
    for (var i=1; i<5; i++) {
        $('#mario').attr('src', 'images/mario/mario_f_'+i+'.png').delay(100);
    }

    // returns to original image
    $('#mario').attr('src', 'images/mario/mario_f_0.png').delay(100);
};

If anyone could help it would be much appreciated :) Thank you very much If you have any questions please ask

4

2 回答 2

0

由于某种原因,延迟不适用于所有 jQuery 函数,因此您需要使用间隔。试试这个代码,它对我有用:

function animateMario() {
    var i = 1;
    var timer = setInterval(function(){
        $('#mario').attr('src', 'images/mario/mario_f_'+i+'.png');
        i++
        if(i==5){
            clearInterval(timer);
            $('#mario').delay(1000*(i+1)).attr('src', 'images/mario/mario_f_0.png');
        }
    }, 100)
};
于 2013-06-12T22:18:27.410 回答
0

这是我的解决方案。Jsfiddle 示例。 没有延迟,不需要 jQuery :) 查看简单的代码。

 //select the element with the #mario ID
 var el_image = document.getElementById('mario');

 //array with the image
 var img = [
            'http://u.cubeupload.com/FollowTheWeb/1.png',
            'http://u.cubeupload.com/FollowTheWeb/2.png',
            'http://u.cubeupload.com/FollowTheWeb/3.png',
            'http://u.cubeupload.com/FollowTheWeb/4.png',
            'http://u.cubeupload.com/FollowTheWeb/5.png'
           ];


 //functionfor create the animation
 var i=0;
 function frame_switch ()
 {
   el_image.src = img[i]; //change the src attribute

     if(i > 3){  //is the index is 4 set i=0
         i=0;
     }else{i++; }
 }


 setInterval(frame_switch,150);  //repeat the function frame_swith() each 150ms
于 2013-06-12T22:46:58.070 回答