2013-02-11 29 views
1

我正在製作一款平臺遊戲。但是我有問題,因爲每當我按空格鍵跳躍時,角色都會陷入半空中。但是,我可以通過握住空格鍵來解決問題,角色將落地。Actionscript 3製作角色跳轉

問題在mainJump()位於Boy類內。

我看到很多人通過使用動作時間線來解決問題,但我的主要問題是,無論如何,我可以通過使用外部類來解決問題嗎?

主要類

package 
{ 
import flash.display.*; 
import flash.text.*; 
import flash.events.*; 
import flash.utils.Timer; 
import flash.text.*; 

public class experimentingMain extends MovieClip 
{ 
    var count:Number = 0; 
    var myTimer:Timer = new Timer(10,count); 

    var classBoy:Boy; 

    //var activateGravity:gravity = new gravity(); 

    var leftKey, rightKey, spaceKey, stopAnimation:Boolean; 

    public function experimentingMain() 
    { 
     myTimer.addEventListener(TimerEvent.TIMER, scoreUp); 
     myTimer.start(); 

     classBoy = new Boy(); 
     addChild(classBoy); 


     stage.addEventListener(KeyboardEvent.KEY_DOWN, pressTheDamnKey); 
     stage.addEventListener(KeyboardEvent.KEY_UP, liftTheDamnKey); 
    } 

    public function pressTheDamnKey(event:KeyboardEvent):void 
    { 
     if (event.keyCode == 37) 
     { 
      leftKey = true; 
      stopAnimation = false; 
     } 

     if (event.keyCode == 39) 
     { 
      rightKey = true; 
      stopAnimation = false; 
     } 

     if (event.keyCode == 32) 
     { 
      spaceKey = true; 
      stopAnimation = true; 
     } 
    } 

    public function liftTheDamnKey(event:KeyboardEvent):void 
    { 
     if (event.keyCode == 37) 
     { 
      leftKey = false; 
      stopAnimation = true; 
     } 

     if (event.keyCode == 39) 
     { 
      rightKey = false; 
      stopAnimation = true; 
     } 

     if (event.keyCode == 32) 
     { 
      spaceKey = false; 
      stopAnimation = true; 
     } 
    } 

    public function scoreUp(event:TimerEvent):void 
    { 
     scoreSystem.text = String("Score : "+myTimer.currentCount); 
    } 

} 
    } 

男孩類

package 
{ 
import flash.display.*; 
import flash.events.*; 

public class Boy extends MovieClip 
{ 
    var leftKeyDown:Boolean = false; 
    var upKeyDown:Boolean = false; 
    var rightKeyDown:Boolean = false; 
    var downKeyDown:Boolean = false; 
    //the main character's speed 
    var mainSpeed:Number = 5; 
    //whether or not the main guy is jumping 
    var mainJumping:Boolean = false; 
    //how quickly should the jump start off 
    var jumpSpeedLimit:int = 40; 
    //the current speed of the jump; 
    var jumpSpeed:Number = 0; 

    var theCharacter:MovieClip; 

    var currentX,currentY:int; 

    public function Boy() 
    { 
     this.x = 600; 
     this.y = 540; 

     addEventListener(Event.ENTER_FRAME, boyMove); 
    } 

    public function boyMove(event:Event):void 
    { 
     currentX = this.x; 
     currentY = this.y; 

     if (MovieClip(parent).leftKey) 
     { 
      currentX += mainSpeed; 
      MovieClip(this).scaleX = 1; 
     } 

     if (MovieClip(parent).rightKey) 
     { 
      currentX -= mainSpeed; 
      MovieClip(this).scaleX = -1; 
     } 

     if (MovieClip(parent).spaceKey) 
     { 
      mainJump(); 
     } 

     this.x = currentX; 
     this.y = currentY; 
    } 

    public function mainJump():void 
    { 
     currentY = this.y; 


     if (! mainJumping) 
     { 

      mainJumping = true; 
      jumpSpeed = jumpSpeedLimit * -1; 
      currentY += jumpSpeed; 
     } 
     else 
     { 
      if (jumpSpeed < 0) 
      { 
       jumpSpeed *= 1 - jumpSpeedLimit/250; 
       if (jumpSpeed > -jumpSpeedLimit/12) 
       { 
        jumpSpeed *= -2; 
       } 
      } 
     } 
     if (jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit) 
     { 
      jumpSpeed *= 1 + jumpSpeedLimit/120; 
     } 
     currentY += jumpSpeed; 

     if (currentY >= stage.stageHeight - MovieClip(this).height) 
     { 
      mainJumping = false; 
      currentY = stage.stageHeight - MovieClip(this).height; 
     } 
    } 
    } 
    } 

回答

0

你已經有了一個mainJumping變量跳躍運行時,這只是事實。爲什麼不使用它?所有的

if (MovieClip(parent).spaceKey || mainJumping) 
{ 
    mainJump(); 
} 
+0

是的,它的工作感謝隊長! – Minelava 2013-02-11 16:44:06

1

首先,正式的代碼,從而消除了時髦的東西,如「pressTheDamnKey」,這甚至不說明功能非常好,因爲函數不能按一個鍵。這是一個事件處理程序,應該命名爲keyDownHandler或onKeyDown,沒有別的。其次,除了事件數據的直接問題之外,您很少想在事件處理程序中執行任何實際工作。而是呼籲實際工作的功能。處理程序處理事件,然後調用執行該工作的代碼。這很好地分離出了關注點,當你想要其他其他也能夠使小男孩除了enterFrameHandler之外還有動畫,就像鼠標一樣。

我可以想象,由於您的計時器每秒發射100次(每個10毫秒),因此您的跟蹤日誌會很快被「分數」線填滿。我會改變這種情況,不要在計時器上觸發,但要在得分實際發生變化時進行刷新。

除了意大利麪代碼之外,跳躍的問題在於,您是通過將按鍵的狀態保存在變量中並讓他不斷檢查它來確定是否按下了鍵。這有幾個原因是不好的:1.他不應該需要向他的環境尋求信息,應該通過他擁有的任何物品或負責告訴他的物品給他。2.它需要你繼續按住空格鍵,否則他將停止移動,因爲他檢查了是否被按住(見問題1)。

我會在下面解決所有這些問題,而不考慮得分,這完全是另一回事。

package 
{ 
    import flash.display.*; 
    import flash.events.*; 
    import flash.text.*; 
    import flash.utils.*; 

    // Sprite is preferred if you are not using the timeline 
    public class Application extends Sprite 
    { 
     private var boy:Boy; 

     public function Application() 
     { 
      boy = new Boy(); 
      addChild(boy); 
      boy.x = 600; // set these here, not in the boy 
      boy.y = 540; 
      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); 
      stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler ); 
     } 

     public function keyDownHandler(event:KeyboardEvent):void 
     { 
      switch(event.keyCode) 
      { 
       case 32: boy.jump(); 
         break; 
       case 37: boy.moveLeft(); 
         break; 
       case 39: boy.moveRight(); 
         break; 
       default: 
        // ignored 
        break; 
      } 
     } 

     public function keyUpHandler(event:KeyboardEvent):void 
     { 
      switch(event.keyCode) 
      { 
       // ignored for jumping (32) 
       case 37: // fall through 
       case 39: boy.stop(); 
         break; 
       default: 
        // ignored 
        break; 
      } 
     } 

    }//class 
}//package 

package 
{ 
    import flash.display.*; 
    import flash.events.*; 

    // It is assumed that there is an asset in the library 
    // that is typed to a Boy, thus it will be loaded onto 
    // the stage by the owner 
    public class Boy extends Sprite 
    { 
     private var horzSpeed :Number = 0; 
     private var vertSpeed :Number = 0; 
     private var floorHeight :Number; 
     private var jumpHeight :Number; 
     private var amJumping :Boolean = false; 

     public function Boy() 
     { 
      addEventListener(Event.ENTER_FRAME, enterFrameHandler); 
     } 

     public function moveLeft():void 
     { 
      horzSpeed = -1; 
     } 

     public function moveRight():void 
     { 
      horzSpeed = 1; 
     } 

     public function stop():void 
     { 
      horzSpeed = 0; 
     } 

     public function jump():void 
     { 
      if (amJumping) return; 
      floorHeight = y; 
      jumpHeight = floorHeight + 20; 
      vertSpeed = 2; 
      amJumping = true; 
      animateJump(); 
     } 

     private function enterFrameHandler(event:Event):void 
     { 
      animate(); 
     } 

     private function animate():void 
     { 
      x += horzSpeed; 
      if(amJumping) 
      { 
       animateJump(); 
      } 
     } 

     // Doing a simple version for this example. 
     // If you want an easier task of jumping with gravity, 
     // I recommend you employ Greensock's superb 
     // TweenLite tweening library. 
     private function animateJump():void 
     { 
      y += vertSpeed; 
      if(y >= jumpHeight) 
      { 
       y = jumpHeight; 
       vertSpeed = -2; 
      } 
      else if(y <= floorHeight) 
      { 
       y = floorHeight; 
       amJumping = false; 
      } 
     } 

    }//class 
}//package 

另一種方式接近這一點,可能是更好的途徑長遠,是讓孩子甚至不負責移動自己。相反,你會在父母,他的主人或一些特殊的動畫師課程中處理這個課程,這個課程負責按計劃進行動畫製作。在這個更加封閉的範例中,這個男孩只負責根據外部世界更新他自己的內部外表,告訴他他正在發生什麼事情。他不再處理內部跳躍,而是負責做像他自己擁有的東西一樣的動畫,比如他的手臂和腿。