Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I have been creating a side-scrolling Actionscript 3.0 game. In this game I want the Player to be able to shoot blow darts as weapons. I had some trouble getting the darts to be created in the right place (in front of the player), but eventually got it working with some help from this page (please look at it for background information on this problem):

http://stackoverflow.com/questions/8031553/flash-actionscript-3-0-game-projectile-creation

I got the darts to be created in the right place (near the player) and a 'movePlayerDarts()' function moves them. But I actually have a new problem. When the player moves after firing a dart, the dart tries to follow 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);
        }
}
share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

You must have added playerDartContainer to the player sprite during your setup, or to another display list container that also holds the player sprite, thus you're moving them both. In that case, the playerDartContainer will always have it's local coordinates as (0,0) as you mentioned -- in terms of it's direct parent, it is not actually moving.


Follow up If you place your player in the middle of the screen, and you place your dart container in the middle of the screen and don't move it, that's where the container's going to stay -- thus the darts will look relative to the player, because their container always stays in the same screen pos as the player. You have to move your background and your darts container, relative to the player. The player's the only thing that should never move (i.e. remain centred). In fact, you have this:

  • document/application class (eg. "YourGameName" or "Main")
    • background sprite (presumably moves relative to player)
    • player sprite (stays in middle of screen)
    • dartContainer sprite (yours stays in middle of screen with player, but should move relative to player)
      • dart 1 (moves relative to container -- which is in same place as player!)
      • dart 2 (moves relative to container -- which is in same place as player!)
      • etc.

I would just have this:

  • document/application class (eg. "YourGameName" or "Main")
    • background sprite (moves relative to player)
    • player sprite (mid screen)
    • dart 1 (moves relative to player)
    • dart 2 (moves relative to player)
    • etc.

That's your choice though. As long as you make sure the container moves in the same way your background does (if you have a scrolling background?), it will work.

public function movePlayerDarts():void {
        //shift the container screen pos relative to the player, since they have
        //different world positions and screen pos is a derived from world pos.
        playerDartContainer.x = (stage.stageWidth / 2) - player.x;
        playerDartContainer.y = (stage.stageHeight / 2) - player.y;

        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);
        }
}
share|improve this answer
    
Hi, Nick. During level loads I add playerDartContainer to the this: this.addChild(playerDartContainer); . I have added other containers to this, including envContainer, which stores the floor and other environmental objects. I have tried adding the dart container to envContainer, but that caused problems, as seen in the link I posted. –  Christian Basar Nov 10 '11 at 17:11
    
Hi @ChristianBasar. See my edit above. –  Nick Wiggill Nov 10 '11 at 18:27
1  
Hi @Nick Wiggill: I have just fixed the problem! :D I took your advice about creating the darts outside of a container. I still have it that a brand new playerDart is created whenever the createDart() function is called. However, I only allow this dart to be created; there can only be one dart in existence at a time. When the dart hits the ground, the playerDart is made null. Then a new dart can be created. The scrollScreen() function moves the playerDart like the other environmental objects in the game. Thanks so much for your help and advice! –  Christian Basar Nov 15 '11 at 21:51
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.