2011-11-07 76 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! –