Flash Actionscript 3.0 Game Projectile Creation
- by Christian Basar
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);
}
}