1
votes

-------- INITIAL ISSUE FIXED, BUT I HAVE A NEW PROLEM - PLEASE SEE BELOW --------------------------

In my side-scrolling game, I am trying to create blow darts which the player shoots. Of course, I want the darts to start where the Player is and then the darts move in their directions.

However, I have a problem. In my game I use a scrolling function that places the player right in the middle of the screen. Here is that function:

        // 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    

As far as I can tell, the scrolling on the 'envContainer' and 'bgContainer' have nothing to do with the problem I'm talking about. I am only concerned about the player's positioning.

Now here is the function I use to create the darts:

    // 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);
        }
    }

What happens is that the darts are always added to the stage at point (350, 350) - the exact point the Player is placed (relative to the screen, which is 700*700). So I know that the problem has to do with the coordinates that the program believes the Player and Dart objects are supposed to be. I just don't know how to fix this. I have looked at the 'globalToLocal()' method, but I am confused on how to use it in this case.

How do I get the dart to be created in a correct position relative to the Player?

I'd appreciate any help. Thanks!

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

EDIT AND UPDATE: I got the darts to be created in the right place (near the player -- see comments below) and a 'movePlayerDarts()' function moves them. But I actually have a new problem. When the player moves after firing a dart, the dart follows him! If the player jumps, the dart rises up. If the player moves to the left, the dart moves slightly to the left.

Obviously, there is some code somewhere which is telling the darts to follow the player. I do not see how, unless the 'playerDartContainer' has something to do with that. But the container is always at position (0,0) and it does not move.

Also, as a test I traced a dart's 'y' coordinate within the constantly-running 'movePlayerDarts()' function. As you can see, that function constantly moves the dart down the y axis by increasing its y-coordinate value. But when I jump, the 'y' coordinate being traced is never reduced, even though the dart clearly looks like it's rising!

If anybody has any suggestions, I'd appreciate them!

Here is the code I use to create the darts:

        // 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

This code is the EnterFrameHandler for the player darts:

        // 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();
        }
    }                       

And finally, this is the code that actually moves all of the player's darts:

            // 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

1 Answers

1
votes

playerDartContainer should be instantiated at the same level as your player instance. If it's not updating it's position relative to player, than the darts instantiated and added to it are appearing wherever it is.

First solution is to set the position of playerDartContainer to be playerDartContainer.x = player.x and playerDartContainer.y = player.y wherever you update your player position. Change the instantiation of your darts to appear at 0,0.

Second solution is to set the position to be 0,0 (which is always the top left corner of your screen) and the playerDart's are offset to be the same position as the player from within the playerDartContainer. Since your player never really moves, neither should the playerDartContainer, and so your darts will appear in the same relative space as the player.