javascript – 将skifree添加为复活节彩蛋有麻烦

前端之家收集整理的这篇文章主要介绍了javascript – 将skifree添加为复活节彩蛋有麻烦前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想使用我已经在我的网站上成功使用的konami代码实现它.我只是不能让它与skifree一起工作.
为了快速参考,这里是konami代码
var kkeys = [],konami = "38,38,40,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,canvas.width,canvas.height);


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

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

        ctx.fillRect (-10-locX,10,canvas.height);
        ctx.fillRect (640*2+20-locX,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,locY);
        }
        else if(right){
            ctx.drawImage(guyRight,locY);
        }
        else{
            ctx.drawImage(guy,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);

解决方法

不确定这一点,但您的复制代码看起来错误;看这里:
if(left){
        if(locX > -320){
            locX--;
        }
    }
    if(right){
        if(locX < 640+320){
            locX++;
        }
    }

你从html复制了这个? > &安培; <角色不应该被逃脱:)

编辑

还要记得更新您的图像路径:D

编辑

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

的setTimeout( “runSki();”

的setTimeout(runSki

原文链接:https://www.f2er.com/js/153138.html

猜你在找的JavaScript相关文章