Actionscript 3 AS3按住按钮时的延伸跳跃

Actionscript 3 AS3按住按钮时的延伸跳跃,actionscript-3,actionscript,Actionscript 3,Actionscript,所以我的马里奥项目必须包括马里奥动作的一个主要部分,当然是选择跳一个较短的高度或相当大的高度。我们都知道,按住跳转按钮可以让他跳得更高,这就是我的目标。在我的例子中,这个按钮是X,我不确定怎么做 这是我目前未成功的尝试,在我的变量中,重力默认设置为0.87 这在我的keyDownHandler中(按键时) 这在我的keyUpHandler中(当按键未按下/放开时) 好的,我已经扩展了我的评论 您可以使用标准的vy=vyLast-g*(t-tLast),在释放跳转键时,只需将vyLast设置为mi

所以我的马里奥项目必须包括马里奥动作的一个主要部分,当然是选择跳一个较短的高度或相当大的高度。我们都知道,按住跳转按钮可以让他跳得更高,这就是我的目标。在我的例子中,这个按钮是X,我不确定怎么做

这是我目前未成功的尝试,在我的变量中,重力默认设置为0.87

这在我的keyDownHandler中(按键时)

这在我的keyUpHandler中(当按键未按下/放开时)


好的,我已经扩展了我的评论


您可以使用标准的
vy=vyLast-g*(t-tLast)
,在释放跳转键时,只需将
vyLast设置为
min(0,vyLast)
,并在地面上按下跳转键时将其设置为跳转起动速度

下面是一个带有红色圆圈的AdobeAIR应用程序示例。它实现了我在注释中描述的逻辑:

 <?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"                
                       >
    <fx:Script>
        <![CDATA[
            import flash.utils.getTimer;
            import mx.graphics.SolidColor;
            public var marioY:Number = 0; //jump height above ground (meters)
            public var g:Number = -9.81; //meter/(second^2)
            public var lastTime:Number = NaN;
            public var lastVerticalSpeed:Number = 0;//speed of a flight -meters/second
            public var jumpSpeed:Number = 10;//initial jump speed - meters/second
            public var timeRatio:Number = 1000;//milliseconds in a second
            public var heightRatio:Number = 50; //pixels/meter

            protected function get landed():Boolean{
                return marioY <= 0 && lastVerticalSpeed <= 0;
            }

            protected function onKeyDown(event:KeyboardEvent):void{
                if(event.keyCode==Keyboard.UP && landed){
                    lastVerticalSpeed = jumpSpeed;
                    trace('fly!');
                }               
            }

            protected function onKeyUp(event:KeyboardEvent):void{
                if(event.keyCode==Keyboard.UP){
                    lastVerticalSpeed = Math.min(0,lastVerticalSpeed);
                    trace('fall!');
                }

            }

            protected function onEnterFrame(event:Event):void{
                if(!isNaN(lastTime)){
                    var deltaTime:Number = (getTimer() - lastTime)/timeRatio;                   
                    marioY+=lastVerticalSpeed*deltaTime;
                    if(landed){
                        lastVerticalSpeed=0;
                        marioY=0;
                    }else{
                        lastVerticalSpeed+=g*deltaTime;
                    }
                }
                mario.y=area.height-marioY*heightRatio-20;  
                lastTime = getTimer();              
            }

        ]]>
    </fx:Script> 
    <s:Group width="100%" height="100%" keyDown="onKeyDown(event)" keyUp="onKeyUp(event)"
             enterFrame="onEnterFrame(event)" id="area"
             creationComplete="area.setFocus()"
             >
        <s:Rect width="100%" height="100%" fill="{new SolidColor(0x0000FF)}"/>
        <s:Ellipse id="mario" width="10" height="10" fill="{new SolidColor(0xFF0000)}"
                   y="100" x="100"
                   />

    </s:Group>
</s:WindowedApplication>


你可以使用标准的
vy=vyLast-g*(t-tLast)
,当跳转键被释放时,只需将
vyLast设置为
min(0,vyLast)
,并在地面上按下跳转键时将其设置为跳转启动速度。对不起,我只学习了大约一个月的AS3,我不太清楚你的意思,也许您可以在代码中使用它作为示例?
    if (event.keyCode == Keyboard.X)
        {
            if (holdJump == false)
            {
                accy = 0;
                gravity = 0.80;
                incSpeedY = 0;
            }
        }
 <?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx"                
                       >
    <fx:Script>
        <![CDATA[
            import flash.utils.getTimer;
            import mx.graphics.SolidColor;
            public var marioY:Number = 0; //jump height above ground (meters)
            public var g:Number = -9.81; //meter/(second^2)
            public var lastTime:Number = NaN;
            public var lastVerticalSpeed:Number = 0;//speed of a flight -meters/second
            public var jumpSpeed:Number = 10;//initial jump speed - meters/second
            public var timeRatio:Number = 1000;//milliseconds in a second
            public var heightRatio:Number = 50; //pixels/meter

            protected function get landed():Boolean{
                return marioY <= 0 && lastVerticalSpeed <= 0;
            }

            protected function onKeyDown(event:KeyboardEvent):void{
                if(event.keyCode==Keyboard.UP && landed){
                    lastVerticalSpeed = jumpSpeed;
                    trace('fly!');
                }               
            }

            protected function onKeyUp(event:KeyboardEvent):void{
                if(event.keyCode==Keyboard.UP){
                    lastVerticalSpeed = Math.min(0,lastVerticalSpeed);
                    trace('fall!');
                }

            }

            protected function onEnterFrame(event:Event):void{
                if(!isNaN(lastTime)){
                    var deltaTime:Number = (getTimer() - lastTime)/timeRatio;                   
                    marioY+=lastVerticalSpeed*deltaTime;
                    if(landed){
                        lastVerticalSpeed=0;
                        marioY=0;
                    }else{
                        lastVerticalSpeed+=g*deltaTime;
                    }
                }
                mario.y=area.height-marioY*heightRatio-20;  
                lastTime = getTimer();              
            }

        ]]>
    </fx:Script> 
    <s:Group width="100%" height="100%" keyDown="onKeyDown(event)" keyUp="onKeyUp(event)"
             enterFrame="onEnterFrame(event)" id="area"
             creationComplete="area.setFocus()"
             >
        <s:Rect width="100%" height="100%" fill="{new SolidColor(0x0000FF)}"/>
        <s:Ellipse id="mario" width="10" height="10" fill="{new SolidColor(0xFF0000)}"
                   y="100" x="100"
                   />

    </s:Group>
</s:WindowedApplication>