我正在创建Flash游戏,我想制作类似的东西: 如果键“x”点击它启动attack1动画,如果键“x”双击它启动attack2动画。怎么做到的?我没有想法。
现在我有简单的代码,当点击“x”时它开始播放attack1动画:
stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
private function key_down(event:KeyboardEvent)
{
if (event.keyCode == 88)
{
if (attack > 0)
{
return;
}// don't interrupt other attack
for (c in codes)
{
if (codes[c] == event.keyCode)
{
attack = c;
Hero.gotoAndStop("attack" + attack);
}
}
}
答案 0 :(得分:1)
存储按键的时间(例如,使用getTimer()
),然后比较二次按下的时间与上一次的时间。如果差距足够小,那就是双击。
粗略的未经测试的代码可以提供您的想法:
var keysPressed:Object = {};
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyboard);
function keyboard(e:KeyboardEvent):void
{
if(keysPressed[e.keyCode] !== undefined)
{
// Compare time, maybe half a second (500ms).
if(getTimer() - 500 < keysPressed[e.keyCode])
{
// Double press.
//
}
}
keysPressed[e.keyCode] = getTimer();
}
答案 1 :(得分:0)
//function for calculating is click is double click or single click:
stage.addEventListener(MouseEvent.CLICK, checkDoubleClick);
var mClick:Boolean = false;
var timer;
var timer2;
//
function checkDoubleClick(e:*)
{
if (! mClick)
{
timer = getTimer() / 1000;
mClick = true;
}
else
{
timer2 = getTimer() / 1000;
}
//if it is a double click
if ((timer2-timer)<.20)
{
trace("double click");
}
else
{
timer = getTimer() / 1000;
trace("single click");
}
}