让我的角色战斗

时间:2013-08-14 18:43:40

标签: actionscript-3

我正在尝试创建一个节拍游戏,现在我让我的角色走路和跳跃。到目前为止我有3个攻击动画,并且工作正常。这就是我想要实现的,如果玩家按下攻击按钮然后角色会攻击,如果玩家一直按下攻击按钮,它将继续其攻击/打击序列,或者如果按下不同的攻击键,它将切换攻击/踢动画(注意:如果玩家按下键,我不想要,必须按键)。

一旦当前的攻击动画完全完成而不是跳到当前攻击动画中间的下一帧,我怎样才能进入下一个攻击动画。因此,当角色进行攻击并按下攻击键时,角色将完成其当前的攻击动画,一旦结束,它就会移动到下一个攻击动画帧,否则它将停止。如果我创建数组或扩展类,我不知道如何做到这一点。我想创建像这样的游戏我发现但它在AS2中的副本

https://www.dropbox.com/s/zhf68zmi0ktmeqq/DD%20%282%29.zip

这是我到目前为止所做的事情

package 
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.utils.*;
    import com.greensock.TweenLite;
    import com.greensock.easing.*;
    import com.greensock.plugins.*;

    public class Player extends MovieClip
    {
        TweenPlugin.activate([BlurFilterPlugin]);
        //Player run speed setting;
        var RunSpeed:Number = 8;
        //Player key presses
        var RightKeyPress:Boolean = false;
        var LeftKeyPress:Boolean = false;
        var UpKeyPress:Boolean = false;
        //Jump variables
        var Gravity:Number = 1.5;
        var JumpPower:Number = 0;
        var CanJump:Boolean = false;
        var Jumped:Boolean = false;
        //Dash variable
        var Pressed:Boolean = false;
        var LastKeyPressed:Number = -1;
        var DashAmount:Number = 250;
        var DoubleTapDelay:Number = 260;//-- delay in milliseconds
        var Dashing:Boolean = false;
        var RightDash:Boolean = false;
        var LeftDash:Boolean = false;

        public function Player()
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyDown);
            stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyPressed);
            addEventListener(Event.ENTER_FRAME,Update);
        }

        function KeyDown(event:KeyboardEvent)
        {
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, SecondDash);
            //If key is down cannot dash
            RightDash = false;
            LeftDash = false;

            //When Key is Down
            if (event.keyCode == 39)
            {
                RightKeyPress = true;
            }

            if (event.keyCode == 37)
            {
                LeftKeyPress = true;
            }

            if (event.keyCode == 38)
            {
                UpKeyPress = true;
            }
        }

        function KeyPressed(event:KeyboardEvent):void
        {
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
            stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);

            //If on floor
            if (CanJump)
            {
                //If right key is down
                if (event.keyCode == 39)
                {
                    if (event.keyCode == 39 && Pressed)
                    {
                        //If right key press matches with recent right key press, dash right
                        Dashing = true;
                        RightDash = true;
                    }
                    Pressed = true;
                    setTimeout(function(){Pressed = false}, DoubleTapDelay);
                }

                if (event.keyCode == 37)
                {
                    if (event.keyCode == 37 && Pressed)
                    {
                        //If left key press matches with recent left key press, dash left
                        Dashing = true;
                        LeftDash = true;
                    }
                    Pressed = true;
                    setTimeout(function(){Pressed = false}, DoubleTapDelay);
                }

            }
        }



        function Update(event:Event)
        {

            //Adding gravity to the game world
            JumpPower +=  Gravity;
            //if player is more than 300 on the y-axis
            if (this.y > 300)
            {
                //Player stays on the ground and can jump
                JumpPower = 0;
                CanJump = true;
            }


                //If already jumped and on floor
                if (Jumped == true && CanJump)
                {
                    //Cannot jump again
                    CanJump = false;

                    //If on floor and right key is pressed run right
                    if ((RightKeyPress))
                    {
                        gotoAndStop('Run');
                        scaleX = 1;
                    }
                    else if ((LeftKeyPress))
                    {
                        //Otherwise if on floor and left key is pressed run left
                        gotoAndStop('Run');
                        scaleX = -1;
                    }

                    //If no key is pressed stay idle
                    if ((!RightKeyPress && !LeftKeyPress))
                    {
                        gotoAndStop('Idle');
                    }
                }


                //If on floor and can jump
                if (CanJump)
                {
                    //If right key is pressed run right
                    if ((RightKeyPress))
                    {
                        x +=  RunSpeed;
                        gotoAndStop('Run');
                        scaleX = 1;
                    }
                    else if ((LeftKeyPress))
                    {
                        //otherwise if left key is pressed run left
                        x -=  RunSpeed;
                        gotoAndStop('Run');
                        scaleX = -1;
                    }

                    if ((UpKeyPress))
                    {
                        //If up key is pressed then jump
                        JumpPower = -15;
                        CanJump = false;
                        gotoAndStop('Jump');
                        Jumped = true;
                    }

                    //If no key is pressed stay idle
                    if ((!RightKeyPress && !LeftKeyPress && CanJump))
                    {
                        gotoAndStop('Idle');
                    }
                }
                else if (CanJump == false)
                {
                    //Otherwise if in air and right key is pressed move right
                    if ((RightKeyPress))
                    {
                        x +=  RunSpeed;
                        scaleX = 1;
                    }
                    else if ((LeftKeyPress))
                    {
                        //Otherwise if left key is pressed then move left
                        x -=  RunSpeed;
                        scaleX = -1;
                    }
                }


                //If Dashing is true
                if (Dashing == true)
                {
                    //Dash right
                    if (RightDash == true)
                    {
                        stage.addEventListener(KeyboardEvent.KEY_DOWN, SecondDash);
                        stage.removeEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
                        TweenLite.to(this,0,{blurFilter:{blurX:1000}});
                        TweenLite.to(this,0.2,{blurFilter:{blurX:0},x:x + DashAmount,ease:Expo.easeOut});
                        Dashing = false;
                    }
                    else if (LeftDash == true)
                    {
                        //Otherwise dash left
                        stage.addEventListener(KeyboardEvent.KEY_DOWN, SecondDash);
                        stage.removeEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
                        TweenLite.to(this,0,{blurFilter:{blurX:1000}});
                        TweenLite.to(this,0.2,{blurFilter:{blurX:0},x:x - DashAmount,ease:Expo.easeOut});
                        Dashing = false;
                    }
                }
                else if (Dashing == false)
                {
                    stage.removeEventListener(KeyboardEvent.KEY_DOWN, SecondDash);
                }


            this.y +=  JumpPower;
        }


        function SecondDash(event:KeyboardEvent)
        {
            stage.removeEventListener(KeyboardEvent.KEY_DOWN, SecondDash);

            if (event.keyCode == 38)
            {
                TweenLite.to(this,0,{blurFilter:{blurX:0}});
                TweenLite.to(this,0,{blurFilter:{blurY:1000}});
                TweenLite.to(this,0.5,{blurFilter:{blurY:0},y:y - DashAmount,ease:Expo.easeOut});
            }
        }

        function KeyUp(event:KeyboardEvent)
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
            RightDash = false;
            LeftDash = false;

            if (event.keyCode == 39)
            {
                RightKeyPress = false;
            }

            if (event.keyCode == 37)
            {
                LeftKeyPress = false;
            }

            if (event.keyCode == 38)
            {
                UpKeyPress = false;
                Jumped = false;
            }
        }
    }
}

1 个答案:

答案 0 :(得分:3)

看起来你甚至都没有尝试过攻击动画......至少从我在你的代码中看到的情况来看。如果你至少尝试过自己,然后发布,那将会很有帮助,但话说回来,我会给你一些起点。另外,我不确定你是否正在使用文档类和其他类来制作这个游戏,或者如果你只是使用时间线.....下面将使用类。如果你没有使用课程......现在停下来,去了解AS3到底是什么。如果可能,您永远不想在AS3中的时间轴上进行编码。

假设我们有3次攻击,包括键“1”,“2”和“3”。将这些变量与其他变量放在一起......就在var LeftDash:Boolean = False正常之后。 (另外,只需将您的所有var更改为private var ....如果这不起作用,只需使用public var ....只是为了完整性)

private var attack1Boolean:Boolean = false;
private var attack2Boolean:Boolean = false;
private var attack3Boolean:Boolean = false;

接下来,使用箭头键的keydown和keyup做同样的事情。将以下内容放在KeyDown方法中。

if (event.keyCode == 49)
{
    attack1Boolean = true;
}
if (event.keyCode == 50)
{
    attack2Boolean = true;
}
if (event.keyCode == 51)
{
    attack3Boolean = true;
}

然后在您的KeyUp方法中添加以下内容

if (event.keyCode == 49)
{
    attack1Boolean = false;
}
if (event.keyCode == 50)
{
    attack2Boolean = false;
}
if (event.keyCode == 51)
{
    attack3Boolean = false;
}

现在您已准备好开始实施攻击。你说你已经为攻击创建了3个动画。这意味着每次攻击都应该拥有自己的时间线(你可以在一个时间轴上拥有全部3个,但也可以用帧分隔,但我不建议这样做)。为了做你想做的事(在下一次攻击发生之前完成每个动画)你将不得不使用gotoAndStopgotoAndPlay

为每个动画创建一个新的.as文件是最有用的。如果你将所有3个动画组合成一个时间轴......那就好了,只需创建一个新的.as文件而不是3个。现在将它们全部链接起来就像你想的那样(如果你不知道怎么做...再一次...去做一个真正的AS3教程...你已经超越自己了)。

您必须创建这些新类文件的实例,因此在您的vars的顶部,添加以下内容(如果您将新类命名为Animation1Animation2 ...等。 )

private var animation1:Animation1;
private var animation2:Animation2;
private var animation3:Animation3;

然后在函数Player put

animation1 = new Animation1();
animation2 = new Animation2();
animation3 = new Animation3();

现在你的动画应该可以使用了。转到Update方法并添加以下内容

//note, the animation1.currentFrame == 1 doesn't have to be 1...it's just the initial starting frame of the animation
if (attack1Boolean && 
    animation1.currentFrame == 1 &&
    animation2.currentFrame == 1 && 
    animation3.currentFrame == 1)
{
    animation1.x = //character.x if you have a class...this.x for yours
    animation1.y = //character.y if you have a class...this.y for yours
    addChild(animation1);
    attack1Boolean = false;
    if (animation1.currentFrame = //the last frame of animation1...a number)
    {
        animation1.gotoAndStop(1);
        removeChild(animation1);
    }
}

if (attack2Boolean && 
    animation1.currentFrame == 1 &&
    animation2.currentFrame == 1 && 
    animation3.currentFrame == 1)
{
    animation2.x = //character.x if you have a class...this.x for yours
    animation2.y = //character.y if you have a class...this.y for yours
    addChild(animation2);
    attack2Boolean = false;
    if (animation2.currentFrame = //the last frame of animation2...a number)
    {
        animation2.gotoAndStop(1);
        removeChild(animation2);
    }
}

if (attack3Boolean && 
    animation1.currentFrame == 1 &&
    animation2.currentFrame == 1 && 
    animation3.currentFrame == 1)
{
    animation3.x = //character.x if you have a class...this.x for yours
    animation3.y = //character.y if you have a class...this.y for yours
    addChild(animation3);
    attack3Boolean = false;
    animation3.gotoAndStop(1);
    if (animation3.currentFrame = //the last frame of animation3...a number)
    {
        animation3.gotoAndStop(1);
        removeChild(animation3);
    }
}

那应该启动角色动画并制作它,这样我们只能在其他人完成后使用一次攻击。此外,由于动画必须在它自己的第1帧上开始的条件几乎也解决了按住键并重复启动动画的问题(尽管如此,你可以继续按住键并开始攻击一旦动画完成,但我不认为这是一个大问题...如果它让我知道)。

尝试所有这些,告诉我有多少错误,因为我是从记忆中做到这一点,如果有帮助就投票,=)。