2014-02-13 2 views
0

최근에 나는 플라 펫 조류라고 불리는 codepen.io 게임을 만들었습니다. 거기에서 잘 작동하고 있었지만, 제 웹 사이트에 그것을 보여 주려고했을 때 아무 것도 나타나지 않았습니다.HTML5 기반 게임이 브라우저에 표시되지 않습니까?

누락 된 부분이나 내 코드의 문제점을 누군가가 알 수 있습니까? 여기에 코드입니다 (죄송합니다 그것이 내가 너무 오래 알고있어하지만 난 도움이 필요) :

<html> 
<head> 
<style type="text/css"> 
#stage { 
    display:block; 
    border:solid 1px #000; 
    margin:auto; 
} 
body{ 
    -webkit-touch-callout: none; 
    -webkit-user-select: none; 
    -khtml-user-select: none; 
    -moz-user-select: none; 
    -ms-user-select: none; 
    user-select: none; 

    background:#333; 
} 
</style> 
<script type="text/javascript"> 
window.requestAnimationFrame = (function(){ 
    return window.requestAnimationFrame || 
    window.mozRequestAnimationFrame ||        
    window.webkitRequestAnimationFrame || 
    window.msRequestAnimationFrame || 
    function(cb){ 
     return setTimeout(cb, 1000/60); 
    }; 
})() 

var can = document.getElementById("stage"), 
    ctx = can.getContext('2d'), 
    wid = can.width, 
    hei = can.height, 
    player, floor, pillars, gravity, thrust, running, 
    rainbows, colider, score, gPat, pPat, trans, termVel, pillGap, 
    pillWid, pillSpace, speed, stars, high, 
    sprite = document.createElement("img"); 
sprite.src = "http://www.cutmypic.com/uploads/title85083782.png"; 
//sprite.src = "http://i.stack.imgur.com/Vy3qB.gif"; 
sprite.onload = function(){ 
    sprite.style.height = 0; 
    loop(); 
}; 
sprite.width = 34; 
sprite.height = 21; 


document.body.appendChild(sprite); 

function init() { 
    high = localStorage.getItem("high") || 0; 

    player = { 
     x: 1/3 * wid, 
     y: 2/5 * hei, 
     r: 13, 
     v: 0 
    }; 
    speed = 2.5; 
    floor = 4/5 * hei; 
    pillars = []; 
    rainbows = []; 
    stars = []; 
    gravity = .30; 
    thrust = gravity * -21; 
    termVel = -thrust + 2; 
    running = false; 
    colider = false; 
    score = 0; 
    trans = 0; 
    pillGap = 135; 
    pillWid = 55; 
    pillSpace = pillWid*3; 
    pPat = ctx.createPattern((function(){ 
     var can = document.createElement("canvas"), 
      ctx = can.getContext("2d"); 

     can.width = 60; 
     can.height = 60; 

     ["green", "green", "green", 
     "#3b5998", "green", "#3b5998"].forEach(function(color, i){ 
      ctx.fillStyle = color; 

      ctx.beginPath(); 
      ctx.moveTo(i*10, 0); 
      ctx.lineTo(i*10+10, 0); 
      ctx.lineTo(0, i*10+10); 
      ctx.lineTo(0, i*10); 
      ctx.closePath(); 
      ctx.fill(); 

      ctx.beginPath(); 
      ctx.moveTo(i*10, 60); 
      ctx.lineTo(i*10+10, 60); 
      ctx.lineTo(60, i*10+10); 
      ctx.lineTo(60, i*10); 
      ctx.closePath(); 
      ctx.fill(); 
     }); 

     return can; 
    })(), "repeat"); 
    gPat = ctx.createPattern((function(){ 
     var can = document.createElement("canvas"), 
      ctx = can.getContext("2d"); 

     can.width = 32; 
     can.height = 32; 
     ctx.save(); 
     ctx.translate(16,16); 
     ctx.rotate(Math.PI/4); 
     ctx.fillStyle = "#79CDCD"; 
     ctx.fillRect(-64,-64,128,128); 
     ctx.fillStyle = "#528B8B"; 
     ctx.fillRect(-8,-64,8,128); 
     ctx.fillRect(14.5,-64,8,128); 
     ctx.restore(); 

     return can; 
    })(), "repeat"); 
} 


function render() { 
    trans -= speed; 
    rainbows = rainbows.filter(function(r){ 
     r.x -= speed; 
     return r.x > -speed; 
    }); 
    if (trans % speed === 0){ 
     rainbows.push({x:player.x-10, y:player.y - (trans%50/25|0)*2 - 1}); 
    } 

    stars = stars.filter(function(s){ 
     trans % 10 || (s.r += 1); 
     s.x -= speed; 
     return s.x > -speed && s.r < 10; 
    }); 
    if(trans % 20 === 0){ 
     stars.push({ 
     x: Math.round(Math.random()*(wid-50)+100), 
     y:Math.round(Math.random()*floor), 
     r:0 
     }); 
    } 


    // backdrop 
    ctx.fillStyle = "#418bbc"; 
    ctx.fillRect(0, 0, wid, hei); 

    //stars 
    ctx.fillStyle = "white"; 
    stars.forEach(function (s){ 
     ctx.fillRect(s.x, s.y - s.r-2, 2, s.r/2); 
     ctx.fillRect(s.x - s.r-2, s.y, s.r/2, 2); 
     ctx.fillRect(s.x, s.y+s.r + 2, 2, s.r/2); 
     ctx.fillRect(s.x+s.r + 2, s.y, s.r/2, 2); 

     ctx. fillRect(s.x + s.r, s.y + s.r, 2, 2); 
     ctx. fillRect(s.x - s.r, s.y - s.r, 2, 2); 
     ctx. fillRect(s.x + s.r, s.y - s.r, 2, 2); 
     ctx. fillRect(s.x - s.r, s.y + s.r, 2, 2); 

    }); 

    //ground 

    ctx.fillStyle = "#2F4F4F"; 
    ctx.fillRect(0, floor, wid, hei-floor); 

    ctx.save(); 
    ctx.translate(trans, 0); 

    //pillars 
    ctx.fillStyle = pPat; 
    ctx.strokeStyle = "#ccc"; 
    ctx.lineWidth = 2; 
    for (var i = 0; i < pillars.length; i++){ 
     var pill = pillars[i]; 
     ctx.fillRect(pill.x, pill.y, pill.w, pill.h); 
     ctx.strokeRect(pill.x, pill.y, pill.w, pill.h); 
    } 

    // stripe 
    ctx.fillStyle = gPat; 
    ctx.fillRect(-trans, floor+2, wid, 15); 
    ctx.restore(); 

    //rainbowwwwws 
    rainbows.forEach(function(r){ 

     ["red","orange","blue","green","blue","indigo"].forEach(function(color, i){ 
      ctx.fillStyle = color; 
      ctx.fillRect(r.x - speed, r.y-9 + i*3, speed+1, 3); 
     }); 
    }); 

    //player 
    ctx.save(); 
    ctx.translate(player.x, player.y); 
    ctx.rotate(player.v*Math.PI/18); 
    ctx.drawImage(sprite, - 17, - 10); 
    ctx.restore(); 

    ctx.fillStyle = "#97FFFF"; 
    ctx.fillRect(0, floor, wid, 2); 
    ctx.fillStyle = "#2F4F4F"; 
    ctx.fillRect(0, floor+1, wid, 1); 
    ctx.fillStyle = "#97FFFF"; 
    ctx.fillRect(0, floor+17, wid, 2); 
    ctx.fillStyle = "#2F4F4F"; 
    ctx.fillRect(0, floor+17, wid, 1); 


    //score 
    ctx.font = "bold 30px monospace"; 
    var hScore = "best:" + (score > high ? score : high), 
     sWid = ctx.measureText(hScore).width, 
     sY = 50; 

    ctx.fillStyle = "black"; 
    ctx.fillText(score, 12, floor + sY + 2); 
    ctx.fillText(hScore, wid - sWid - 10, floor + sY + 2); 
    ctx.fillStyle = "white"; 
    ctx.fillText(score, 10, floor + sY); 
    ctx.fillText(hScore, wid - sWid - 12, floor + sY); 
} 

function adjust() { 
    if (trans%pillSpace === 0){ 
     var h; 
     pillars.push({ 
      x: -trans + wid, 
      y: 0, 
      w: pillWid, 
      h: (h = Math.random() * (floor - 300) + 100) 
     }); 

     pillars.push({ 
      x: -trans + wid, 
      y: h + pillGap, 
      w: pillWid, 
      h: floor - h - pillGap 
     }); 
    } 

    pillars = pillars.filter(function(pill){ 

     return -trans < pill.x + pill.w; 
    }); 




    player.v += gravity; 
    if (player.v > termVel){ 
     player.v = termVel; 
    } 
    player.y += player.v; 

    if (player.y < player.r) { 
     player.y = player.r; 
     player.v = 0; 
    } 

    for(var i = 0; i < pillars.length; i++){ 
     var pill = pillars[i]; 
     if (pill.x + trans < player.x + player.r && 
      pill.x + pill.w + trans > player.x - player.r){   

      if (player.y - player.r > pill.y && 
       player.y - player.r < pill.y + pill.h){ 
       colider = true 
       running = false; 
       render(); 
       break; 
      } 
      if (player.y + player.r < pill.y + pill.h && 
       player.y + player.r > pill.y){ 
       colider = true 
       running = false; 
       render(); 
       break; 
      } 
      if (!pill.passed && i%2 == 1){ 
       score++; 
       pill.passed = true; 
      } 
     } 
    } 

    if (player.y + player.r - player.v > floor) { 
     player.y = floor - player.r; 
     running = false; 
     colider = true; 
     render(); 
    } 
} 

document.onmousedown = function() { 
    if (running) { 
     player.v = thrust; 
    } else if (!colider) { 
     running = true; 
    } else { 
     if (score > high){ 
      localStorage.setItem("high", score); 
     } 
     init(); 
    } 
}; 

</script> 
</head> 
<body> 
<canvas id="stage" width="400" height="600"></canvas> 
</body> 
+0

어떤 브라우저를 사용하십니까? 사용중인 기술로보고있는이 브라우저를 지원하는지 확인하십시오. –

+0

'loop()'가 호출되었지만 결코 함수를 생성하지 않았습니다. 무엇이 잘못되었는지보기 위해 콘솔을 보는 것이 항상 좋은 생각입니다. –

답변

2

귀하의 #stage는 DOM에 존재하는 이전 document.getElementById('stage')를 호출.

자바 스크립트는 페이지에로드 된 순서대로 실행됩니다. 따라서 Javascript를 HTML 문서의 맨 아래로 이동하거나 onload 이벤트 리스너로 이동할 수 있습니다.

자세히 말하자면 페이지가로드되면 페이지 상단의 항목이 먼저 들어 와서 구문 분석됩니다. <script> 태그가 발생하면 브라우저는 자동으로 자바 스크립트를 실행하여 페이지 본문에 도달합니다. CodePen은 페이지가 완전히로드 된 후 코드를 실행하도록 설정되었을 가능성이 있지만, 모든 페이지를 자신의 페이지로 이동하면로드하기 전에 실행 중입니다.