我正在制作一个节拍游戏,到目前为止,我已经让角色跑步和跳跃。我现在想让我的角色向左或向右冲刺。因此,如果玩家向右按下键,则非常快,然后角色会突然出现。我怎样才能实现这一目标。这是我到目前为止所做的。
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Player extends MovieClip
{
//Player run speed setting
var RunSpeed:Number = 8;
//Player key presses
var RightKeyPress:Boolean = false;
var LeftKeyPress:Boolean = false;
var UpKeyPress:Boolean = false;
//Jump variables
var Gravity:Number = 1.5;
var JumpPower:Number = 0;
var CanJump:Boolean = false;
var Jumped:Boolean = false;
public function Player()
{
// constructor code
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyPressed);
addEventListener(Event.ENTER_FRAME,Update);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyReleased);
}
function KeyPressed(event:KeyboardEvent)
{
//When Key is Down
if (event.keyCode == 39)
{
RightKeyPress = true;
}
if (event.keyCode == 37)
{
LeftKeyPress = true;
}
if (event.keyCode == 38)
{
UpKeyPress = true;
}
}
function Update(event:Event)
{
//Adding gravity to the game world
JumpPower += Gravity;
//if player is more than 300 on the y-axis
if (this.y > 300)
{
//Player stays on the ground and can jump
JumpPower = 0;
CanJump = true;
}
//If on floor
if (CanJump)
{
//If right key is pressed run right
if ((RightKeyPress))
{
x += RunSpeed;
gotoAndStop('Run');
scaleX = 1;
}
else if ((LeftKeyPress))
{
//otherwise if left key is pressed run left
x -= RunSpeed;
gotoAndStop('Run');
scaleX = -1;
}
if ((UpKeyPress))
{
//If up key is pressed then jump
JumpPower = -15;
CanJump = false;
gotoAndStop('Jump');
Jumped = true;
}
//If no key is pressed stay idle
if ((!RightKeyPress && !LeftKeyPress && CanJump))
{
gotoAndStop('Idle');
}
}
else if (CanJump == false)
{
//Other if in air and right key is pressed move right
if ((RightKeyPress))
{
x += RunSpeed;
scaleX = 1;
}
else if ((LeftKeyPress))
{
//Otherwise if left key is pressed then move left
x -= RunSpeed;
scaleX = -1;
}
}
//If already jumped and on floor
if (Jumped == true && CanJump)
{
//Cannot jump again
CanJump = false;
//If on floor and right key is pressed run right
if ((RightKeyPress))
{
gotoAndStop('Run');
scaleX = 1;
}
else if ((LeftKeyPress))
{
//Otherwise if on floor and left key is pressed run left
gotoAndStop('Run');
scaleX = -1;
}
//If no key is pressed stay idle
if ((!RightKeyPress && !LeftKeyPress))
{
gotoAndStop('Idle');
}
}
this.y += JumpPower;
}
function KeyReleased(event:KeyboardEvent)
{
if (event.keyCode == 39)
{
event.keyCode = 0;
RightKeyPress = false;
}
if (event.keyCode == 37)
{
event.keyCode = 0;
LeftKeyPress = false;
}
if (event.keyCode == 38)
{
event.keyCode = 0;
UpKeyPress = false;
Jumped = false;
}
}
}
}
答案 0 :(得分:1)
所以我做了一点测试并想出了这个。它现在似乎工作得很好。我没有以极长的时间测试它,但它可以让你开始。我将把它留给你实现你自己的代码。只需复制/粘贴到新的AS3文档中并观看控制台
即可import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressed);
var pressed :Boolean = false;
var lastKeyPressed :Number = -1;
var doubleTapDelay :Number = 260; //-- delay in milliseconds
function keyPressed(e:KeyboardEvent):void
{
if (lastKeyPressed == e.keyCode && pressed) trace("double tapped " + e.keyCode);
lastKeyPressed = e.keyCode;
pressed = true;
setTimeout(function(){pressed = false},doubleTapDelay);
}
编辑使用KEY_DOWN
这似乎与KEY_DOWN
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
var pressed :Boolean = false;
var lastKeyPressed :Number = -1;
var doubleTapDelay :Number = 260; //-- delay in milliseconds
function keyPressed(e:KeyboardEvent):void
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
if (lastKeyPressed == e.keyCode && pressed) trace("double tapped " + e.keyCode);
lastKeyPressed = e.keyCode;
pressed = true;
setTimeout(function(){pressed = false},doubleTapDelay);
}
function onKeyboardUp(e:KeyboardEvent):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
}
再次为演示编辑
DEMO:http://ronnieswietek.com/_random/dash_example.swf
消息来源:http://ronnieswietek.com/_random/dash_example.fla
import flash.events.KeyboardEvent;
import com.greensock.TweenLite;
import com.greensock.easing.*;
import com.greensock.plugins.*;
TweenPlugin.activate([BlurFilterPlugin]);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_DOWN, permaKeyDown);
var pressed :Boolean = false;
var lastKeyPressed :Number = -1;
var dashAmount :Number = 50;
var doubleTapDelay :Number = 260; //-- delay in milliseconds
function permaKeyDown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 38: //-- up arrow
char.y = char.y - 2;
break;
case 39: //-- right arrow
char.x = char.x + 2;
break;
case 40: //-- down arrow
char.y = char.y + 2;
break;
case 37: //-- left arrow
char.x = char.x - 2;
break;
}
}
function keyPressed(e:KeyboardEvent):void
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
if (lastKeyPressed == e.keyCode && pressed)
{
trace("double tapped " + e.keyCode);
doDash(e.keyCode);
}
lastKeyPressed = e.keyCode;
pressed = true;
setTimeout(function(){pressed = false}, doubleTapDelay);
}
function onKeyboardUp(e:KeyboardEvent):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
}
function doDash(keyCode:Number):void
{
switch (keyCode)
{
case 38: //-- up arrow
TweenLite.to(char,0,{blurFilter:{blurY:50}});
TweenLite.to(char,0.3,{blurFilter:{blurY:0},y:char.y - dashAmount,ease:Expo.easeOut});
break;
case 39: //-- right arrow
TweenLite.to(char,0,{blurFilter:{blurX:50}});
TweenLite.to(char,0.3,{blurFilter:{blurX:0},x:char.x + dashAmount,ease:Expo.easeOut});
break;
case 40: //-- down arrow
TweenLite.to(char,0,{blurFilter:{blurY:50}});
TweenLite.to(char,0.3,{blurFilter:{blurY:0},y:char.y + dashAmount,ease:Expo.easeOut});
break;
case 37: //-- left arrow
TweenLite.to(char,0,{blurFilter:{blurX:50}});
TweenLite.to(char,0.3,{blurFilter:{blurX:0},x:char.x - dashAmount,ease:Expo.easeOut});
break;
}
}