【问题标题】:How to move the by straight rather than in an angle如何通过直线而不是角度移动
【发布时间】:2017-04-25 01:05:42
【问题描述】:

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

所以我现在打击敌人的方式是这样的

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。感谢任何帮助。

【问题讨论】:

  • 你不能危害密码。
  • 不,我要求的不是代码,我想要移动汽车的想法。我正在研究星形路径查找算法并了解如何集成它
  • 你必须让每辆车与众不同,然后应用一个单独的行为,当它击中目标时,随着惯性变化时逐步增加角度,我认为你会有好的......或者在死之前使用 pixi.js 和 tweenlite (GSAP) xD

标签: javascript html canvas


【解决方案1】:

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

你会很容易找到一切可以做得更好的东西;)

【讨论】:

  • 我不需要寻路算法吗?
  • Gsap 可以自己使用,现在就开始学习吧,简直太简单了(像 jquery animate 但真的更好)
猜你喜欢
  • 2021-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多