2
\$\begingroup\$

this is my code

var myGamePiece;

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

var 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);
         window.addEventListener('keydown', function (e) {
            myGameArea.keys = (myGameArea.keys || []);
            myGameArea.keys[e.keyCode] = (e.type == "keydown");
        })
        window.addEventListener('keyup', function (e) {
            myGameArea.keys[e.keyCode] = (e.type == "keydown");            
        })
    },
    clear : function() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
    }

function component(width, height, color, x, y) {
    this.gamearea = myGameArea;
    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.speedX = 0;
    myGamePiece.speedY = 0;    
    if (myGameArea.keys && myGameArea.keys[37]) {myGamePiece.speedX = -5; }
    if (myGameArea.keys && myGameArea.keys[39]) {myGamePiece.speedX = 5; }
    if (myGameArea.keys && myGameArea.keys[38]) {myGamePiece.speedY = -5; }
    if (myGameArea.keys && myGameArea.keys[40]) {myGamePiece.speedY = 5; }
    if (myGamePiece < 299) {myGamePiece.speedX = -5}
    myGamePiece.newPos();    
    myGamePiece.update();
}

I want myGamePiece to look exactly were the mouse cursor is looking. How can I do this?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Ahhh, this took me awhile to figure out for my project, and I ended up stitching together about 5 different SO&SE GameDev posts and then modified it myself to end up with this code:

this.update = function(){
  ctx = myGameArea.context;
  ctx.fillStyle = color;
  //center object
  this.center.x = this.x + this.w / 2;
  this.center.y = this.y + this.h / 2;

  // Find angle from the X axis that the mouse is
  // from center of myGamePiece. Add 90deg so it is from the Y axis
  this.angle = Math.atan2(mouse.y - this.center.y, mouse.x - this.center.x) + (Math.PI/2);

  // set rotation center to player center
  ctx.translate(this.center.x, this.center.y);
  ctx.rotate(this.angle);
  // set rotation center back
  ctx.translate(-this.center.x, -this.center.y);
  ctx.fillRect(this.x, this.y, this.width, this.height); //render
  ctx.setTransform(1, 0, 0, 1, 0, 0);   // reset canvas transforms
}

You also need to add an EventListener to track where the mouse is. This will work with the code above.

document.body.addEventListener("mousemove", function (e) {
  mouse.x = e.pageX;
  mouse.y = e.pageY;
}, false);

Since many people have already explained each of these concepts probably better than I can, I'll just post them below.

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .