2016-09-18 6 views
0

작은 일치하는 퍼즐 게임에서 일하는 중풍. 현재 남은 시간과 macthes 인 타일 수에 대한 텍스트를 표시합니다. 나는 최고의 완료 시간 (사람이 얼마나 빨리 게임을 끝내는 지)을 위해 계속해서 만들려고 노력하고 있지만, 많은 문제가있다. "txt"와 "matchesFoundText"라는 두 변수를 입력 할 때 자동 완성 상자가 팝업되고 ".text"가 옵션 중 하나로 표시되므로 "txt.text"와 같은 것을 얻을 수 있습니다. 그러나 나는 그렇지 않습니다. "bestTimeTxt"에 대한 모든 옵션을 얻었고 왜 이런 일이 일어나고 있는지 전혀 알지 못합니다. 그래서 "bestTimeTxt"를 선택할 수없는 이유는 무엇입니까? . 여기 내 전체 스크립트입니다.easeljs에서 텍스트를 만들 수 없습니다.

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Recipe: Drawing a square</title> 
    <script src="easel.js"></script> 
    <script type="text/javascript"> 


     var canvas; 
     var stage; 
     var squareSide = 70; 
     var squareOutline = 5; 
     var max_rgb_color_value = 255; 
     var gray = Graphics.getRGB(20, 20, 20); 
     var placementArray = []; 
     var tileClicked; 
     var timeAllowable; 
     var totalMatchesPossible; 
     var matchesFound; 
     var txt; 
     var bestTime; 
     var bestTimeTxt; 
     var matchesFoundText; 
     var squares; 

     function init() { 
     var rows = 5; 
     var columns = 6; 
     var squarePadding = 10; 

     canvas = document.getElementById('myCanvas'); 



     stage = new Stage(canvas); 

     var numberOfTiles = rows*columns; 

     matchesFound = 0; 

     timeAllowable = 5; 

     bestTime = 0; 

     txt = new Text(timeAllowable, "30px Monospace", "#000"); 
     txt.textBaseline = "top"; // draw text relative to the top of the em box. 
     txt.x = 500; 
     txt.y = 0; 

     bestTimeTxt = new Text(bestTime, "30px Monospace", "#000"); 
     bestTimeTxt.textBaseLine = "top"; 
     bestTimeTxt.x = 500; 
     bestTimeTxt.y = 80; 



     stage.addChild(txt); 
     stage.addChild(bestTimeTxt); 

     squares = []; 

     totalMatchesPossible = numberOfTiles/2; 

     Ticker.init(); 
     Ticker.addListener(window); 
     Ticker.setPaused(false); 

     matchesFoundText = new Text("Pairs Found: "+matchesFound+"/"+totalMatchesPossible, "30px Monospace", "#000"); 
     matchesFoundText.textBaseline = "top"; // draw text relative to the top of the em box. 
     matchesFoundText.x = 500; 
     matchesFoundText.y = 40; 



     stage.addChild(matchesFoundText); 

     setPlacementArray(numberOfTiles); 

     for(var i=0;i<numberOfTiles;i++){ 
      var placement = getRandomPlacement(placementArray); 
      if (i % 2 === 0){ 
      var color = randomColor(); 
      } 
      var square = drawSquare(gray); 
      square.color = color; 
      square.x = (squareSide+squarePadding) * (placement % columns); 
      square.y = (squareSide+squarePadding) * Math.floor(placement/columns); 
      squares.push(square); 
      stage.addChild(square); 
      square.cache(0, 0, squareSide + squarePadding, squareSide + squarePadding); 
      square.onPress = handleOnPress; 
      stage.update(); 
     }; 
     } 


     function drawSquare(color) { 
     var shape = new Shape(); 
     var graphics = shape.graphics; 

     graphics.setStrokeStyle(squareOutline); 
     graphics.beginStroke(gray); 
     graphics.beginFill(color); 
     graphics.rect(squareOutline, squareOutline, squareSide, squareSide); 

     return shape; 


     } 


     function randomColor(){ 
     var color = Math.floor(Math.random()*255); 
     var color2 = Math.floor(Math.random()*255); 
     var color3 = Math.floor(Math.random()*255); 
     return Graphics.getRGB(color, color2, color3) 
     } 


     function setPlacementArray(numberOfTiles){ 
     for(var i = 0;i< numberOfTiles;i++){ 
      placementArray.push(i); 
     } 
     } 


     function getRandomPlacement(placementArray){ 
     randomNumber = Math.floor(Math.random()*placementArray.length); 
     return placementArray.splice(randomNumber, 1)[0]; 
     } 


     function handleOnPress(event){ 
     var tile = event.target; 

     tile.graphics.beginFill(tile.color).rect(squareOutline, squareOutline, squareSide, squareSide); 

     if(!!tileClicked === false || tileClicked === tile){ 
      tileClicked = tile; 
      tileClicked.updateCache("source-overlay"); 
     }else{ 
      if(tileClicked.color === tile.color && tileClicked !== tile){ 
      tileClicked.visible = false; 
      tile.visible = false; 
      matchesFound++; 
      matchesFoundText.text = "Pairs Found: "+matchesFound+"/"+totalMatchesPossible; 
      if (matchesFound===totalMatchesPossible){ 
       gameOver(true); 
      } 
      }else{ 
      tileClicked.graphics.beginFill(gray).rect(squareOutline, squareOutline, squareSide, squareSide); 
      } 
      tileClicked.updateCache("source-overlay"); 
      tile.updateCache("source-overlay"); 
      tileClicked = tile; 
     } 
     stage.update(); 
     } 


     function tick() { 
     secondsLeft = Math.floor((timeAllowable-Ticker.getTime()/1000)); 

     txt.text = secondsLeft; 

     ; 

     if (secondsLeft <= 0){ 
      gameOver(false); 
     } 
     stage.update(); 
     } 


     function gameOver(win){ 




     Ticker.setPaused(true); 

     for(var i=0;i<squares.length;i++){ 
      squares[i].graphics.beginFill(squares[i].color).rect(5, 5, 70, 70); 
      squares[i].onPress = null; 
      if (win === false){ 
      squares[i].uncache(); 
      } 
     } 

     var replayParagraph = document.getElementById("replay"); 

     replayParagraph.innerHTML = "<a href='#' onClick='history.go(0);'>Play Again?</a>"; 

     if (win === true){ 
      matchesFoundText.text = "You win!" 
     }else{ 
      txt.text = secondsLeft + "... Game Over"; 

     } 
     } 


     function replay(){ 
     init(); 
     } 


    </script> 
</head> 
<body onload="init()"> 
    <header id="header"> 
    <p id="replay"></p> 
    </header> 
    <canvas id="myCanvas" width="960" height="400"></canvas> 
</body> 
</html> 

답변

1

좋아요 그래서 x 및 bestTimeTxt 변수의 y 위치 그것이 다른 모든 것들의 위치에 의해 가려 질 원인이 되었기 때문에 분명히 내가 문제가 된 이유입니다.