0

所以我的马里奥项目必须包括马里奥运动的主要内容,这当然是跳短高度或相当大高度的选项。众所周知,按住跳跃键会让他跳得更高,这就是我的目标。就我而言,那个按钮是 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;
            }
        }
4

1 回答 1

0

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

可以使用标准vy=vyLast-g*(t-tLast),松开跳跃键时设置为跳跃开始速度,在地面按下跳跃键时设置跳跃起始速度vyLastmin(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>
于 2013-08-01T20:45:02.643 回答