0

Code isn't working, I need to randomly position a square component. I tried running some code, but nothing happens. Here's my code:

function component(width, height, color, x, y, type) {
    this.type = type;
    if (type == "image"){
 this.image = new Image();
 this.image.src = color;
    }
    this.score = 0;
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;    
    this.x = x;
    this.y = y;
    this.gravity = 0;
    this.gravitySpeed = 0;
    this.update = function() {
        ctx = myGameArea.context;
        if (this.type == "image"){
     ctx.drawImage(this.image,this.x, this.y, this.width,this.height);
        } else {
            ctx.fillStyle = color;
            ctx.fillRect(this.x, this.y, this.width, this.height);
        }
    }

}
function startGame() {
 random = Math.floor((Math.random()*100) + 1);
 document.write(random);

 random2 = Math.floor((Math.random()*100) + 1);
 document.write(random2);

 square = new component(random, random2, "green", random, random2);
myGameArea.start();
return square  
}


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

function updateGameArea() {
    
 myGameArea.clear();
 square.update();

}

For some reason, the function startGame doesn't work correctly. Nothing happens when I run the code. I opened the console log as well, and no syntax errors registered.

1 Answers1

0

You can get the code to work by applying it like this, for example:

function component(width, height, color, x, y, type) {
    this.type = type;
    if (type == "image"){
 this.image = new Image();
 this.image.src = color;
    }
    this.score = 0;
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;    
    this.x = x;
    this.y = y;
    this.gravity = 0;
    this.gravitySpeed = 0;
    this.color=color;
    this.update = function() {
       random = Math.floor((Math.random()*100) + 1);
       random2 = Math.floor((Math.random()*100) + 1);
       function getRandomColor() {
           var letters = '0123456789ABCDEF';
           var color = '#';
           for (var i = 0; i < 6; i++ ) {
              color += letters[Math.floor(Math.random() * 16)];
            }
           return color;
       }
       randcolor=getRandomColor();
        ctx = myGameArea.context;
        if (this.type == "image"){
     ctx.drawImage(this.image, random, random2, random,random2);
        } else {
            ctx.fillStyle = randcolor;
            ctx.fillRect(random, random2, random, random2);
        }
        this.x=random;
        this.y=random2;
        this.width=random;
        this.height=random2;
        this.color=randcolor;
    }

}
function startGame() {
 random = Math.floor((Math.random()*100) + 1);


 random2 = Math.floor((Math.random()*100) + 1);


 square = new component(random, random2, "green", random, random2);
myGameArea.start();
return square  
}


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

function updateGameArea() {
    
 myGameArea.clear();
 square.update();

}


document.getElementById("start").addEventListener('click', startGame)
<div id="start">press to start</div>

In here I did three things:

  • I added a text that you can click
  • I added an event listener on this text that invokes the startGame() function
  • I got rid of the document.write() statements. I built this is Jsfiddle, which doesn't allow it, and it's considered bad practice anyway.
H. Figueiredo
  • 828
  • 8
  • 17
Sventies
  • 1,204
  • 1
  • 14
  • 30