Actionscript 3跳动画问题

时间:2016-04-23 13:34:28

标签: actionscript-3 flash animation

  

我有这个问题,当我按下我的播放器的向上键跳转时,播放器进入跳跃动画,但在半空中立即返回到空闲动画。我的问题是如何让玩家保持跳跃动画,然后一旦他到达地面就回到空闲状态。

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
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你说这是AS3,但是Key.isDown()是一个弃用的AS2 class.function

在任何情况下,您都声明onEnter会在每一帧上运行(就像您的gameloop一样),因此除非您按住方向箭头,或者当前正在释放空格键,然后它可以预测运行else { warMage.gotoAndStop("idleWarmage"); }

  1. 我已经用AS3键盘事件替换了你的AS2方法(意味着逻辑只在你实际与键盘交互时触发)。
  2. 您的jumped布尔值令人困惑,因为它似乎指的是 而不是 ;即使你的评论也用现在的“跳跃”时态澄清了,所以我也做了同样的事情。
  3. 你的条件的真正目标是看你的角色是否应该闲着。这取决于他们目前是否正在移动或跳跃。由于我们可以在每个按键期间清楚地表达这一点,因此可以简洁地设置idle布尔值。
  4. 最后,只有一个enterFrame功能触发,移动角色现在的处理方式与引力相同,warMage.x += vx;
  5. 请参阅下面的修订代码。希望能帮助到你。 =)

    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
            }
        }
    }