2011-11-07 60 views
1

--------初始問題固定,但我有一個新的PROLEM - 請參閱下面------------------ --------Flash Actionscript 3.0 Game Projectile Creation

在我的側面滾動遊戲中,我正在嘗試製作玩家射擊的飛鏢。當然,我希望飛鏢能夠從玩家所在的位置開始,然後飛鏢會朝着他們的方向移動。

但是,我有一個問題。在我的遊戲中,我使用了一個滾動功能,將玩家放在屏幕中間。下面是函數:

 // This function scrolls the MovieClip Containers on the screen 
    private function scrollScreen(event:Event):void { 

     // Here we offset the elements' 'x' and 'y' coordinates by 
     // the distance between the Player and the centre of the stage 
      // Environment Container 
      envContainer.x += (stage.stageWidth * 0.5) - player.x; 
      // Background Container 
       // Background moves more slowly along the 'x' axis to simulate distance 
      bgContainer.x += ((stage.stageWidth * 0.5) - player.x) * 1/20; 
      // Position the Player at the centre of the game screen ('x' axis) 
      player.x = stage.stageWidth * 0.5; 

     // Here we offset the elements' 'y' coordinates 
      // Environment Container 
      envContainer.y += (stage.stageHeight * 0.5) - player.y; 
      // Background Container 
      bgContainer.y += (stage.stageHeight * 0.5) - player.y; 
      // Position the Player at the centre of the game screen ('y' axis) 
      player.y = stage.stageHeight * 0.5;    
    } // End of 'scrollScreen()' function  

至於我可以告訴大家,在「envContainer」和「bgContainer」滾動無關與我談論這個問題。我只關心玩家的定位。

現在,這裏是我用來創建飛鏢功能:

// This function creates a dart 
    private function createDart():void { 

     if (playerDartContainer.numChildren <= 4) { 

      trace("DART!"); 

      playerDart = new PlayerDart(); 

      playerDart.x = player.x; 
      playerDart.y = player.y; 

      trace("PD x: " + playerDart.x); 
      trace("Player x: " + player.x); 
      trace("PD y: " + playerDart.y); 
      trace("Player y: " + player.y); 

      // Set the new dart's direction to equal that of the player 
      // Player's facing right 
      if (player.scaleX == 1) { 
       // So dart faces right, too 
       playerDart.scaleX = 1; 
      } 
      // Player's facing left 
      else if (player.scaleX == -1) { 
       // So dart faces left, too 
       playerDart.scaleX = -1; 
      } 

      playerDartContainer.addChild(playerDart); 

      trace("Num children: " + playerDartContainer.numChildren); 
     } 
    } 

什麼情況是,在飛鏢總是添加到舞臺上在點(350,350) - 確切點的播放器被置於(相對於屏幕,這是700 * 700)。所以我知道這個問題與程序認爲Player和Dart對象應該是的座標有關。我只是不知道如何解決這個問題。我已經看過'globalToLocal()'方法,但我很困惑如何在這種情況下使用它。

如何讓飛鏢在相對於玩家的正確位置上創建?

我很感激任何幫助。謝謝!

------------------------------------------ EDIT AND UPDATE --------------------------

編輯和更新:我得到飛鏢創建在正確的地方(靠近玩家 - 見下面的評論)和一個'movePlayerDarts()'函數移動它們。但我確實有一個新問題。當玩家在擲鏢後移動時,鏢跟着他!如果玩家跳躍,飛鏢會上升。如果玩家向左移動,飛鏢會稍微向左移動。

很明顯,有一些代碼告訴飛鏢跟隨玩家。我不明白,除非'playerDartContainer'與此有關。但容器始終處於位置(0,0)並且不移動。

另外,作爲測試,我在不斷運行的'movePlayerDarts()'函數中追蹤了飛鏢的'y'座標。正如你所看到的那樣,該函數通過增加它的y座標值,不斷地將飛鏢向y軸下移。但是當我跳起來的時候,被追蹤的'y'座標永遠不會減少,儘管飛鏢顯然看起來像是在上升!

如果有人有任何建議,我會很感激他們!

這裏是我用來創建飛鏢代碼:

 // This function creates a dart 
    public function createDart():void { 

     if (playerDartContainer.numChildren <= 4) { 
      // Play dart shooting sound 
      sndDartShootIns.play(); 

      // Create a new 'PlayerDart' object 
      playerDart = new PlayerDart(); 

      // Set the new dart's initial position and direction depending on the player's direction 
      // Player's facing right 
      if (player.scaleX == 1) { 
       // Create dart in front of player's dart gun 
       playerDart.x = player.x + 12; 
       playerDart.y = player.y - 85; 
       // Dart faces right, too 
       playerDart.scaleX = 1; 
      } 
      // Player's facing left 
      else if (player.scaleX == -1) { 
       // Create dart in front of player's dart gun 
       playerDart.x = player.x - 12; 
       playerDart.y = player.y - 85; 
       // Dart faces left, too 
       playerDart.scaleX = -1; 
      } 
      playerDartContainer.addChild(playerDart); 
     } 
    } // End of 'createDart()' function 

此代碼是EnterFrameHandler玩家飛鏢:

 // In every frame, call 'movePlayerDarts()' to move the darts within the 'playerDartContainer' 
    public function playerDartEnterFrameHandler(event:Event):void { 
     // Only move the Player's darts if their container has at least one dart within 
     if (playerDartContainer.numChildren > 0) { 
      movePlayerDarts(); 
     } 
    }      

最後,這是實際上將所有的代碼玩家的飛鏢:

  // Move all of the Player's darts 
    public function movePlayerDarts():void { 
      for (var pdIns:int = 0; pdIns < playerDartContainer.numChildren; pdIns++) { 
       // Set the Player Dart 'instance' variable to equal the current PlayerDart 
       playerDartIns = PlayerDart(playerDartContainer.getChildAt(pdIns)); 

       // Move the current dart in the direction it was shot. The dart's 'x-scale' 
        // factor is multiplied by its speed (5) to move the dart in its correct 
        // direction. If the 'x-scale' factor is -1, the dart is pointing left (as 
        // seen in the 'createDart()' function. (-1 * 5 = -5), so the dart will go 
        // to left at a rate of 5. The opposite is true for the right-ward facing 
        // darts 
       playerDartIns.x += playerDartIns.scaleX * 1; 

       // Make gravity work on the dart 
       playerDartIns.y += 0.7; 
       //playerDartIns.y += 1; 

       // What if the dart hits the ground? 
       if (HitTest.intersects(playerDartIns, floor, this)) { 
        playerDartContainer.removeChild(playerDartIns); 
       } 

       //trace("Dart x: " + playerDartIns.x); 
       trace("Dart y: " + playerDartIns.y); 
      } 
    } 

回答

1

playerDartContainer應該在與您的播放器實例相同的級別實例化。如果它沒有更新它相對於玩家的位置,那麼實例化並添加到其上的飛鏢出現在任何地方。

第一種方法是設置playerDartContainer的位置是playerDartContainer.x = player.xplayerDartContainer.y = player.y無論你更新你的球員的位置。將您的飛鏢的實例化更改爲0,0。

第二種解決方案是將位置設置爲0,0(始終是屏幕的左上角),並且playerDart的偏移位置與playerDartContainer中玩家的位置相同。由於玩家從未真正移動過,所以玩家DartContainer以及您的飛鏢都不會出現在與玩家相同的相對空間中。

+0

謝謝,多米尼克。但是,我仍然有問題。我將下面的代碼放置在scrollScreen()函數中,該函數將播放器置於屏幕中央:'playerDartContainer.x = player.x;然後在'createDart()'函數中,我試圖將飛鏢定位在(0,0)和'playerDartContainer'的座標上。但飛鏢仍然不會出現在玩家面前;他們的座標是相對於舞臺而不是容器或玩家。 –

+0

嗨Christian。嘗試將playerDartContainer.x和y設置爲(0,0)。然後將飛鏢設置爲player.x座標。如果您將其設置爲playerDartContainer的座標(它位於內部,且設置爲與player.x位置相同),則將其設置爲與玩家的座標相同。所以這是多餘的,也不清楚。追蹤playerDartContainer的位置。你也可以,如果這兩者都不起作用,可以嘗試將dart設置爲stage.x * 0.5和stage.y * 0.5。 –

+1

嗨多米尼克。好吧,我已經開始工作了! :D它一段時間沒有工作,但是我記得我將'playerDartContainer'添加到了'envContainer'中,它存儲了所有內容,並由'scrollScreen()'滾動。所以最後,我設置了'playerDartContainer.x = 0;'和'playerDartContainer.y = 0;'在玩家座標'playerDart.x = player.x + 12'附近創建了新的飛鏢,並從中移除了'playerDartContainer' 'envContainer。'非常感謝您的幫助,Dominic! –