我正在创建一个游戏,当玩家拿着棒球棒时,当你点击按钮时你挥动你的球棒。当你挥动你的蝙蝠时,你击中了敌人,敌人会像你打高尔夫球一样向你的方向飞去。我已经完成了移动和攻击功能的工作,但我怎样才能记录击中,以便在面对它并且敌人返回时击中敌人。这就是我到目前为止所做的:
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
public class Player extends MovieClip
{
var walkSpeed:Number = 4;
var walkRight:Boolean = false;
var walkLeft:Boolean = false;
var walkUp:Boolean = false;
var walkDown:Boolean = false;
var attacking:Boolean = false;
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)
{
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 == true)
{
walkRight = false;
walkLeft = false;
walkUp = false;
walkDown = false;
}
else if (attacking == false)
{
var dx = parent.mouseX - x;
var dy = parent.mouseY - y;
var angle = Math.atan2(dy,dx) / Math.PI * 180;
rotation = angle;
if (walkRight == true)
{
x += walkSpeed;
gotoAndStop('walk');
}
if (walkUp == true)
{
y -= walkSpeed;
gotoAndStop('walk');
}
if (walkLeft == true)
{
x -= walkSpeed;
gotoAndStop('walk');
}
if (walkDown == true)
{
y += walkSpeed;
gotoAndStop('walk');
}
}
}
function stopWalk(event:KeyboardEvent)
{
if (attacking == false)
{
if (event.keyCode == 68)
{
event.keyCode = 0;
walkRight = false;
gotoAndStop('stance');
}
if (event.keyCode == 87)
{
event.keyCode = 0;
walkUp = false;
gotoAndStop('stance');
}
if (event.keyCode == 65)
{
event.keyCode = 0;
walkLeft = false;
gotoAndStop('stance');
}
if (event.keyCode == 83)
{
event.keyCode = 0;
walkDown = false;
gotoAndStop('stance');
}
}
}
function attack(event:MouseEvent)
{
if (attacking == false)
{
attacking = true;
gotoAndStop('attack');
}
}
}
}
答案 0 :(得分:0)
当角色的方向改变时,根据他们现在面对的方式改变变量,例如;当他们面朝下时方向= 0,当他们面向右方时为1等等。然后使用这个和敌人的位置来判断敌人是否被击中。为了让敌人飞回来,你会得到角色的方向来计算他们飞回的方向。我会给出一些示例代码来帮助解释,但我在平板电脑上。