[SOLVED] Creating Bullets with an Array


(Christian Smithroat) #1

Need help! I’m new to flashpunk and as3. I’m going through the tutorial and I just finished “Keyboard & Mouse Input”. Before I move on, I want to get the spaceship to fire bullets. Already made a Bullet class that extends Entity. I got the bullet picture spawning at my ships location when I fire, but the bullet will not travel up the screen. Also, I’m having trouble with arrays in As3. I made the length 10, but when I fire I can spawn as many bullets as many times I press the fire button. Please help! I’m familiar with Java, but as3 syntax and updating an object’s location in as3 is really messing me up.

Thanks in advance!

Image links are below.

Gameplay:

*http://imgur.com/z7p70oh

what is shown when the fire button is pressed. the ship can move left and right

Main class:

*http://i.imgur.com/nUyfVSN.png?1

MyWorld class:

*http://i.imgur.com/mFwWJn1.png

Player class (next 2 images):

*http://i.imgur.com/gbDnUmm.png part 1

*http://i.imgur.com/nTPn5tm.png part 2

Bullet class:

*http://i.imgur.com/J8KXuET.png


(Justin Wolf) #2

Greetings, Christian. For future reference, copy and paste your code so it’s easier for us to access and read, thus making it easier for us to help you! :smile:

I can already spot your problem from your shoot(); function. In FlashPunk, every Entity has an update function that you can override from classes extending said Entity (like your Bullet class currently is doing). Right now, when you are creating your bullets, you’re calling move(); on the first frame that they are created instead of every frame of their existence.

To solve this, open your Bullet.as class and add the following code:

override function update():void
{
     move();
     super.update();
}

This will in turn call the move(); function every frame of the Bullet’s existence (unless you toggle the active property of the Bullet to false, in which case the update function does not get called).


(Christian Smithroat) #4

It works! Thank you so much! <3 <3 <3

Can you explain to me why my array is not working properly? The size of the bullet array is just suppose to be 10, but I am still able to fire a bullet each time i press the fire button. In Java, after I fire 10 bullets I would not be able to fire any more untill i reset all the pointers to null and create the bullet objects again.


(JP Mortiboys) #5

You don’t actually need to create an array at all - calling world.add() will create a persistent reference to the bullet object, so you don’t need to hold a reference around. You can simply do this:

public class Player extends Entity {
  // ...

  public function shoot():void {
    var bullet:Bullet = new Bullet();
    bullet.x = x + 18;
    bullet.y = y;
    world.add(bullet);
  }
}

You’d only need a loop if you wanted to create more than one bullet at a time, like a spread shot or something.


(JP Mortiboys) #6

Also, looking at your shoot code, I don’t think it does what you expect - it looks like it’s literally creating 10 (identical) bullets on every shot. It’s storing the references to the bullet objects in the local array, but they’re just being overwritten each time you fire (not a problem in FP since the player doesn’t “own” the bullets, the world does).

If you want to limit the number of bullets you can fire, you just need to store the total number of active bullets somewhere, and update it when a bullet is created or destroyed. You could use the world’s classCount or typeCount functions to do this, assuming the bullets only belong to the one player - if you wanted multiplayer or drones then you’d have to store it at player level.


(Christian Smithroat) #8

Sweet, man thanks! I’m not use to this library yet. I still can’t wrap my head around that the work load is being done for me. I want to store all this data somewhere like its Java. Can you point me to some links to where i can get more comfortable with AS3 and FP? The syntax in this language is funky looking.


(Zachary Lewis) #9

Learning ActionScript 3.0

I learned a ton from this book.