A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: Shooting Bullets in Any Direction?

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    7

    Post Shooting Bullets in Any Direction?

    I need a code in AS2 that launches a bullet ("bullet") from the character ("hero") towards wherever you clicked the mouse.

    For example, if I click somewhere, I want the bullet to launch straight towards where I clicked. Thanks for any help!

  2. #2
    Senior Member
    Join Date
    Jun 2003
    Location
    Kent, WA
    Posts
    536
    How's your trig? You'll need it for this one.

    The basic idea goes like this...

    Code:
    // Get the difference between the hero and mouse
    var diff_x:Number = _xmouse - hero._x;
    var diff_y:Number = _ymouse - hero._y;
    
    // atan2 returns the radians (direction) of the provided x/y diffs
    var dir:Number = Math.atan2(diff_y, diff_x);
    
    // sin and cos will return the x/y components of the direction
    bullet.xdir = Math.cos(dir);
    bullet.ydir = Math.sin(dir);
    bullet.speed = 5;
    
    // Add those components * speed each frame
    bullet.onEnterFrame = function()
    {
       this._x += this.speed * this.xdir;
       this._y += this.speed * this.ydir;
    };
    Make sense?

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    7
    It makes sense, and thank you for the lesson, but two things don't work. Only one bullet can shoot at a time, and that bullet goes towards your mouse wherever it is on the screen. For example, the first click launches the bullet correctly, but when you click again it doesn't reset, it just changes direction.

    I need a code that can also duplicate the bullet and shoots a new one each time you click.

  4. #4
    Senior Member
    Join Date
    Jun 2003
    Location
    Kent, WA
    Posts
    536
    You'll need to set the bullet movieclip to "Export for Actionscript" in your library. Give it the export name of "bullet." Delete the bullet movieclip off the stage, because we'll be dynamically adding them as you click.

    Add this code before the code I gave you above:

    Code:
    static var uid:Number = 0;
    var bullet:MovieClip = attachMovie("bullet", "bullet"+uid, uid++);
    bullet._x = hero._x;
    bullet._y = hero._y;
    This will create a new instance of the bullet movieclip on the stage, and position it on top of the hero. The remaining code I gave you will then handle the movement of that particular instance.

    One important note... you will want to remove bullets if they go off stage. Just because you don't see them doesn't mean Flash isn't still running code for them.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    7

    Doesnt work?

    I don't know how to Export to Actionscript. I think I've seen it somewhere, but I just cant find it! Help? Flash MX 2004 or Adobe Flash CS5.

  6. #6
    Senior Member
    Join Date
    Jun 2003
    Location
    Kent, WA
    Posts
    536
    Find your Library window. (Ctrl+L if you don't see it.) Find the bullet movieclip listed there, and right-click on it, then select Properties. Check the box that says "Export for Actionscript".

  7. #7
    Junior Member
    Join Date
    Jun 2011
    Posts
    7

    Error...

    Code:
    **Error** Scene=Scene 1, layer=bg, frame=70:Line 3: Attribute used outside class.
         	static var uid:Number = 0;
    
    Total ActionScript Errors: 1 	 Reported Errors: 1
    The code:

    stop();
    _root.onMouseDown = function(){
    static var uid:Number = 0;
    var boolet:MovieClip = attachMovie("boolet", "boolet"+uid, uid++);
    boolet._x = pistolMC._x;
    boolet._y = pistolMC._y;
    var diff_x:Number = _xmouse - pistolMC._x;
    var diff_y:Number = _ymouse - pistolMC._y;
    var dir:Number = Math.atan2(diff_y,diff_x);
    boolet.xdir = Math.cos(dir);
    boolet.ydir = Math.sin(dir);
    boolet.speed = 50;
    boolet.onEnterFrame = function()
    {
    this._x += this.speed * this.xdir;
    this._y += this.speed * this.ydir;
    }
    }

  8. #8
    Senior Member
    Join Date
    Jun 2003
    Location
    Kent, WA
    Posts
    536
    Sorry, ya statics can only be used in certain circumstances. Move uid outside of the onMouseDown and remove the static.

    Code:
    stop();
    uid:Number = 0;
    _root.onMouseDown = function(){
        var boolet:MovieClip = attachMovie("boolet", "boolet"+uid, uid++);
        // etc...
    [/code]

  9. #9
    Junior Member
    Join Date
    Jun 2011
    Posts
    7
    Thank you so much for your help! It works now.

  10. #10
    Junior Member
    Join Date
    Jun 2011
    Posts
    7
    I have another problem. I need the bullets to hitTest the enemies, but it doesn't work because the uid number at the end.

    Here is my code:

    Code:
    var boolet:MovieClip = attachMovie("boolet", "boolet"+uid, uid++);
    //the rest of the shooting code
    _root.onEnterFrame = function(){
    if(worm1.hitTest(boolet)){
    //perform hittest operations here
    }
    }

  11. #11
    Senior Member
    Join Date
    Jun 2003
    Location
    Kent, WA
    Posts
    536
    So you're going to have to loop through a list (ie array) of objects and perform the collision tests against each one. You can do this two ways - you can either keep a list of enemies, and check that from each bullet, or you can use a list of bullets and check that from each enemy. You can do it either way, but logically speaking it makes sense for a bullet to check against enemies.

    As bullets (or enemies) are added and removed from the game, the list they are on will need to be updated. There are smarter ways to do collision detection (like using spatial partitioning to minimize collision tests), but this is the most basic.

    Try working out the code to do that on your own. If you still need help, let me know.
    Last edited by marshdabeachy; 07-07-2011 at 05:53 PM.

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