the problem is I have to pressed the keys down for I guess 2 seconds, like slash.
it must just a single press the action come out .
I already declared keysUp and Down.
whats wrong with my codes ?
package
{
import flash.display.MovieClip
import flash.events.Event
import flash.events.KeyboardEvent
public class Sample extends MovieClip
{
//game casts
public var _startMarker:StartMarker;
public var _hero:hero;
public var _boundaries:Boundaries;
public var _enemy:Enemy;
public var _mountain:Mountain;
public var slash:MovieClip;
//gravity & speed
private var gravityx:Number;
private var gravityy:Number;
private var mainSpeed:Number = 5;
//movement
private var leftKey :Boolean = false;
private var rightKey :Boolean = false;
private var aKey :Boolean = false;
private var sKey :Boolean = false;
public function Sample():void
{
// assign default values
_hero.gotoAndStop('idle');
backg.x = 50;
gravityx = 0;
gravityy = 0;
_startMarker.visible = false;
// set focus for keyboard input
stage.focus = stage;
_hero.scaleX = -1;
// add event listeners
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
private function enterFrameHandler(e:Event):void
{
// gravitate the player
gravityy += 2;
// move the player
_hero.x += gravityx;
_hero.y += gravityy;
// process collisions
processCollisions();
// scroll the stage
scrollStage();
//enemy hits
}
private function keyDownHandler(e:KeyboardEvent):void
{
if(e.keyCode == 37)
{
leftKey = true;
gravityx = -mainSpeed;
_hero.scaleX = 1;
_hero.gotoAndStop('walking');
}
else if(e.keyCode == 39)
{
rightKey = true;
gravityx = mainSpeed;
_hero.scaleX = -1;
_hero.gotoAndStop('walking');
}
else if(e.keyCode == 65)
{
leftKey = false;
rightKey = false;
aKey = true;
_hero.gotoAndStop('slash');
}
}
private function keyUpHandler(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 37:
leftKey = false;
_hero.gotoAndStop('idle');
case 39:
rightKey = false;
_hero.gotoAndStop('idle');
case 65:
aKey = false;
_hero.gotoAndStop('idle');
gravityx = 0;
break;
default:
}
}
private function processCollisions():void
{
// when player is falling
if (gravityy > 0)
{
// respawn if player fell off the stage
if (_hero.y > stage.stageHeight)
{
_hero.x =_startMarker.x;
_hero.y =_startMarker.y;
_boundaries.x = 0;
_boundaries.y = 0;
_mountain.x = 0;
backg.x = 0;
backg.y = 0;
gravityy = 0;
}
// otherwise, process collisions with boundaries
else
{
var collision:Boolean = false;
if (_boundaries.hitTestPoint(_hero.x, _hero.y, true))
{
collision = true;
}
if (collision)
{
while (collision)
{
_hero.y -=0.1;
collision = false;
if (_boundaries.hitTestPoint(_hero.x, _hero.y, true))
{
collision = true;
}
if (_hero.hitTestObject(_mountain))
{
trace("cant go through");
_hero.gotoAndStop('walking');
_hero.x+=5;
}
if (_hero.hitTestObject(_enemy))
{
trace("they hit");
_enemy.visible = false;
}
}
gravityy = 0;
}
}
}
}
private function scrollStage():void
{
_mountain.x += (stage.stageWidth * 0.5) - _hero.x;
_enemy.x += (stage.stageWidth * 0.5) - _hero.x;
backg.x += (stage.stageWidth * 0.5) - _hero.x;
_boundaries.x += (stage.stageWidth * 0.5) - _hero.x;
_hero.x = stage.stageWidth * 0.5;
}
private function heroAttack():void
{
}
}
}
thanks in advance !
and have a good day
-cheers