A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: [RESOLVED] Bullets in as3

  1. #1
    Member
    Join Date
    Oct 2009
    Location
    Scotland
    Posts
    32

    resolved [RESOLVED] Bullets in as3

    I was wondering if anyone knew how to create bullets in as3?

    I have a main character = hero.mc

    Basically I need to store the bullets in an array and have them firing from the hero. It sounds simple enough, but for some reason I can't get it to work.

    So far I have this:

    private var bullets:Array;

    public function createBullets() {
    bullets = new Array();
    var bullet = new Object();
    bullet.startx = hero.mc.x;
    bullet.starty = hero.mc.y;
    bullet.width = 10.0;
    bullet.height = 10.0;
    bullet.moveRight = true;
    bullet.walkSpeed = .3;
    }


    I have have a keyboard function that runs createBullets() when the spacebar is pressed:


    public function keyDownFunction(event:KeyboardEvent) {
    if (gameMode != "play") return;

    if (event.keyCode == 37) {
    hero.moveLeft = true;
    } else if (event.keyCode == 39) {
    hero.moveRight = true;
    } else if (event.keyCode == 32) {
    createBullets();
    } else if (event.keyCode == 38) {
    if (!hero.inAir) {
    hero.jump = true;
    }
    }
    }

    Any help would be greatly appreciated
    -------------------------------------------------
    Don't F**K with Fedor

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You have not told us what the difficulty you are having is. "It doesn't work" tells us absolutely nothing.

    You are re-creating a new bullets array every time you run createBullets. And you're never putting your new bullet into the bullets array.

  3. #3
    Member
    Join Date
    Oct 2009
    Location
    Scotland
    Posts
    32
    Ok, I probably should have posted this in the noob thread.

    Anyways, the movie clip isn't being created at all. I've since pushed the bullet into the array and added "stage.addChild(bullet);" but still no luck.

    Thanks anyway.
    -------------------------------------------------
    Don't F**K with Fedor

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Are you sure that createBullets is getting called?

    Also, don't add to the stage. You want to add to either a new container just for bullets, or to an existing container. Since you are setting the x and y from hero.mc, you can add the new bullet to hero to get it in the right position. Otherwise, you should use localToGlobal and globalToLocal to transform the mc position to the local coordinates of whatever parent you end up adding it to.

  5. #5
    Member
    Join Date
    Oct 2009
    Location
    Scotland
    Posts
    32
    Argh... I give up.

    Yep it's definitely calling the createBullets function, I've got no idea what's wrong though. I've tired various different methods such as creating a new bullet container as you suggested but nothing seems to be working. The bullet movieclip just isn't being created.

    Ah well, nevermind

    Thanks for the help
    -------------------------------------------------
    Don't F**K with Fedor

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It is being created. But you are creating a new bare object, which is not a DisplayObject. you did not mention that you got an error when trying to add it to the stage. Errors tell you what is wrong! read them! post them if you are trying to get help!

    Do you have a bullet clip in the library? If you do, you will need to export it for actionscript and call it Bullet. And instead of
    Code:
    new Object();
    you will need
    Code:
    new Bullet();
    Ideally, you would also define a Bullet.as class which declares all those properties you set and does stuff with them.

    If you do not have a bullet clip in the library, what did you expect a bullet to look like, and how did you expect flash to know?

  7. #7
    Member
    Join Date
    Oct 2009
    Location
    Scotland
    Posts
    32
    Apologies for being vague. The reason that I didn't mention anything about an error message was because there wasn't one, and yes, I do have a movieclip for the bullet and have exported it for actionscript.

    The mistake that I was making was indeed the fact that I was creating a new object. It seems to be working fine now, thanks.

    Do you happen to know of anyway to get this movieclip to move continuously across the stage?

    I've tried adding bullet.x++; to the createBullet function but that seems to do nothing.

    Thanks again.
    -------------------------------------------------
    Don't F**K with Fedor

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It doesn't do nothing. It moves the new Bullet one pixel to the right. But it only does it once. If you want it to happen each frame, then you'll have to make an event listener for Event.ENTER_FRAME which does that.

  9. #9
    Member
    Join Date
    Oct 2009
    Location
    Scotland
    Posts
    32
    Awesome stuff, I've managed to get it working fine now!

    Sorry to keep bugging you, but I'm now having a little trouble with my hitTests.

    I've managed to figure out how to perform hitTests with movieclips that are already on the stage.

    In the createHero() function, I added the line:

    hero.mc = level.hero;
    ... so that the hero movieclip embedded inside the level movieclip can be referenced using hero.mc which will make it easier for me to manipulate the character later.

    This hitTest works perfectly:

    if (hero.mc.hitTestObject(viruses[i].mc)) {
    somefunction();
    But I am having trouble with doing the same thing for the bullet seeing as it is only being added to the stage when the createBullet function is being called (spacebar pressed).

    Seeing as I am unable to give the bullet an instance name, I assumed this would suffice:

    if (Bullet.hitTestObject(viruses[i].mc)) {
    virusDie(i);
    However I get the error message:

    Call to a possibly undefined method hitTestObject through a reference with static type Class.

    Any thoughts?
    -------------------------------------------------
    Don't F**K with Fedor

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Well, Bullet is a Class. I think you want to see whether any of your bullets are hitting any of your viruses. There are various strategies for optimizing the collision checking, but the basic strategy is to check each bullet against each virus. To do that, you will need to have all your bullets in an array. You did have a bullets array, but I haven't seen your latest code. If you are putting each Bullet into the bullets array as you create them, then you can iterate over that array.

    Code:
    for (var j:int = 0; j < bullets.length; j++){
      if (bullets[j].hitTestObject(viruses[i].mc)){
        virusDie(i);
      }
    }
    Don't forget to remove the bullet from the bullets array when you remove it from the display.

  11. #11
    Member
    Join Date
    Oct 2009
    Location
    Scotland
    Posts
    32
    Ok, well I'm feeling pretty stupid right about now.

    The bullets are indeed being added to the bullets array, along with having an array for the viruses. I added your code so my function now looks like this:


    public function checkVirusCollisions() {

    var i:int=viruses.length-1;i>=0;i--


    for(var j:int = 0; j < bulletArray.length; j++){
    if (bulletArray[j].hitTestObject(viruses[i].mc)) {
    virusDie(i);
    bulletDie(j);
    }

    // lose health
    if (hero.mc.hitTestObject(viruses[i].mc)) {

    heroLosesHealth();
    }
    }
    }
    The problem hasn't really been solved, the virusDie and bulletDie functions aren't being called.

    I get no error message, but removing var i:int=viruses.length-1;i>=0;i-- from the for loop seems to have also prevented the heroLosesHealth function from being called.
    -------------------------------------------------
    Don't F**K with Fedor

  12. #12
    Member
    Join Date
    Oct 2009
    Location
    Scotland
    Posts
    32
    UPDATE:


    public function checkVirusCollisions() {
    // viruses

    var i:int = ("virus"+1);

    for(var j:int = 0; j < bulletArray.length; j++){
    if (bulletArray[j].hitTestObject(viruses[i].mc)) {
    virusDie(i);
    bulletDie(j);
    }



    // hero jumps on virus
    if (hero.mc.hitTestObject(viruses[i].mc)) {

    heroLosesHealth();
    }
    }
    }
    var i:int = ("virus"+1); seems to have done the trick. The viruses are being removed upon impact with the bullet.

    However the heroLosesHealth function still isn't being called for some reason.
    -------------------------------------------------
    Don't F**K with Fedor

  13. #13
    Member
    Join Date
    Oct 2009
    Location
    Scotland
    Posts
    32
    UPDATE 2:

    I've managed to get the heroLosesHealth function to work.

    Cheers for all the help Sir
    -------------------------------------------------
    Don't F**K with Fedor

  14. #14
    Member
    Join Date
    Oct 2009
    Location
    Scotland
    Posts
    32
    Argh... please don't hit me.

    I've discovered that the heroLosesHealth function only works for the next virus in the array.

    I have the viruses already added to the stage with instance names virus1, virus2, etc..

    The heroLosesHealth function only works for the next virus in the game and not the others. However if I kill the first virus, then the function will work for the second virus and so on.

    I've tried changing var i:int = ("viruses"+1); to var i:int = ("viruses"++); but I then get the error message:

    1106: Operand of increment must be a reference.
    -------------------------------------------------
    Don't F**K with Fedor

  15. #15
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    if viruses is really an array, then i should go from 0 to viruses.length-1. The easy way to do that is
    Code:
    for (var i:int = 0; i < viruses.length; i++){
    Given what you've said, I don't think viruses is really an array like that. Fix that.

  16. #16
    Member
    Join Date
    Oct 2009
    Location
    Scotland
    Posts
    32
    Muchas Gracias, all sorted.
    -------------------------------------------------
    Don't F**K with Fedor

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center