无法添加skifree作为复活节彩蛋

时间:2012-08-01 12:47:39

标签: javascript jquery

我想使用konami代码实现它,我已经在我的网站上成功使用了它。我无法让它与skifree一起工作。 为了快速参考,这是konami代码:

var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
$(document).keydown(function(e) {
 kkeys.push( e.keyCode );
if ( kkeys.toString().indexOf( konami ) >= 0 ){
$(document).unbind('keydown',arguments.callee);
// Launch easter egg here
  }
});

以下是来自此网站的skifree代码:http://timelessname.com/canvas/skifree/

    var left;
    var right;
    var faster = false;

    var step = 0;
    var obst = new Array();

    var locX = 430;
    var locY = 100;

    var running = true;

    var guy = new Image();
    var guyLeft = new Image();
    var guyRight = new Image();
    var crash = new Image();
    var rock = new Image();
    var tree = new Image();
    var bush = new Image();
    guy.src = "http://timelessname.com/canvas/skifree/guy_down.png";
    guyLeft.src = "http://timelessname.com/canvas/skifree/guy_left.png";
    guyRight.src = "http://timelessname.com/canvas/skifree/guy_right.png";
    crash.src = "http://timelessname.com/canvas/skifree/crash.png";
    rock.src = "http://timelessname.com/canvas/skifree/rock.png";
    tree.src = "http://timelessname.com/canvas/skifree/tree.png";

    $(window).keydown(function(e){
        if(e.keyCode == 37){
            left = true;
        }
        else if(e.keyCode == 39){
            right = true;
        }
        else if(e.keyCode == 70){
            faster = true;
        }
    });
    $(window).keyup(function(e){
        if(e.keyCode == 37){
            left = false;
        }
        else if(e.keyCode == 39){
            right = false;
        }
        else if(e.keyCode == 70){
            faster = false;
        }
        else if(e.keyCode == 32){
            if(!running){
                step = 0;
                obst = new Array();
                locX = 430;
                locY = 100;
                running = true;
                runSki();
            }
        }
    });




    //TODO: wrap edges (no wall)

    function runSki(){
        if(!running) return;
        var canvas = document.getElementById("can");
        var ctx = canvas.getContext("2d");

        ctx.fillStyle = "rgb(255,255,255)";
        ctx.fillRect (0, 0, canvas.width, canvas.height);


        if(left){
            if(locX > -320){
                locX--;
            }
        }
        if(right){
            if(locX < 640+320){
                locX++;
            }
        }

        ctx.fillStyle = "rgb(0,0,0)";

        ctx.fillRect (-10-locX, 0, 10, canvas.height);
        ctx.fillRect (640*2+20-locX, 0, 10, canvas.height);


        for(var i = 0; i < obst.length;i++){
            var o = obst[i];
            o.y-=2.5;
            if(faster){
                o.y-=2.5;
            }
            ctx.drawImage(o.type,o.x-locX,o.y);
            if(o.y < -30){
              obst.splice(i,1);
              i--;
            }

            var tX = o.x-locX+5;
            var tY = o.y+5;

            var d = Math.sqrt((tX-320+3)*(tX-320+3)+ (tY-100+5)*(tY-100+5));
            if(d < 20){
                ctx.drawImage(crash,320,locY);
                running = false;
            }
        }
        if(running){
        if(left){
            ctx.drawImage(guyLeft,320,locY);
        }
        else if(right){
            ctx.drawImage(guyRight,320,locY);
        }
        else{
            ctx.drawImage(guy,320,locY);
        }
        }

        var randomnumber=Math.floor(Math.random()*641)
        if(Math.floor(step*10)%10==0){
            var type;
            if(Math.floor(Math.random()*2) == 0){
              type = rock;
            }
            else{
              type = tree;
            }
            var obj = {x: Math.floor(Math.random()*641*2), y:480, type: type};
            obst.push(obj);
        }
        step+= 0.1;
        if(running){
            setTimeout("runSki();",1); 
        }
    }
    setTimeout("runSki();",1000);

1 个答案:

答案 0 :(得分:3)

不确定,但您复制的代码看起来不对;看到这里:

    if(left){
        if(locX > -320){
            locX--;
        }
    }
    if(right){
        if(locX < 640+320){
            locX++;
        }
    }

你有没有从html复制过这个? >&amp;不应转义<个字符:)

修改

另请记住更新图像路径:D

修改

我让这个在这个页面上工作。刚刚添加了<canvas/>元素,将滑雪javascript包装在一个名为setupSki的函数中(由konami函数调用)并替换了

的两个实例

setTimeout("runSki();"

setTimeout(runSki