0

我正在创建一个生存僵尸游戏,当僵尸靠近玩家时,玩家可以攻击僵尸。如何创建玩家的视线,如果僵尸在玩家的特定半径和角度(基本上靠近玩家),那么玩家可以攻击。我设法计算出面向前方的玩家,但是我如何从玩家面向的方式以及僵尸是否在玩家的半径和角度内创建像 +- 15 这样的角度。到目前为止,这就是我所做的

package 
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
import flash.display.Graphics;

public class Player extends MovieClip
{
//Player Setting
var walkSpeed:Number = 4;
var walkRight:Boolean = false;
var walkLeft:Boolean = false;
var walkUp:Boolean = false;
var walkDown:Boolean = false;
var attacking:Boolean = false;
var radius:Number = 60;

public function Player()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,walk);
addEventListener(Event.ENTER_FRAME,Update);
stage.addEventListener(KeyboardEvent.KEY_UP,stopWalk);
stage.addEventListener(MouseEvent.CLICK,attack);
}

function walk(event:KeyboardEvent)
{
//When Key is Down
if (event.keyCode == 68)
{
walkRight = true;
}
if (event.keyCode == 87)
{
walkUp = true;
}
if (event.keyCode == 65)
{
walkLeft = true;
}
if (event.keyCode == 83)
{
walkDown = true;
}
}

function Update(event:Event)
{

//if attacking is true then key moves are false;
if ((attacking == true))
{
walkRight = false;
walkLeft = false;
walkUp = false;
walkDown = false;
}
else if ((attacking == false))
{
//Else if attacking is false then move and rotate to mouse;
var dx = parent.mouseX - x;
var dy = parent.mouseY - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
rotation = angle;

                               var radiusx:Number = x+radius*Math.cos(rotation/180*Math.PI);
       var radiusy:Number = y+radius*Math.sin(rotation/180*Math.PI);

if ((walkRight == true))
{
x +=  walkSpeed;
gotoAndStop(2);
}
if ((walkUp == true))
{
y -=  walkSpeed;
gotoAndStop(2);
}
if ((walkLeft == true))
{
x -=  walkSpeed;
gotoAndStop(2);
}
if ((walkDown == true))
{
y +=  walkSpeed;
gotoAndStop(2);
}
}
}

//Calculate the distance between the two
public function distanceBetween(player:Object,zombies:Object):Number
{
var dx:Number = player.x - zombies.x;
var dy:Number = player.y - zombies.y;
return Math.sqrt(((dx * dx) + dy * dy));
}

function stopWalk(event:KeyboardEvent)
{
if ((attacking == false))
{
if (event.keyCode == 68)
{
event.keyCode = 0;
walkRight = false;
gotoAndStop(1);
}
if (event.keyCode == 87)
{
event.keyCode = 0;
walkUp = false;
gotoAndStop(1);
}
if (event.keyCode == 65)
{
event.keyCode = 0;
walkLeft = false;
gotoAndStop(1);
}
if (event.keyCode == 83)
{
event.keyCode = 0;
walkDown = false;
gotoAndStop(1);
}
}
}

function attack(event:MouseEvent)
{
if ((attacking == false))
{
attacking = true;
gotoAndStop(3);
if (hitTestObject(MovieClip(root).Zo))
{
trace('ouch');

}
}
}
}
}
4

1 回答 1

1

在伪代码中,这是您可以执行此操作的一种方法:

// check distance between player and zombie
  // if zombie is close enough 
       //get angle between player and zombie
       //check difference between player facing and angle
       //if difference between angle <= 15 degrees 
          //attack zombie

这是一个函数,它将返回从玩家到给定目标的角度:

// I used the code from your code above to create this function
function getAngle(targetX:Number, targetY:Number):Number
{
     var dx = targetX - player.x;
     var dy = targetY - player.y;
     var angle = Math.atan2(dy,dx) / Math.PI * 180;

     return angle;
}

这是您的代码中的距离函数,我对其进行了修改以接受影片剪辑:

public function distanceBetween(player:MovieClip,zombie:MovieClip):Number
{
    var dx:Number = player.x - zombie.x;
    var dy:Number = player.y - zombie.y;
    return Math.sqrt(((dx * dx) + dy * dy));
}

所以你可以使用上面的伪代码来创建代码来解决你的问题:

// check distance between player and zombie
if (distanceBetween(player,zombie) <= maxRadius)
{
    // get angle between player and zombie
    var angleToZombie:Number = getAngle(zombie.x, zombie.y);

    // I am assuming that the facing is going to be towards the mouse
    // calculate facing of player
    var currentFacing:Number = getAngle(mouseX, mouseY);

    //check difference between player facing and angle
    if (Math.abs(currentFacing - angleToZombie) <= 15)
    {
         // attack the zombie
    }
}
于 2013-04-19T20:23:12.347 回答