本文介绍了ActionScript 3的跳跃动画问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
import KeyObject;
import flash.events.Event;
var hsp = 15;
var vy:Number = 0;//Vertical speed variable
var grav:Number = 20;//gravity variable
var jumped:Boolean = false;//Checking if we jumped, false means
//not jumping
warMage.gotoAndStop("idleWarmage");
//Player initially starts with idle animation
var Key:KeyObject = new KeyObject(stage);//Adds the new method for keyboard check
//The stage always checks for these events and functions
stage.addEventListener(Event.ENTER_FRAME, onEnter);
stage.addEventListener(Event.ENTER_FRAME, gameloop);
function onEnter(e:Event):void
{
if(Key.isDown(Key.RIGHT))//Check if right arrow is pressed
{
warMage.x += 15;//Move at this speed
warMage.gotoAndStop("RunWarmage");//Play this animation
warMage.scaleX = 1;//Keep the image svale to right
}
else if(Key.isDown(Key.LEFT))//Check if left arrow is pressed
{
warMage.x -= 15;//Move left
warMage.scaleX = -1;//Flip the image
warMage.gotoAndStop("RunWarmage");//Play this animation
}
else if(Key.isDown(Key.UP))//Check if spacebar is pressed
{
if(!jumped)//the boolean is true
{
vy -= 70;//Player jumps 50 pixels upward
jumped = true;
warMage.gotoAndStop("JumpWarmage");//Play the jump animation
}
}
else
{
warMage.gotoAndStop("idleWarmage");//Return to idle state
}
}
function gameloop(e:Event):void
{
if(warMage.x + 36.55 < 0)//Setting room boundaries
{
warMage.x = 0;//Setting boundary on right
}
if(warMage.x + 55.22 > 999)//Setting boundary on left
{
warMage.x = 999;
}
vy += grav;
if(!ground.hitTestPoint(warMage.x + 36.55, warMage.y + 55.22, true))//If we're not on a surface
{
warMage.y += vy;//apply gravity to the player
}
for(var i = 0;i < 109; i++)
{ //If the warmage is on the ground
if(ground.hitTestPoint(warMage.x + 36.55, warMage.y + 55.22, true))
{
warMage.y--;//A pixel above the platform
vy = 0;//Gravity isn't applied on the player
jumped = false;//We're not jumping
}
}
}
解决方案
You said this was AS3, but Key.isDown()
is a deprecated AS2 class.function
In any case, you've declared that onEnter
would run on every frame (just as your gameloop
does), therefore unless you are holding down the directional arrows, or are currently releasing the spacebar, then it will predictably run else { warMage.gotoAndStop("idleWarmage"); }
.
- I've replaced your AS2 methods with AS3 Keyboard events (meaning the logic only fires when you're actually interacting with the keyboard).
- Your
jumped
boolean is confusing as appears to refer to what was rather than what is; even your comments clarified it in the present tense of "jumping", so I've done the same. - The true goal of your conditionals is to see whether your character should be idle or not. This is determined by whether they're currently not moving or jumping. Since we can articulate this during each keypress, the
idle
boolean can be set concisely. - Finally, with only one
enterFrame
function firing, moving the character is now handled the same way your gravity was, withwarMage.x += vx;
See the revised code below. Hope it helps. =)
import flash.events.Event;
var hsp = 15; // the horizontal speed of the character
var vx:Number = 0; // Horizontal speed variable
var vy:Number = 0; // Vertical speed variable
var grav:Number = 20; //gravity variable
var jumping:Boolean = false; // False means we're not jumping.
var idle:Boolean = false; // Whether or not the character is idle
warMage.gotoAndStop("idleWarmage"); //Player initially starts with idle animation
//The stage always checks for these events and functions
stage.addEventListener(KeyboardEvent.KEY_UP, keyEvents);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyEvents);
stage.addEventListener(Event.ENTER_FRAME, gameloop);
function keyEvents(e:KeyboardEvent) {
if (e.type == KeyboardEvent.KEY_DOWN) {
switch (e.keyCode) {
case 39: // Right Arrow
vx = hsp; //Move at this speed
warMage.gotoAndStop("RunWarmage"); //Play this animation
warMage.scaleX = 1; //Keep the image svale to right
idle = false;
break;
case 37: // Left Arrow
vx = -hsp; //Move left
warMage.scaleX = -1; //Flip the image
warMage.gotoAndStop("RunWarmage"); //Play this animation
idle = false;
break;
default:
if (!jumping) {
idle = true;
}
}
}
if (e.type == KeyboardEvent.KEY_UP) {
switch (e.keyCode) {
case 39: // Right Arrow
case 37: // Left Arrow
vx = 0;
if (!jumping) {
idle = true;
}
break;
case 32: // Spacebar
case 38: // Up Arrow
vy -= 70; //Player jumps 50 pixels upward
jumping = true;
idle = false;
warMage.gotoAndStop("JumpWarmage"); //Play the jump animation
break;
}
}
if (idle) {
warMage.gotoAndStop("idleWarmage"); //Return to idle state
}
}
function gameloop(e:Event):void {
warMage.x += vx;
if (warMage.x + 36.55 < 0) { //Setting room boundaries
warMage.x = 0; //Setting boundary on right
}
if (warMage.x + 55.22 > 999) { //Setting boundary on left
warMage.x = 999;
}
vy += grav;
if (!ground.hitTestPoint(warMage.x + 36.55, warMage.y + 55.22, true)) { //If we're not on a surface
warMage.y += vy; //apply gravity to the player
}
for (var i = 0; i < 109; i++) { //If the warmage is on the ground
if (ground.hitTestPoint(warMage.x + 36.55, warMage.y + 55.22, true)) {
warMage.y--; //A pixel above the platform
vy = 0; //Gravity isn't applied on the player
jumping = false; //We're not jumping
}
}
}
这篇关于ActionScript 3的跳跃动画问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!