2016-09-17 17 views
0

私はちょっと単純なマッチングパズルゲームに取り組んできました。現在のところ、カウントダウン時間と一致するタイルの数がテキストとして表示されており、完成した最高の時間(基本的にどのくらい速くパズルを完成したか)を作成しようとしています。しかし、私はそれを作成しようとするたびにテキストが表示されません。私はどこに変数 "txt"を配置する場合は、コードを記述して気づいた。私は "txt.text"を得るので、オートコンプリートボックスは利用可能なオプションとして.textと表示されます。私は時間を表示するために使用しているbestTimeTxt変数を記述するときにそのオプションを取得しません。私は何が間違っているのか分からない、ここに私のコードです。JavaScriptのテキストが表示されない

<!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 = 300; 
     bestTimeTxt.y = 0; 

     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; 

     bestTimeTxt.text = "test"; 

     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> 

答えて

0

私が持っていた問題は、テキストのxとyの位置が他のすべての背後に表示されていたことでした。

関連する問題