【问题标题】:Flash Actionscript 3.0 Game Projectile CreationFlash Actionscript 3.0 游戏弹丸创建
【发布时间】:2011-11-07 00:32:17
【问题描述】:

-------- 最初的问题已修复,但我有一个新问题 - 请参见下文 ---------- ----

在我的横向卷轴游戏中,我尝试制作玩家射击的吹镖。当然,我希望飞镖从玩家所在的位置开始,然后飞镖朝他们的方向移动。

但是,我有一个问题。在我的游戏中,我使用了一个滚动功能,将玩家放在屏幕中间。这是那个函数:

        // 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()' 方法,但我对如何在这种情况下使用它感到困惑。

如何在相对于播放器的正确位置创建飞镖?

如果有任何帮助,我将不胜感激。谢谢!

------------------------------------------ 编辑和更新--------------------------

编辑和更新:我在正确的位置(靠近玩家——见下面的 cmets)创建了飞镖,并使用“movePlayerDarts()”函数移动它们。但我实际上有一个新问题。当玩家发射飞镖后移动时,飞镖会跟随他!如果玩家跳跃,飞镖就会升起。如果玩家向左移动,飞镖会稍微向左移动。

显然,某处有一些代码告诉飞镖跟随玩家。我不明白如何,除非“playerDartContainer”与此有关。但是容器始终在位置 (0,0) 并且它不会移动。

此外,作为测试,我在不断运行的“movePlayerDarts()”函数中追踪了飞镖的“y”坐​​标。如您所见,该函数通过增加 y 坐标值不断将 dart 沿 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);
            }
    }

【问题讨论】:

    标签: flash actionscript-3


    【解决方案1】:

    playerDartContainer 应该与您的 player 实例在同一级别实例化。如果它没有更新它相对于玩家的位置,那么实例化并添加到它的飞镖就会出现在它所在的任何位置。

    第一个解决方案是设置playerDartContainer的位置为playerDartContainer.x = player.xplayerDartContainer.y = player.y无论您在哪里更新您的玩家位置。将飞镖的实例更改为显示在 0,0。

    第二种解决方案是将位置设置为 0,0(始终是屏幕的左上角),并且 playerDart 的偏移量与 playerDartContainer 中的玩家位置相同。由于您的玩家从不真正移动,因此 playerDartContainer 也不应该真正移动,因此您的飞镖将出现在与玩家相同的相对空间中。

    【讨论】:

    • 谢谢,多米尼克。但是,我仍然遇到问题。我将以下代码放在 scrollScreen() 函数中,它使播放器在屏幕上居中:playerDartContainer.x = player.x; playerDartContainer.y = player.y; 然后在“createDart()”函数中,我尝试将飞镖定位在 (0,0) 和'playerDartContainer' 的坐标。但是飞镖仍然没有出现在玩家面前;它们的坐标是相对于舞台的,而不是容器或玩家的。
    • 嗨,克里斯蒂安。尝试将 playerDartContainer.x 和 y 设置为 (0,0)。然后将飞镖设置为 player.x 坐标。如果将其设置为 playerDartContainer 的坐标(它位于其中,并且设置为等于 player.x 位置),则与将其设置为玩家的坐标相同。所以这是多余的,也不清楚。追踪 playerDartContainer 的位置。如果这些都不起作用,您也可以尝试将飞镖设置为 stage.x * 0.5 和 stage.y * 0.5。
    • 嗨,多米尼克。好的,我得到它的工作! :D 有一段时间没用,但后来我记得我在 'envContainer' 中添加了 'playerDartContainer',它存储所有内容并由 'scrollScreen()' 滚动。所以最后,我设置了playerDartContainer.x = 0;playerDartContainer.y = 0; 在玩家坐标playerDart.x = player.x + 12 附近创建了新飞镖,并从'envContainer'中删除了'playerDartContainer'。非常感谢您的帮助,多米尼克!
    • 摇滚吧!是的,请确保您的“容器”实例都生活在相对于该阶段 + 其他移动实例的某个位置。
    猜你喜欢
    • 2011-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-15
    相关资源
    最近更新 更多