hi, i'm making an object (box4) in my code move, but in doing so my program now displays nothing (simple white screen). can anyone see anything obviously wrong in my code? i apologize profusely if this is rude.
my html document:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<canvas id="canvas" width="600" height="600"></canvas>
<script src="sprites/box.js"></script>
<script src="sprites/box2.js"></script>
<script src="sprites/box3.js"></script>
<script src="sprites/box4.js"></script>
<script src="sprites/arrow.js"></script>
<script src="classes/keycode.js"></script>
<script>
window.onload = function () {
setInterval(update, 1000 / 30);
var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
arrow = new Arrow(),
box = new Box();
box2 = new Box2();
box3 = new Box3();
box4 = new Box4();
holdLeft = holdRight = false;
box.X = 80;
box.Y = 40;
box2.X = 80;
box2.Y = 40;
box3.X = 150;
box3.Y = 150;
box4.X = 200;
box4.Y = 200;
arrow.X = 300;
arrow.Y = 300;
function update() {
if (holdLeft) {
box4.VX = -4;
}
if (holdRight) {
box4.VX = 4;
}
box4.PX += box4.VX;
box4.PY += Box4.VY;
if (box4.ONG) {
box4.VX *= 0.8;
} else {
box4.VY += box4.GRAV;
}
}
function update() {
if (holdLeft) {
box4.VX = -4;
}
if (holdRight) {
box4.VX = 4;
}
box4.PX += box4.VX;
box4.PY += box4.VY;
if (onG) {
box4.VX *= 0.8;
} else {
box4.VY += box4.GRAV;
}
function KeyboardEventHandler(event) {
if (event.keyCode == keycode.LEFT) {
holdLeft = true;
}
if (event.keyCode == keycode.RIGHT) {
holdRight = true;
}
if (event.keyCode == keycode.UP) {
if(yv<-3) {
yv=-3;
}
if (event.keyCode == keycode.DOWN) {
}
}
window.addEventListener('keydown', KeyboardEventHandler, false);
drawFrame();
function drawFrame() {
context.clearRect(0, 0, canvas.width, canvas.height);
box.Y += box.VY;
box.X += box.VX;
box2.Y += box2.VY;
box2.X += box2.VX;
box3.Y += box3.VY;
box3.X += box3.VX;
if (box.Y - box.Size < 0 | box.Y + box.Size > canvas.height) {
box.VY = -box.VY;
}
if (box.X - box.Size < 0 | box.X + box.Size > canvas.width) {
box.VX = -box.VX;
}
if (box2.Y - box2.Size < 0 | box2.Y + box2.Size > canvas.height) {
box2.VY = -box2.VY;
}
if (box2.X - box2.Size < 0 | box2.X + box2.Size > canvas.width) {
box2.VX = -box2.VX;
}
if (box3.Y - box3.Size < 0 | box3.Y + box3.Size > canvas.height) {
box3.VY = -box3.VY;
}
if (box3.X - box3.Size < 0 | box3.X + box3.Size > canvas.width) {
box3.VX = -box3.VX;
}
var dx = box.X - arrow.X;
var dy = box.Y - arrow.Y;
arrow.Rotation = Math.atan2(dy, dx);
box.draw(context);
box2.draw(context);
box3.draw(context);
box4.draw(context);
window.requestAnimationFrame(drawFrame, canvas);
}
}
}
</script>
</body>
</html>
What I have tried:
box4's class:
function Box4() {
var x = 0,
y = 0,
color = "red",
size = 20,
px=py=200;
vx=vy=0;
grav=0.5;
onG=false;
Object.defineProperty(this, 'VX',
{
get: function () {
return vx;
},
set: function (value) {
vx = value;
}
}
)
Object.defineProperty(this, 'PX',
{
get: function () {
return px;
},
set: function (value) {
px = value;
}
}
)
Object.defineProperty(this, 'PY',
{
get: function () {
return py;
},
set: function (value) {
py = value;
}
}
)
Object.defineProperty(this, 'GRAV',
{
get: function () {
return grav;
},
set: function (value) {
grav = value;
}
}
)
Object.defineProperty(this, 'ONG',
{
get: function () {
return onG;
},
set: function (value) {
onG = value;
}
}
)
Object.defineProperty(this, 'VY',
{
get: function () {
return vy;
},
set: function (value) {
vy = value;
}
}
)
Object.defineProperty(this, 'Size',
{
get: function () {
return size;
},
set: function (value) {
size = value;
}
}
)
Object.defineProperty(this, 'X',
{
get: function () {
return x;
},
set: function (value) {
x = value;
}
}
)
Object.defineProperty(this, 'Y',
{
get: function () {
return y;
},
set: function (value) {
y = value;
}
}
)
Box4.prototype.draw = function (context) {
context.save();
context.translate(x, y);
context.lineWidth = 2;
context.fillStyle = colour;
context.beginPath();
context.moveTo(-size, -size);
context.lineTo(-size, size);
context.lineTo(size, size);
context.lineTo(size, -size);
context.closePath();
context.fill();
context.stroke();
context.restore();
};
}