Billy But Whole
Billy But Whole

Reputation: 31

Function doesn't appear in browser

I'm trying to reproduce this game from the w3 website. My canvas shows but ever since I added movement functions my object (var myGamePiece) AKA red square, stopped showing. Feel free to inspect. I checked 10 times for any typos or anything but couldn't find any, code shows no errors. Seriously don't know what the hell is going on. I spent hours trying to make it work.

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <style>
    canvas {
        border: 1px solid #d3d3d3;
        background-color: #f1f1f1;
    }
    </style>
</head>
<body onload="startGame()">

<script>
    
function startGame() {
    myGameArea.start();
}

var myGameArea = {
    canvas: document.createElement("canvas"),
    start : function() {
        this.canvas.width = 600;
        this.canvas.height = 600;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
    }
}

var myGamePiece;
function startGame () {
    myGameArea.start();
    myGamePiece = new component(30, 30, "red", 10, 120);
}


function component(width, height, color, x, y) {
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;
    this.x = x;
    this.y = y;
    this.update = function() {
      ctx = myGameArea.context;
      ctx.fillStyle = color;
      ctx.fillRect(this.x, this.y, this.width, this.height);
    }

    this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;
    }
}

function updateGameArea() {
    myGameArea.clear();
    myGamePiece.newPos();
    myGamePiece.update();
}

function moveup() {
    myGamePiece.speedY -= 1;
}

function movedown() {
    myGamePiece.speedY += 1;
}

function moveleft() {
    myGamePiece.speedX -= 1;
}

function moveright() {
    myGamePiece.speedX += 1;
}

</script>

<button onclick="moveup()">UP</button>
<button onclick="movedown()">DOWN</button>
<button onclick="moveleft()">LEFT</button>
<button onclick="moveright()">RIGHT</button>

</body>
</html>

Upvotes: 1

Views: 90

Answers (2)

Aimal Khan
Aimal Khan

Reputation: 449

You need to call updateGameArea at start and after moveup, moveDown, moveLeft and moveRight

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <style>
    canvas {
        border: 1px solid #d3d3d3;
        background-color: #f1f1f1;
    }
    </style>
</head>
<body>
<script>
    


var myGameArea = {
    canvas: document.createElement("canvas"),
    start : function() {
        this.canvas.width = 600;
        this.canvas.height = 600;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
    }
}
startGame();

var myGamePiece;
function startGame () {
    myGameArea.start();
    myGamePiece = new component(30, 30, "red", 10, 120);
    updateGameArea();
}


function component(width, height, color, x, y) {
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;
    this.x = x;
    this.y = y;
    this.update = function() {
      ctx = myGameArea.context;
      ctx.fillStyle = color;
      ctx.fillRect(this.x, this.y, this.width, this.height);
    }  
    this.clear = function() {
      ctx = myGameArea.context;
      ctx.clearRect(0, 0, 600, 600);
    }
    
    
    this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;
    }
}

function updateGameArea() {
    myGamePiece.clear();
    myGamePiece.newPos();
    myGamePiece.update();
}

function moveup() {
    myGamePiece.speedY -= 1;
    updateGameArea();
}

function movedown() {
    myGamePiece.speedY += 1;
    updateGameArea();
}

function moveleft() {
    myGamePiece.speedX -= 1;
    updateGameArea();
}

function moveright() {
    myGamePiece.speedX += 1;
    updateGameArea();
}

</script>

<button onclick="moveup()">UP</button>
<button onclick="movedown()">DOWN</button>
<button onclick="moveleft()">LEFT</button>
<button onclick="moveright()">RIGHT</button>

</body>
</html>

Upvotes: 1

Ashique
Ashique

Reputation: 355

You have written multiple startGame function. please remove the top one. Also the main problem is that you forgot to update game area after an interval and a clear function in myGameArea object. so the code should be like below-

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <style>
    canvas {
        border: 1px solid #d3d3d3;
        background-color: #f1f1f1;
    }
    </style>
</head>
<body onload="startGame()">

<script>

var myGameArea = {
    canvas: document.createElement("canvas"),
    start : function() {
        this.canvas.width = 600;
        this.canvas.height = 600;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.interval = setInterval(updateGameArea, 20);
    },
    clear : function() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
}

var myGamePiece;
function startGame () {
    myGameArea.start();
    myGamePiece = new component(30, 30, "red", 10, 120);
}


function component(width, height, color, x, y) {
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;
    this.x = x;
    this.y = y;
    this.update = function() {
      ctx = myGameArea.context;
      ctx.fillStyle = color;
      ctx.fillRect(this.x, this.y, this.width, this.height);
    }

    this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;
    }
}

function updateGameArea() {
    myGameArea.clear();
    myGamePiece.newPos();
    myGamePiece.update();
}

function moveup() {
    myGamePiece.speedY -= 1;
}

function movedown() {
    myGamePiece.speedY += 1;
}

function moveleft() {
    myGamePiece.speedX -= 1;
}

function moveright() {
    myGamePiece.speedX += 1;
}

</script>

<button onclick="moveup()">UP</button>
<button onclick="movedown()">DOWN</button>
<button onclick="moveleft()">LEFT</button>
<button onclick="moveright()">RIGHT</button>

</body>
</html>

Codepen: https://codepen.io/ashfaq_haq/pen/MWWvKWq?editors=1000

Upvotes: 2

Related Questions