我正在创建一个生存僵尸游戏,当僵尸靠近玩家时,玩家可以攻击僵尸。如何创建玩家的视线,如果僵尸处于玩家的特定半径和角度(基本上靠近玩家),则玩家可以进行攻击。我设法将面向前方的玩家计算出来但是如何从玩家面对的方式创建角度如+ - 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');
}
}
}
}
}
答案 0 :(得分:1)
在psuedo代码中,这是你可以这样做的一种方式:
// 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
}
}