ActionScript 3的跳跃动画问题 [英] Actionscript 3 jumping animation issue

查看:192
本文介绍了ActionScript 3的跳跃动画问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

有这个问题,林在那里,当我preSS上键为我的球员跳,玩家去跳动画,但立即返回到空闲的动画,而在半空中。我的问题是我如何能得到玩家按住跳跃的动画,然后返回到怠速,一旦他到达地面。


 进口KeyObject;
进口对象类型:flash.events.Event;VAR HSP = 15;
VAR VY:数= 0; //垂直变速
VAR GRAV:数= 20; //重力变
VAR跳了起来:布尔= FALSE; //检查是否我们跳下,虚假手段
//不跳warMage.gotoAndStop(idleWarmage);
//播放器最初启动闲置动画VAR键:KeyObject =新KeyObject(阶段); //添加新方法键盘检查//舞台总是检查这些活动和功能
stage.addEventListener(Event.ENTER_FRAME,的OnEnter);
stage.addEventListener(Event.ENTER_FRAME,gameloop);功能的OnEnter(五:事件):无效
{    如果(Key.isDown(Key.RIGHT))//检查右箭头是pressed
    {
        warMage.x + = 15; //以这样的速度移动
        warMage.gotoAndStop(RunWarmage); //播放这部动画
        warMage.scaleX = 1; //保持图像svale向右
    }
    否则,如果(Key.isDown(Key.LEFT))//检查左箭头是pressed
    {
        warMage.x - = 15; //左移
        warMage.scaleX = -1; //翻转图像
        warMage.gotoAndStop(RunWarmage); //播放这部动画
    }
    否则,如果(Key.isDown(Key.UP))//检查空格键为pressed
    {
        如果(!跳下)//布尔是真实的
        {
            VY - = 70; //播放器跳到50像素向上
            跳下= TRUE;
            warMage.gotoAndStop(JumpWarmage); //播放动画跳
        }
    }
    其他
    {
        warMage.gotoAndStop(idleWarmage); //返回到空闲状态
    }
}
功能gameloop(五:事件):无效
{
    如果(warMage.x + 36.55℃的)//设置房间边界
    {
        warMage.x = 0; //在右边设置的边界
    }
    如果(warMage.x + 55.22> 999)//设置边界上留下
    {
        warMage.x = 999;
    }
    VY + = GRAV;
    如果(!ground.hitTestPoint(warMage.x + 36.55,+ warMage.y 55.22,TRUE))//如果我们不是在表面上
    {
        warMage.y + = VY; //应用重力给玩家
    }
    对于(VAR I = 0; I< 109;我++)
    {//如果warmage是在地面上
        如果(ground.hitTestPoint(warMage.x + 36.55,+ warMage.y 55.22,真))
        {
            warMage.y - ; //该平台上方的像素
            VY = 0; //重力不施加在播放机上
            跳下= FALSE; //我们不跳
        }
    }
}


解决方案

您说这是AS3,但 Key.isDown()是一个去precated AS2 class.function

在任何情况下,你已经宣称的OnEnter 将每一帧上运行(就像你的 gameloop 做),因此,除非你按住方向箭头,或正在释放空格键,然后将predictably运行其他{warMage.gotoAndStop(idleWarmage); }


  1. 我已经更换了你的AS2的方法AS3键盘事件(即逻辑只有当你真正与键盘交互的火灾)。

  2. 跃升布尔是混乱的似乎是指什么样的而不是什么;即使您的意见澄清它在present紧张跳楼的,所以我做了相同的。

  3. 您的条件语句的真正目的是看你的性格是否应该被闲置或没有。这是无论他们目前还没有移动或跳跃确定。既然我们可以在每个关键preSS中阐明这一点,空闲布尔可设置简洁

  4. 最后,只有一个的enterFrame 函数射击,移动字符现在处理方式的重力是一样的,用 warMage.x + = VX;

请参阅修订code以下。希望它帮助。 =)

 进口对象类型:flash.events.Event;VAR HSP = 15; //字符的水平速度
VAR VX:数= 0; //卧式变速
VAR VY:数= 0; //垂直变速
VAR GRAV:数= 20; //重力变
VAR跳跃:布尔= FALSE; //造假手段我们不跳。
VAR空闲:布尔= FALSE; //是否字符是空闲warMage.gotoAndStop(idleWarmage); //播放器最初启动闲置动画//舞台总是检查这些活动和功能
stage.addEventListener(KeyboardEvent.KEY_UP,使用的KeyEvent);
stage.addEventListener(KeyboardEvent.KEY_DOWN,使用的KeyEvent);
stage.addEventListener(Event.ENTER_FRAME,gameloop);功能的KeyEvent(E:KeyboardEvent的){
    如果(e.type == KeyboardEvent.KEY_DOWN){
        开关(e.key code){
            案例39://右箭头
                VX = HSP; //将在这个速度
                warMage.gotoAndStop(RunWarmage); //播放这部动画
                warMage.scaleX = 1; //保持图像svale向右
                空闲= FALSE;
                打破;
            案例37://左箭头
                VX = -hsp; //向左移动
                warMage.scaleX = -1; //翻转图像
                warMage.gotoAndStop(RunWarmage); //播放这部动画
                空闲= FALSE;
                打破;
            默认:
                如果(!跳){
                    空闲= TRUE;
                }        }
    }    如果(e.type == KeyboardEvent.KEY_UP){
        开关(e.key code){
            案例39://右箭头
            案例37://左箭头
                VX = 0;
                如果(!跳){
                    空闲= TRUE;
                }
                打破;
            案例32://空格键
            案例38://向上箭头
                VY - = 70; //播放器跳到50像素向上
                跳跃=真;
                空闲= FALSE;
                warMage.gotoAndStop(JumpWarmage); //播放跳跃动画
                打破;
        }
    }    如果(空闲){
        warMage.gotoAndStop(idleWarmage); //返回到空闲状态
    }
}功能gameloop(五:事件):无效{
    warMage.x + = VX;    如果(warMage.x + 36.55℃的){//设置房间边界
        warMage.x = 0; //右边设置的边界
    }    如果(warMage.x + 55.22> 999){//左侧设置边界
        warMage.x = 999;
    }    VY + = GRAV;
    如果(!ground.hitTestPoint(warMage.x + 36.55,+ warMage.y 55.22,真)){//如果我们不是在表面上
        warMage.y + = VY; //应用重力给玩家
    }    为(变量I = 0; I&所述; 109;我++){//如果warmage是在地面上
        如果(ground.hitTestPoint(warMage.x + 36.55,+ warMage.y 55.22,真)){
            warMage.y--; //该平台上方的像素
            VY = 0; //重力不施加在播放机上
            跳跃= FALSE; //我们不跳
        }
    }
}

Im having this issue where when I press the up key for my player to jump, the player goes to the jump animation but immediately returns to the idle animation while in mid air. My question is how can i get the player to hold the jump animation and then return to idle once he reaches the ground.

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"); }.

  1. I've replaced your AS2 methods with AS3 Keyboard events (meaning the logic only fires when you're actually interacting with the keyboard).
  2. 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.
  3. 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.
  4. Finally, with only one enterFrame function firing, moving the character is now handled the same way your gravity was, with warMage.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的跳跃动画问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆