如何直接而不是一个角度移动

时间:2017-04-25 01:05:43

标签: javascript html5 canvas

我正在开发一个有敌人和汽车的游戏。所以现在我的汽车以对角线形状向敌人移动并击中它们并获得积分。现在我想让英雄以直接的方式移动(像蛇游戏没有对角线运动)然后击中敌人并获得积分。

所以我目前打击敌人的方式就像

function angleBetweenTwoPoints(p1, p2) {
    return Math.atan2(p2.y - p1.y, p2.x - p1.x) * 180 / Math.PI;
  }
function degreeToRadian(degrees) {
    return degrees * (Math.PI / 180);
  }
var angle = angleBetweenTwoPoints(this.target.position, this.position);
      var cos = Math.cos(degreeToRadian(angle)) * -1;
      var sin = Math.sin(degreeToRadian(angle));
      this.angle = angle;
      this.position.x += cos * this.speed;
      this.position.y -= sin * this.speed;
      if (distance(this.position, this.target.position) < 10) {
        this.target.position.x = Math.random() * mainCanvas.width;
        this.target.position.y = Math.random() * mainCanvas.height;
        this.hitCount++;
        console.log("Hit!");
        ctx.fillText("points : " + hitCount, 32, 32);


      }

所以现在我想让汽车不是对角地直线移动。

这是我的工作game。感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

使用Pixi:http://www.pixijs.com/ 和GSAP:https://greensock.com/

你会很容易找到所做的一切;)