A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: how to make bullets explode when they hit an enemy/object?

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Posts
    11

    how to make bullets explode when they hit an enemy/object?

    Hello, having issues with this.. I'll try to explain best I can.

    I just need bullets to display an explosion effect when hitting an object/enemy. Right now they are just disappearing with removeMovieClip();.

    Nothing happens when I tell the bullet to go to the next frame to display the explosion effect in its own movieclip, and I can't figure out how to create the effect and have it attach to the bullets last location when it hit something on the last stage.

    Really need help!

  2. #2
    Junior Member
    Join Date
    Feb 2022
    Posts
    27
    you need to use attachMovie, and set attached movieClip coordinates as your bullet movieclip

  3. #3
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Ok the bullets array will store each bullet attached after pressing the shoot button, so each bullet will move forward +=20 x coordinate.

    When a bullet from the bullets array hits the enemy, the mc will be moved to the exploding_bullets array & spliced out from bullets array.

    If an mc in the exploding_bullets array reaches its final frame it will be removed from the exploding_bullets array & from the stage.

    You can add more robot movieclips that get affected by bullets to the robots array.

    PHP Code:
    var bullets=[]
    var 
    exploding_bullets=[]
    var 
    index=-1;
    var 
    stageWidth=550;
    shoot_btn.onPress=function(){
    index++
    attachMovie("bullet","bullet"+index,index);
    bullets.push(_root["bullet"+index]);
    _root["bullet"+index]._x=robot_1._x+robot_1._width+20
    _root
    ["bullet"+index]._y=robot_1._y+robot_1._height/2
    _root
    ["bullet"+index].gotoAndStop(1);
    trace("adding bullet " index);
    }


    var 
    robots=[robot_2]
    onEnterFrame=function(){
    for(var 
    j=0;j<robots.length;j++){
    for(var 
    i=0;i<bullets.length;i++){
    bullets[i]._x+=20
    if(bullets[i]._x>stageWidth+bullets[i]._width*3||bullets[i]._x<0-bullets[i]._width){
    removeMovieClip(bullets[i]);
    bullets.splice(i,1)
    trace("removed bullet out of stage range");
    }
    if(
    robots[j].hitTest(bullets[i])){
    exploding_bullets.push(bullets[i]);
    bullets.splice(i,1)
    exploding_bullets[exploding_bullets.length-1].gotoAndPlay(2);
    trace("exploding bullet " i);
    }
    }
    }
    for(var 
    h=0;h<exploding_bullets.length;h++){
    if(
    exploding_bullets[h]._currentframe==exploding_bullets[h]._totalFrames){
    removeMovieClip(exploding_bullets[h]);
    exploding_bullets.splice(h,1);
    trace("removing exploded bullet " h);
    }
    }

    download 0.1

  4. #4
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Quote Originally Posted by Alex_Cherkasoff View Post
    you need to use attachMovie, and set attached movieClip coordinates as your bullet movieclip
    I tried this. It doesn't seem to work because the bullets themselves are generated movieclips, all with unique names. Would you be able to throw some quick code my way doing it this way? Seems like the most

  5. #5
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    I can try this way but idk if it will work, the game is going to have hundreds of enemies, and I want the bullets to explode when hitting the ground also.

  6. #6
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Ok I added some angled shooting so that you can see the wall gets hit, I just added the wall to the begining of the robots array.

    The angled function compares the _xmouse & _ymouse coordinates to the position of your robot which is called bullet_start_x & bullet_start_y, if you plan to update the position of the your robot you must repeat lines 7 & 8 to update them without var written.

    When you do bg.onPress by clicking the stage, it stores the angle it the bullet should be at in the bullets_x & bullets_y arrays .

    bg movieclip was locked & set as a separate layer which is the blue background.

    PHP Code:
    var bullets=[]
    var 
    bullets_x=[]
    var 
    bullets_y=[]
    var 
    exploding_bullets=[]
    var 
    index=-1;

    var 
    bullet_start_x=robot_1._x+robot_1._width+20;
    var 
    bullet_start_y=robot_1._y+robot_1._height/2
    var mouse_down=false

    bg
    .onPress=function(){
    degrees=-angled(_root._xmouse,_root._ymouse);
    var 
    angle=degrees*(Math.PI/180);

    var 
    xposition=40Math.sin(angle);
    var 
    yposition=40*Math.cos(angle);

    index++
    attachMovie("bullet","bullet"+index,index);
    bullets.push(_root["bullet"+index]);
    bullets_x.push(xposition)
    bullets_y.push(yposition)
    _root["bullet"+index]._x=bullet_start_x
    _root
    ["bullet"+index]._y=bullet_start_y
    _root
    ["bullet"+index].gotoAndStop(1);
    trace("adding bullet " index);
    _root["bullet"+index]._rotation=degrees
    }


    var 
    robots=[wall,robot_2]
    onEnterFrame=function(){

    for(var 
    j=0;j<robots.length;j++){
    for(var 
    i=0;i<bullets.length;i++){
    bullets[i]._x+=bullets_y[i]
    bullets[i]._y+=bullets_x[i]

    if(
    bullets[i]._x>width||bullets[i]._x<0||bullets[i]._y>height||bullets[i]._y<0){
    removeMovieClip(bullets[i]);
    bullets.splice(i,1)
    bullets_x.splice(i,1)
    bullets_y.splice(i,1)
    trace("removed bullet out of stage range");
    }
    if(
    robots[j].hitTest(bullets[i])){
    exploding_bullets.push(bullets[i]);
    trace("exploding bullet " bullets[i]);
    bullets.splice(i,1)
    bullets_x.splice(i,1)
    bullets_y.splice(i,1)
    exploding_bullets[exploding_bullets.length-1].gotoAndPlay(2);
    }
    }
    }
    for(var 
    h=0;h<exploding_bullets.length;h++){
    if(
    exploding_bullets[h]._currentframe==exploding_bullets[h]._totalFrames){
    trace("removing exploded bullet " exploding_bullets[h]);
    removeMovieClip(exploding_bullets[h]);
    exploding_bullets.splice(h,1);

    }
    }
    }

    function 
    angled(mousex,mousey) {
    var 
    r=Math.atan2(bullet_start_x-_root._xmouse,bullet_start_y-_root._ymouse);
    return(
    r*180/Math.PI+90)-Math.floor((r*180 /Math.PI+90)/360)*360

    download 0.2
    Last edited by AS3.0; 09-19-2022 at 02:01 AM.

  7. #7

  8. #8
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Unfortunately this method isn't going to work for me I think, because of the way I have it programmed (the hitTest code is not all on the main timeline, an is nested in the bullet mc).

    I'm able to make the bullet go to an 'explode' frame and play an explosion animation there, but because the bullet is moving from code on the main timeline, the explosion keeps traveling.

    There has to be a really simple way to do this!

  9. #9
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    I will literally pay someone to get this solved at this point!

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