【问题标题】:How to make enemies face player如何让敌人面对玩家
【发布时间】:2016-05-11 14:06:54
【问题描述】:

我的游戏是一种简单的 2D 自上而下的设计风格。我已经有敌人跟随玩家,但我不知道如何让敌人面对他。我什至不知道从哪里开始。

这是我让敌人跟随玩家的代码:

 if ( enemy.x > player.x ){
      enemy.x -= 7;
 }else {
      enemy.x += 7;
 {
 if ( enemy.y > player.y ){
      enemy.y -= 7;
 }else {
      enemy.y += 7;
 {

【问题讨论】:

  • 你想让敌人准确地面对玩家(360 度)还是只面对敌人移动的 8 个方向?
  • 你明白了吗?

标签: flash-cs6 flash-cc actionscript-3


【解决方案1】:

我假设你有一个变量来确定敌人的方向。另外,假设您只是使用四个 cardinal 方向。

您可以将敌人的 x,y 坐标与玩家的坐标进行简单比较。

sudo 代码:

if enemy x > player x // player is to the left of the enemy
    enemy face left
else if enemy x < player x // player is to the right of the enemy
    enemy face right
else if enemy y > player y // player is below the enemy
    enemy face down
else
    enemy face up

这将使你的敌人几乎总是面向左/右。通过比较敌人 x/y 与玩家 x/y 的增量,您可以在完成这项工作后获得更多参与。

类似:

delta x = abs(player x - enemy x)
delta y = abs(player y - enemy y)

if delta x > delta y // Enemy is further away on the x axis than the y axis
    make a choice of facing left/right
else
    make a choice of facing up/down

希望这可以帮助您入门。

【讨论】:

    【解决方案2】:

    如果你想让敌人完全面向玩家,也就是完整的 360 度,你可以计算enemyplayer 之间的角度并使用rotation 属性,如下所示:

    var angle:Number = Math.atan2(player.y - enemy.y, player.x - enemy.x) * (180 / Math.PI);
    enemy.rotation = angle;
    

    为了使它看起来正确,您应该将enemy 的注册设置在其中心,以便它围绕其中心旋转,并将其朝右绘制,使 0 角朝右。

    【讨论】:

    • 我用了这个方法。敌人确实面对玩家。但它并不干净,并不总是直接面对玩家 var angle:Number = Math.atan2(myCar.y - mc_enemyCar02.y, myCar.x - mc_enemyCar02.x) * (180 / Math.PI); mc_enemyCar02.rotation = 角度;
    • 不确定您所说的“干净”和“直接”是什么意思,但如果您的myCarmc_enemyCar 相同,它始终准确地面向玩家容器。否则,您将不得不将您的汽车坐标转换为敌方汽车坐标空间:var car:Point = enemyCarsContainer.globalToLocal(myCar.localToGlobal(new Point())。还要确保你的敌车被绘制为完全向右。数学是正确的,你的图形可能是错误的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多