代码用于冲刺角色。如果用户在第一次点击后在x刻度内按下右箭头,它将使对象移动得更快。
用户只有时间再次按下。当时间用完时,它应该重置。
通过按右箭头键,计时器开始计时,即使没有按下按键也会保持滴答声(如果用户在此时间内再次按下,对象将移动得更快) - 当它达到0时,如果没有按下,则应该将布尔变量重置为false,但它不会这样做。
在onKeyUp函数中,if if应为true
所以,点击一下然后离开它:
function onKeyUp ... //no key being pressed
//leave it until timer reaches 0.
if (timer <= 0) {
TimerCounter = false;
condition1 = false;
condition2 = false;
timer = 5;
复制并粘贴下面的代码,运行它并查看跟踪。如果您点击右箭头并释放,即使计时器变为0,也会说
定时器:0; 条件1:真实; 条件2:正确;
什么时候不应该。
package
{
(...)
public class Main extends Sprite
{
var player:Sprite = new Sprite();
var keys:Array = [];
var sprint:Boolean = false;
var condition1:Boolean = false;
var condition2:Boolean = false;
var TimerCounter:Boolean = false;
var timer:int = 7;
public function Main():void
{
player.graphics.beginFill(0x000000);
player.graphics.drawCircle(0, 0, 25);
player.graphics.endFill;
addChild(player);
player.x = 100;
player.y = 100;
player.addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
function getBack (e:Event) {
player.x = 100;
}
function update(e:Event) {
trace("Timer: ",timer);
trace("Condition 1: ", condition1);
trace("Condition 2: ", condition2);
if ((TimerCounter)&&(timer > 0)) {
timer --;
condition1 = true;
}
if (keys[Keyboard.RIGHT]) {
TimerCounter = true;
if ((condition1)&&(condition2)) {
sprint = true;
}
if (sprint) {
player.x += 7;
} else player.x += 1;
}
function onKeyDown(e:KeyboardEvent):void {
keys[e.keyCode] = true;
}
function onKeyUp(e:KeyboardEvent):void {
keys[e.keyCode] = false;
if ((condition1)&&(timer>0)) {
condition2 = true;
}
if (sprint) {
TimerCounter = false;
condition1 = false;
condition2 = false;
sprint = false;
timer = 7;
}
if (timer <= 0) {
TimerCounter = false;
condition1 = false;
condition2 = false;
timer = 7;
}
} //onKeyUp function end.
} // Class end.
} // package end.
答案 0 :(得分:1)
您的问题是,onKeyUp
并不表示未按下该键。当钥匙被释放时,这只会被解雇一次,而不是像你的问题所暗示的那样经常被解雇。您需要签入其他功能。
function onKeyUp ... //key is released
//leave it until timer reaches 0.
if (timer <= 0) {
TimerCounter = false;
condition1 = false;
condition2 = false;
timer = 5;
选项:
update
函数中检查它,然后向onKeyUp
/ onKeyDown
添加一个额外的布尔值以查看是否实际按下了该键(告知是否增加计时器)。keys[e.keyCode]
事件检查密钥/哪个密钥是否已关闭。这里的关键是你不能在onKeyUp
事件中保持定时器检查,因为只要每个键上/下,它只被调用一次,不是,只要按键启动。< / LI>