A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: Help! (On Guns and Dynamically Loading Projectiles)

  1. #1
    Title? Am I King or something?
    Join Date
    Jul 2009
    Location
    Ponyville, Happytown on the continent Nowhereland
    Posts
    26

    Question Help! (On Guns and Dynamically Loading Projectiles)

    Hey,

    I'm at a standstill right now. I'm trying to make an Asteroids remake (if you want to know more, I'll post it once it is in Alpha stage) and I already have a nicely-functioning spaceship-movement system; however, my weapons aren't working the way I want them to. I want them to:
    -Dynamically load when spacebar is pressed
    -Start at ship and fire immediately in ship's current direction
    -Have more than one projectile at a time firing without a projectile reset
    -Not follow the ship around if spacebar is held down

    Now I'm not a total newb at flash, and I already know how to dynamically load sprites onto the stage. However, the shots always reset if I press spacebar again and follow the ship around if I hold spacebar. I want it to fire rapidly, like a machine gun, but completely ignore what the ship or the other instances of shots are currently doing and just
    -start at the ship
    -move in the direction the ship was facing

    Can anybody help me at all?

    Fusiox
    Last edited by --Fusiox--; 10-27-2009 at 05:13 PM.

  2. #2
    Title? Am I King or something?
    Join Date
    Jul 2009
    Location
    Ponyville, Happytown on the continent Nowhereland
    Posts
    26
    Sorry, I forgot the whole "duplicateMovieClip" function. Now I can load as many instances of the shot as I want; however, I still have some problems.

    -The shots stop where they are every time I shoot a new bullet.
    -If I hold spacebar down, the shots don't move; the only shot that moves is the last shot created when I let go of the spacebar.

    Help me please!

    Fusiox

  3. #3
    formerly hooligan2001 :) .hooligan's Avatar
    Join Date
    Mar 2008
    Posts
    405
    I'm guessing that when ever you create a new movieclip your giving it the exact same instance name so it will only reference the newest created version of it by its name. Will probably have to see your bullet creation/movement code to be sure though.
    World Arcade | Facebook | Twitter - Follow MEEEE! or not......

  4. #4
    Senior Member Orkahm52's Avatar
    Join Date
    Jan 2006
    Location
    Perth, Australia
    Posts
    335
    I think hooligan's right. It's either that or the same depth. Instead of using duplicate movie, I'd simply use attachMovie for all the shots.

    To fix the depth/name issue, add an incremental number to both of them when you create each shot, like so:

    Code:
    var shots:Number = 0;
    if (shooting) {
      shots++;
      _root.attachMovie("shot", "shot"+shots, 10+shots);
    }
    Hope this helps

  5. #5
    Title? Am I King or something?
    Join Date
    Jul 2009
    Location
    Ponyville, Happytown on the continent Nowhereland
    Posts
    26
    Hello again. This is segmented parts of my code:

    Code:
    // at the beginning, when I set down some constants
    function init(){
    
      //normal initialization
      myShip.dx = 0;
      myShip.dy = 0;
      myShip.speed = 0;
      myShip.dir = 0;
      myShip.thrustSpeed = 0;
      myShip.gotoAndStop("still");
      lifeBar.gotoAndStop(1);
      
      bullet.dx = 0;
      bullet.dy = 0;
      bullet.speed = 0;
      bullet.dir = 0;
      bullet.thrustSpeed=0;
      bulletName = 0;
    } // end init
    
    // skipping many lines to my function checkKeys  
    
      if (Key.isDown(Key.SPACE)){
      bulletName += 1;
    	duplicateMovieClip("bullet", "bullet" + bulletName, bulletName)
    	bullet._x = myShip._x; 
    	bullet._y = myShip._y;
    	bullet.dir = myShip.dir;
    	bullet.thrustSpeed = 10;	
    	bullet.move(); // simply is "bullet._x += bullet.dx" and "bullet._y += bullet.dy"; dx and dy are defined in the below function "bullet.turn()"
    	bullet.turn(); // lots of stuff using basic trig
      } // end if
    } // end checkKeys
    
    bullet.turn = function(){
      this._rotation = this.dir;
    
      //get new thrust vector
      degrees = this.dir;
      degrees -= 90;
      radians = degrees * Math.PI / 180;
      thrustDX = this.thrustSpeed * Math.cos(radians);
      thrustDY = this.thrustSpeed * Math.sin(radians);
    
      //define thrust to dx and dy
      this.dx = thrustDX;
      this.dy = thrustDY; 
    
    } // end turn
    I have the "bullet" sprite placed off-screen (hard-coded for now). I don't get error messages but it just doesn't work. I can see that this code seems very wrong in some ways but I don't know how to fix it. It's my first time trying to implement rapid-fire weapons in a game

    If you don't get how thrustSpeed works it simply makes the sprite's rotation equal to var dir, and var dir equal to var degrees (-90), converts degrees to radians using trig, defines thrustDX and thrustDY using Math.cos(radians) and Math.sin(radians) multiplying with the sprite's thrustSpeed (in this case, multiplied by ten) and finally making the sprite's dx equal to thrustDX and sprite's dy equal to thrustDY. (When I add thrust, I change the "this.dx += thrustDX" and "this.dy += thrustDY", making it constantly add the thrustDX and thrustDY to sprite's dx and dy)

    (Yes I know that it is written in AS2 but I'm sure you can get the general drift. I have trouble learning new languages )

    And here is the game:asteroids.fla

    If you can help, I would greatly appreciate it.

    Fusiox
    Last edited by --Fusiox--; 10-29-2009 at 04:37 PM.

  6. #6
    formerly hooligan2001 :) .hooligan's Avatar
    Join Date
    Mar 2008
    Posts
    405
    You need to have bullet.move(); and bullet.turn(); run every frame. at the moment it only runs while your space key is down.

    PHP Code:
    if (Key.isDown(Key.SPACE)){

            
    bulletName += 1;
        
    duplicateMovieClip("bullet""bullet" bulletNamebulletName)
        [
    bullet+"bulletName"]._x myShip._x
        [
    bullet+"bulletName"]._y myShip._y;
        [
    bullet+"bulletName"].dir myShip.dir;
        [
    bullet+"bulletName"].thrustSpeed 10;

           [
    bullet+"bulletName"].onEnterFrame = function() {
               
             
    //place your movement and trig code here or external function associated with them

           
    }


      } 
    // end if 
    Also just realised your pointing the code to the original bullet movieclip not the duplicate.
    Last edited by .hooligan; 10-29-2009 at 08:10 PM.
    World Arcade | Facebook | Twitter - Follow MEEEE! or not......

  7. #7
    Title? Am I King or something?
    Join Date
    Jul 2009
    Location
    Ponyville, Happytown on the continent Nowhereland
    Posts
    26
    Err...

    Sorry to be such a noob, but I tried what you did. Using your code, errors occured and the computer said "Unexpected "." encountered" for practically every line using [bullet + "bulletName"] as the variable. What am I supposed to do? I'm sure it's something to do with the square brackets used as the variable...

    Sorry!
    Fusiox

  8. #8
    formerly hooligan2001 :) .hooligan's Avatar
    Join Date
    Mar 2008
    Posts
    405
    whoops should be something like ["bullet"+bulletName]._x = myShip._x; or _root["bullet"+bulletName]._x = myShip._x;
    World Arcade | Facebook | Twitter - Follow MEEEE! or not......

  9. #9
    Flash Intermediate XenElement's Avatar
    Join Date
    Sep 2008
    Location
    At my computer
    Posts
    196
    Okay, just one note, for bullets, you are definitely going to need arrays. The bullets don't all go the same direction right? so you need to have an array that holds ALL the directions for every bullet on screen.
    In the process of designing a quirky little game engine called gulp. Check out it's progress below: @ my blog.

    ---

    Check out my blog at XenElement.com

  10. #10
    Title? Am I King or something?
    Join Date
    Jul 2009
    Location
    Ponyville, Happytown on the continent Nowhereland
    Posts
    26
    Hey Xen,

    The problem with arrays or case lists is that I would need at least 36 different case breaks to implement it (at least if I use my direction-defining function). I don't want to do that much programming; plus doing so would be hard-coding, and everyone knows that you shouldn't hard-code unless there is no other way to code the function the way you want it without hardcoding :/

    Anyway's even if I created a case list, I would still have to dynamically program and change the current names of the bullet, so that wouldn't work.

    And the code still doesn't work >

    I'll try something else. Sorry for being a noob.

  11. #11
    Senior Member Orkahm52's Avatar
    Join Date
    Jan 2006
    Location
    Perth, Australia
    Posts
    335
    You wouldn't need to do any of that, just give each bullet Y speed and X speed variables. Set the speed when the bullet is created and then add its speed to its position every frame.

  12. #12
    Flash Intermediate XenElement's Avatar
    Join Date
    Sep 2008
    Location
    At my computer
    Posts
    196
    Well, see the problem is, you're trying to keep your code, when there's a better way. @Orkahm52: I'm assuming that he/she plans to have multiple bullets. If so, he also probably wants them to not all go the same direction. This means he needs an array to keep track of all the directions.

    @Fuision:

    You have an entire function that keeps updating the bullets direction. Why? A bullet keeps going in the same direction forever doesn't it?


    [EDIT] Dunno why I didn't see this before, but you are coding from AS2 right? I was looking at it from an AS3 standpoint. Sorry. Although, the concept is pretty much the same.
    In the process of designing a quirky little game engine called gulp. Check out it's progress below: @ my blog.

    ---

    Check out my blog at XenElement.com

  13. #13
    Senior Member Orkahm52's Avatar
    Join Date
    Jan 2006
    Location
    Perth, Australia
    Posts
    335
    XenElement, if you store the xSpeed and ySpeed in each individual bullet, there would be no need for an array, right? When a bullet is fired it's xSpeed and ySpeed would be set based on it's rotation.

    As far as I can see, that turn() function is only called for each new bullet, when space is pressed, rather than every frame. I might be wrong, however, cause Flash 8 is unable open the fla.

  14. #14
    Title? Am I King or something?
    Join Date
    Jul 2009
    Location
    Ponyville, Happytown on the continent Nowhereland
    Posts
    26
    Oh sorry about that. I'm using Flash CS4, forgot to save it in compatibility mode =/

    If I didn't make myself clear before, what I'm trying to do in my current (or previous) code is to change the name of each bullet every time it is fired. So the new code basically is targeted to the new bullet that is fired, since every time a bullet is duplicated and fired I add 1 to the var bulletName. The main two reasons that I think the code is not working is:
    #A syntax error. I'm sure there's a way to actually be able to add a var and an instance name to create an entirely new entity, but I feel that I'm either typing it wrong or I need to create a var which equals the instance bullet plus the var bulletName. I'll get on it.
    #Every time the code updates, the previous bullet is no longer valid for the code. In effect, whenever a new bullet appears the old bullet is dropped and left there while the code targets the new bullet, kind of like how giving a little kid a new toy most likely will make him drop his old one, forget about it, and play with the new toy. In this case, I need to create code that tells the computer to keep moving the old bullet even though the main code is targeted at the most recent instance of bullet. And that is what I think you mean when you tell me to create an array.

    I can't resave my .fla because I currently don't have access to the computer with Flash CS4 (it's at home) but I'll try to fix this and post a resaved copy some time in the next few days.

    Thanks for all your help!
    -Fusiox

  15. #15
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    I don't understand why this is so hard?

    Source: simpleAsteroids.fla

    Demo: Simple Asteroids in 10 Minutes - ImprisonedPride
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  16. #16
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    I remember when I first tried to do something like this...My answers:
    1) ARRAYS
    2) attachMovieClip();

    Do what XenElement said. And yes, you can just put an onEnterFrame on each bullet, but I think that using arrays is much cleaner and more organized.

    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  17. #17
    Title? Am I King or something?
    Join Date
    Jul 2009
    Location
    Ponyville, Happytown on the continent Nowhereland
    Posts
    26
    Resaved!

    Unfortunately I can't save in a Flash 8 version, but I can save it in a cs3 format. Here it is!

    asteroids(2) cs3.fla

    asteroids cs3.fla

    The top one is my newest edited version, the bottom is the code before I started the thread.

    THanks for all your help (again!)
    -Fusiox

  18. #18
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    http://www.mediafire.com/?ntmz5nwhamn

    well first of all, learn to atleast format your code properly.....70% of the errors were from misplaced brackets.
    Next, you have to tell the functions whom to operate on. If you use _root["bullet"+bulletname] everywhere, the code will execute for that one alone, that is the current/last value of bulletname. also if you call a function you have to keep it's scope/visibility in mind..... you can't run bullet1.move() when move() exists in the parent timeline.

    I have fixed your code, and saved it with cs4.
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

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