2013-08-01 93 views
0

所以我的馬里奧項目必須包括一個馬里奧的運動主食,當然是跳選擇一個短的高度或相當大的一個選項。衆所周知,按住跳躍按鈕可以讓他跳得更高,這就是我的目標。在我的情況下,該按鈕是X,我不確定如何做到這一點。AS3擴展跳轉時按住按鈕

這是我目前不成功的嘗試,並且在我的變量中默認情況下重力被設置爲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; 
      } 
     } 
+1

您可以使用標準的'VY = vyLast-G *(T-TLAST)',和剛剛成立'vyLast'到'分鐘(0,vyLast ),當跳躍鍵被釋放時,並且當在地面上按下跳躍鍵時將其設置爲跳躍起始速度。 – user1875642

+0

對不起,我只學了一個月左右的AS3,我不太清楚你的意思,也許你可以在代碼中使用它作爲例子嗎? –

回答

0

好吧,我我已發表了我的評論。

您可以使用標準vy=vyLast-g*(t-tLast),以及剛剛成立vyLastmin(0,vyLast)當跳躍鍵被釋放,並設置它時被壓在地上跳躍鍵跳躍啓動速度。

以下是帶有跳躍紅圈的示例Adobe 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>