我正在创建一个生存僵尸游戏,当僵尸靠近玩家时,玩家可以攻击僵尸。如何创建玩家的视线,如果僵尸在玩家的特定半径和角度(基本上靠近玩家),那么玩家可以攻击。我设法计算出面向前方的玩家,但是我如何从玩家面向的方式以及僵尸是否在玩家的半径和角度内创建像 +- 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');
}
}
}
}
}