My problem here is that when i press the spacebar, it plays the attack animation but as soon as i release it it stops the animation process. I want it where i can simply press and release the button and then it completes the animation by itself.
function keyPressed(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.RIGHT)
{
rDown = true;
}
if(e.keyCode == Keyboard.LEFT)
{
lDown = true;
}
if(e.keyCode == Keyboard.UP && onGround)
{
jumped = true;
}
if(e.keyCode == Keyboard.DOWN && onGround)
{
crouchMode = true;
}
if(e.keyCode == Keyboard.SPACE && onGround)
{
attackMode = true;
}
}
function keyReleased(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.RIGHT)
{
rDown = false;
}
if(e.keyCode == Keyboard.LEFT)
{
lDown = false;
}
if(e.keyCode == Keyboard.UP)
{
jumped = true;
}
if(e.keyCode == Keyboard.DOWN)
{
crouchMode = false;
}
if(e.keyCode == Keyboard.SPACE)
{
attackMode = false;
}
}
function gameLoop(e:Event):void
{
playerStart();
}
}
}
function playerStart():void
{
warMage.y += grav;//Apply gravity to player
collisionStageCheck();
if(crouchMode)
{
warMage.gotoAndStop("CrouchWarmage");
rDown = false;
lDown = false;
}
if(attackMode)
{
if(warMage.scaleX == 1)//If we face right
{
warMage.x += 5;//Lunge right
}
if(warMage.scaleX == -1)//If we face left
{
warMage.x -= 5;//Lunge left
}
warMage.gotoAndPlay("AttackWarmage");
}
if(rDown)//If we move right
{
warMage.x += 12;
warMage.gotoAndStop("RunWarmage");
warMage.scaleX = 1;
}
if(lDown)//If we move left
{
warMage.x -= 12;
warMage.gotoAndStop("RunWarmage");
warMage.scaleX = -1;
}
if(jumped)//If we jumped
{
warMage.y -= 30;
warMage.gotoAndStop("JumpWarmage");
}
if(!rDown && !lDown && !jumped && !crouchMode && !attackMode)//If we are in neither states
{
warMage.gotoAndStop("idleWarmage");
}
}
The attack animation is not completing because attackMode
is being set to false
on the spacebar keyReleased
function. Don't do that. Instead, make attackMode
false on the last frame of the animation. Do this in your Main class nested inside your if (attackMode) {
logic block:
if (heroMage.currentFrame == "LastFrame")
{
attackMode = false;
}
Obviously you'd name the last frame of the attack animation "LastFrame".
I also found a more elegant approach here , using eventDispatcher
which eliminates the need to check the current frame, but adds a slight nuisance (in my opinion) of having code inside a movie clip timeline.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.