I am working on a beginner project and I have extreme trouble creating a simple start screen. I want to have a blank screen with a play button and a title. Pressing the play button should cause the header and button to disappear and the cards to be shown. However, when I press the play button I get this error:
Uncaught TypeError: GameLoop.start is not a function
at enterGame (blackjack.js:30:14)
at HTMLButtonElement.onclick (index.html:16:43)
I cannot figure out what this means as start is definitely a function in GameLoop. This is what I see now when I enter my game:
The dealer card and buttons show for some reason, unknown to me. Here is the code:
index.html:
<!DOCTYPE html>
<html>
<head>
<title>cwd.gameloop.js</title>
<meta charset="UTF-8">
<meta name="viewport" content=""width=device-width, initial-scale="1.0">
<title>Black Jack</title>
<link rel="stylesheet" href="blackjack.css">
<script src="blackjack.js"></script>
<script src="gameloop.js"></script>
</head>
<body>
<div id="start-screen" class="screen" style="display:block">
<h1>Welcome to the Blackjack trainer!</h1>
<button onclick="enterGame()">Play</button>
</div>
<canvas id="canvas" style="display:none"></canvas>
<h2>Dealer: <span id="dealer-sum"></span></h2>
<div id="dealer-cards">
<img id="hidden" src="./Card images/card_back.png">
</div>
<h2>You: <span id="player-sum"></span></h2>
<div id="player-cards"></div>
<br>
<button id="hit">Hit</button>
<button id="stand":>Stand</button>
<button id="double":>Double</button>
<button id="surr":>Surrender</button>
<p id="results"></p>
</body>
</html>
blackjack.css:
body {
font-family: Arial, Helvetica, sans-serif;
text-align: center;
color: white;
background-color: #225338;
}
#dealer-cards img {
height: 280px;
width: 200px;
margin: 1px;
}
#player-cards img {
height: 280px;
width: 200px;
margin: 1px;
}
#hit {
width: 100px;
height: 50px;
font-size: 20px;
}
#stand {
width: 100px;
height: 50px;
font-size: 20px;
}
#double {
width: 100px;
height: 50px;
font-size: 20px;
}
#surr {
width: 100px;
height: 50px;
font-size: 20px;
}
relevant parts of blackjack.js:
window.onload = function() {
}
window.onresize = function() {
GameLoop.onresize();
}
function enterGame() {
GameLoop.start();
}
gameloop.js:
class GameLoop {
constructor() {
this.fps = 60;
this.ctx = null;
this.cnv = null;
this.loop = null;
}
prepareCanvas() {
this.cnv = document.getElementById('canvas');
this.ctx = this.cnv.getContext('2d');
document.body.style.margin = 0;
document.body.style.padding = 0;
this.onresize();
}
onresize() {
if ( this.cnv ) {
this.cnv.width = window.innerWidth;
this.cnv.height = window.innerHeight;
this.resize();
}
}
start() {
this.toggleScreen('start-screen',false);
this.toggleScreen('canvas',true);
this.prepareCanvas();
this.init();
this.loop = setInterval(() => {
this.update();
this.render();
}, 1000/this.fps);
}
toggleScreen(id,toggle) {
let element = document.getElementById(id);
let display = ( toggle ) ? 'block' : 'none';
element.style.display = display;
}
I have been stuck on this for hours.
