好的,所以我的主要问题是flash的KeyboardEvent
速度慢。让我们说我按左箭头。由于某种原因,运动延迟然后移动。当我进行双跳或跳跃时,我不得不按住按钮而不是按一下。我的问题是闪存有没有办法检测到任何键的单击,如何解决keyCode的移动延迟?
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
//Initialized variables
var theKey:KeyObject = new KeyObject(stage);//Help the stage checks for keypressed objects
var hspeed:Number = 0;// horizontal speed
var vspeed:Number = 0;// vertical speed
var gravity:Number = 2;//Gravity
var friction:Number = .5;//Friction
var ground:int = 800;//Bottom of the stage
var JumpMax:Number = 0;//Double jump
//All Booleans (Mainly player states)
var aDown:Boolean = false;//Press left key
var dDown:Boolean = false;//Press right key
var Jumped:Boolean = false;//Player in jumping state
var AttackMode:Boolean = false;//Player in attacking state
var CrouchMode:Boolean = false;//Player in crouched state
var RightSide:Boolean = false;//Player hitting right side of block
var LeftSide:Boolean = false;//Player hitting left side of block
var DownSide:Boolean = false;//Player hitting the top of block
var UpSide:Boolean = false;//Player hits bottom of block
ultraDaimyo.gotoAndStop("idleDaimyo");
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyYeet);
function keyYeet(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.D)
{
dDown = true;
}
if(e.keyCode == Keyboard.A)
{
aDown = true;
}
if(e.keyCode == Keyboard.C)
{
AttackMode = true;
}
if(e.keyCode == Keyboard.W || Jumped == true)
{
Jumped = true;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, keyNot);
function keyNot(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.D)
{
dDown = false;
}
if(e.keyCode == Keyboard.A)
{
aDown = false;
}
if(e.keyCode == Keyboard.C)
{
AttackMode = false;
}
if(e.keyCode == Keyboard.W)
{
Jumped = false;
}
}
stage.addEventListener(Event.ENTER_FRAME, gameCycle);
function gameCycle(e:Event):void
{
hspeed *= friction;
ultraDaimyo.x += hspeed;
ultraDaimyo.y += gravity;
if(AttackMode)
{
ultraDaimyo.gotoAndStop("attackDaimyo");
ultraDaimyo.x += 5;
if(ultraDaimyo.scaleX == -1)
{
ultraDaimyo.gotoAndStop("attackDaimyo");
ultraDaimyo.x -= 10;
}
}
if(Jumped == true)
{
ultraDaimyo.y -= 30;
ultraDaimyo.gotoAndStop("jumpDaimyo");
}
if(dDown)
{
hspeed += 20;
ultraDaimyo.gotoAndStop("runDaimyo");
ultraDaimyo.scaleX = 1;
}
if(aDown)
{
hspeed -= 20;
ultraDaimyo.gotoAndStop("runDaimyo");
ultraDaimyo.scaleX = -1;
}
if(!aDown && !dDown && !AttackMode && !Jumped)
{
ultraDaimyo.gotoAndStop("idleDaimyo");
}
if(ultraDaimyo.x - ultraDaimyo.width/2 < 0)//If the player goes past the left side
{
ultraDaimyo.x = ultraDaimyo.width/2;
}
if(ultraDaimyo.x + ultraDaimyo.width/2 > 1400)//If the player goes past right
{
ultraDaimyo.x = 1400 - ultraDaimyo.width/2;//Player cant go past
}
if(ultraDaimyo.y + ultraDaimyo.height/2 < floor && Jumped)//If we are above the floor
{
if(Jumped)
{
ultraDaimyo.gotoAndStop("jumpDaimyo");
}
gravity++;//Accelerate gravity in the process
}
if(ultraDaimyo.y + ultraDaimyo.height/2 > floor)
{
Jumped = false;//If we are on the floor then we're not jumping
gravity = 0;//Gravity can no longer be applied
ultraDaimyo.y = floor - ultraDaimyo.height/2;//Player sits on top of the floor
}
}
答案 0 :(得分:1)
KeyboardEvent
不会连续发射,因为这样会很难检测出单击键。
您需要做的是使用KeyboardEvent
仅更改速度变量,但将速度应用于其他连续Event
中的位置变量,即Event.ENTER_FRAME
。
我根据您的代码创建了一个示例:
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
//Initialized variables
var speedX:Number = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyYeet);
function keyYeet(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.D)
{
speedX = 20;
}
if(e.keyCode == Keyboard.A)
{
speedX = -20;
}
}
stage.addEventListener(KeyboardEvent.KEY_UP, keyNot);
function keyNot(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.D || e.keyCode == Keyboard.A)
{
speedX = 0;
}
}
addEventListener(Event.ENTER_FRAME, gameCycle);
function gameCycle(e:Event):void
{
ultraDaimyo.x += speedX;
}
这个想法是设置当键降低到某个值时的速度,并在键重新启动时将其设置回0。
速度持续施加到该位置。
来自评论:
请仔细检查你的第一句话。
果然有一个拼写错误,但内容是我想要的。
结果是连续射击=连续运动。
不适合我。对于不同的(键盘)硬件或软件(操作系统,闪存运行时),这可能是不同的。其他人也有类似的经历,可以在手头问题的这些副本中看到。
让我们不要看看四处移动的显示对象,并比较关于它们是否连续移动的意见。让我们收集一些可比数据。
这是一个小文档类,用于计算KEY_DOWN
事件发生5次之间的延迟。
要使用它,请确保应用程序具有焦点并按下任何按钮,直到轨迹出现在输出面板中。
释放该键可重置阵列,并可按住另一个键。
package
{
import flash.utils.getTimer;
import flash.events.KeyboardEvent;
import flash.display.Sprite;
public class Main extends Sprite
{
private var oldTime:Number = 0;
private var deltaTimes:Array = [];
public function Main()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onKeyDown(e:KeyboardEvent):void
{
var currentTime:Number = getTimer();
// collect 5 time differences between two consecutive occurrences of this event
if (deltaTimes.length < 5)
{
deltaTimes.push(currentTime - oldTime)
}
else
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
trace(deltaTimes);
}
oldTime = currentTime;
}
private function onKeyUp(e:KeyboardEvent = null):void
{
deltaTimes = [];
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
}
}
以下是我的结果:
7584,499,49,51,50
2117,500,50,50,50
1786,500,50,50,49
1375,499,48,50,52
1395,500,50,50,50
1385,500,50,50,50
1243,501,47,52,49
2167,501,49,50,50
1587,500,50,50,50
3871,500,50,48,53
2237,500,49,51,49
1715,500,47,53,50
可以观察到三个重要的事情:
我在上面的回答中提供的代码解决了这个问题。