【问题标题】:Actionscript 3 jumping animation issueActionscript 3 跳跃动画问题
【发布时间】:2016-08-17 03:06:15
【问题描述】:

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

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

【问题讨论】:

  • 根据您发布的代码,您一键启动,它将转到“idleWarmage”,
  • 是否有特定的命令可以说保持动画直到玩家到达地面?
  • 没有现成的命令。只有当 jumped 为 false 时,Warmag 才能进入空闲状态。当跳跃动画完成时,您应该将跳跃变量设为 false。

标签: actionscript-3 flash animation


【解决方案1】:

你说这是 AS3,但 Key.isDown() 是一个已弃用的 AS2 类。函数

无论如何,您已经声明onEnter 将在每一帧上运行(就像您的gameloop 一样),因此除非您按住方向箭头或当前正在释放空格键,否则它将可以预见地运行else { warMage.gotoAndStop("idleWarmage"); }

  1. 我已将您的 AS2 方法替换为 AS3 键盘事件(这意味着该逻辑仅在您实际与键盘交互时触发)。
  2. 您的jumped 布尔值令人困惑,因为它似乎指的是曾经而不是;甚至你们的 cmets 都用“跳跃”的现在时进行了澄清,所以我也这样做了。
  3. 条件句的真正目标是查看您的角色是否应该处于空闲状态。这取决于他们当前是否没有移动或跳跃。由于我们可以在每次按键时明确这一点,因此可以简洁地设置 idle 布尔值。
  4. 最后,只有一个enterFrame函数触发,移动角色现在的处理方式与你的重力相同,warMage.x += vx;

请参阅下面的修改后的代码。希望能帮助到你。 =)

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

【讨论】:

    猜你喜欢
    • 2017-10-21
    • 2013-09-15
    • 1970-01-01
    • 2013-10-15
    • 2013-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多