【问题标题】:AS3 Extended Jump when Holding ButtonAS3 按住按钮时的扩展跳跃
【发布时间】:2013-08-02 18:18:59
【问题描述】:

所以我的马里奥项目必须包括马里奥运动的主要内容,当然是选择短高度或相当大的高度。众所周知,按住跳跃键让他跳得更高,这就是我的目标。在我的例子中,那个按钮是 X,我不确定该怎么做。

这是我目前的尝试失败,我的变量中默认设置重力为 0.87。

这是在我的 keyDownHandler 中(按下键时)

    if (event.keyCode == Keyboard.X && onGround == true)
        {
            vy +=  jumpForce;
            holdJump = true;
            onGround = false;

            if(holdJump == true && onGround == false)
            {
                _mario.y += 1;
            }
            else
            {
                vy = vy + (grav * 0.20);
                holdJump = false;
            }

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

    if (event.keyCode == Keyboard.X)
        {
            if (holdJump == false)
            {
                accy = 0;
                gravity = 0.80;
                incSpeedY = 0;
            }
        }

【问题讨论】:

  • 你可以使用标准的vy=vyLast-g*(t-tLast),只要松开跳跃键时将vyLast设置为min(0,vyLast),设置为在地面按下跳跃键时的跳跃起始速度。跨度>
  • 抱歉,我才研究AS3大概一个月,不太清楚你的意思,或许你可以在代码中使用它作为例子?

标签: actionscript-3 actionscript


【解决方案1】:

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

你可以使用标准的vy=vyLast-g*(t-tLast),只要松开跳跃键时将vyLast设置为min(0,vyLast),设置为在地面按下跳跃键时的跳跃起始速度。

这是带有跳跃红色圆圈的示例 Adob​​e Air 应用程序。它实现了我在评论中描述的逻辑:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多