+ 2
Error on my code. What is the Error??
Please Help me with this error on my code. https://code.sololearn.com/WGRKLC32qxK0/?ref=app
5 Respuestas
+ 2
there was a "}" too much some this. errors and I changed it so the js runs as soon as the window loads
I tested it and it worked
hope this helped a little 😅
Happy Coding! 😄
+ 4
thanks Anton Böhler ! what was the error?
+ 1
now there is no error
but I couldn't test it work; since I'm on phone
+ 1
JS (second half):
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.color = color;
this.update = function(){
ctx = myGameArea.context;
ctx.fillStyle = this.color;
ctx.fillRect(this.x, this.y, this.width, this.height);
}
this.crashWith = function(otherobj) {
var myleft = this.x;
var myright = this.x + (this.width);
var mytop = this.y;
var mybottom = this.y + (this.height);
var otherleft = otherobj.x;
var otherright = otherobj.x + (otherobj.width);
var othertop = otherobj.y;
var otherbottom = otherobj.y + (otherobj.height);
var crash = true;
if(mybottom < othertop ||
mytop > otherbottom || myright < otherleft || myleft > otherright){
crash = false;
}
return crash;
}
this.newPos = function(){
this.x += this.speedX;
this.y += this.speedY;
}
}
function updateGameArea(){
if(myGamePiece.crashWith(myObstacle)){
myGameArea.stop();
}else{
myGameArea.clear();
myGamePiece.newPos();
myObstacle.update();
myGamePiece.update();
}
}
function moveup(){
myGamePiece.speedY -= 1;
}
function movedown() {
myGamePiece.speedY +=1;
}
function moveright() {
myGamePiece.speedX +=1;
}
function moveleft() {
myGamePiece.speedX -=1;
}
function stopmove() {
myGamePiece.speedX = 0;
myGamePiece.speedY = 0;
}
0
JS (first half):
var myGamePiece;
var myObstacle;
var myGameArea;
window.onload = function(){
myGameArea = {
canvas: document.createElement("canvas"),
start: function() {
this.canvas.width = 480;
this.canvas.height = 270;
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);
},
stop: function(){
clearInterval(this.Interval);
}
};
startGame();
}
function startGame() {
console.log("hey");
myGameArea.start();
myObstacle = new component(10, 200, "green", 300, 120);
myGamePiece = new component(30, 30, "red", 10, 120);
}