A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: ActionScript code for how to make enemies shoot?

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

    Question ActionScript code for how to make enemies shoot?

    Hi there. This is my first post and and it's an inquiry as to how to make enemies shoot bullets in AS2. I already have the main character shooting bullets, but for some reason can't get enemies to shoot them.

    The enemies are all generated using arrays and so are the bullets. So each enemy and bullet is a copy of the same movie clip.

    The problem is whenever there's more than ONE copy of the enemy on stage, the bullets only generate on that copy unless it's destroyed. Then they appear on the next copy, etc. etc.

    Also the collision detection only works for some of the bullets.

    I'm not great at programming, so all the code in my game is Frankensteined together from dead tutorial sites I've found online, and so I'm also hoping this is a simple thing to do.

    TL;DR would really appreciate if someone could just throw me some code so my enemies can shoot projectiles at the character.

  2. #2
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Well there is multiple ways to achieve this based on perspective and how the game is already being built. You should have the file open source for the time being and share the source file and later close the source again if this is a non-open source project.
    Last edited by AS3.0; 09-23-2021 at 05:36 PM.

  3. #3
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Weaver did you attach the source?

  4. #4
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Thank you for replying, and sorry for the late response. It is a non-open source project. If possible, I'd like to do my best and share the code without linking the full file but will if there's no other option. Yeah, making this harder than it has to be, sorry.

    I'll try to explain it all here.

    The enemies are generated using an array in a frame in the main timeline:

    var flyArray:Array = [];

    Then this function accompanies it on the same frame:

    function createFly() {
    currentFly = _root.attachMovie("boltflyer", "fly"+flyArray.length, _root.getNextHighestDepth());
    currentFly._x = 420;
    currentFly._y = 25;
    if(currentFly._y>150){
    currentFly._y=149
    }
    flyArray.push(currentFly);
    }

    When the main character hits an empty trigger movieclip it triggers the "currentFly" function and generates an enemy. When the main character hits multiple instances of these empty MC's it generates the same enemies using the "currentFly" function.

    On the same frame there is this code that generates the bullets, which also has its own array variable, named "ESArray":

    function createES() {
    currentES++;
    currentES = _root.attachMovie("enemyshot", "es1"+ESArray.length, _root.getNextHighestDepth());
    currentES._x = _root["boltflyer"+fly]._x;
    currentES._y = _root["boltflyer"+fly]._y;
    ESArray.push(currentES);
    }


    Then, inside the enemy movie clips, there is code that looks like this:

    stop();
    var timer = 77;

    onEnterFrame=function(){
    timer--;
    if (timer <= 0) {
    _root.createES();
    timer = 77;
    }
    }


    This code generates the bullet by calling the function defined in the timeline.

    And then, finally, on the bullet movie clip itself, there is this line of code that is supposed to attach the bullet movie clip copies to the enemy movie clip copies:

    for (f=0; f<_root.flyArray.length; f++) {
    this._x = _root["fly"+f]._x;
    this._y = _root["fly"+f]._y;
    }


    So the issue is it only generates the bullet on one of the enemies. Let's say there's five enemies on screen, only one will have a bullet shooting from it. If that enemy that's shooting the bullet dies, then the bullets switch over to the next enemy movie clip. This continues until all of the enemies are dead.
    Last edited by xelaeyks; 01-30-2022 at 07:10 PM.

  5. #5
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Can anyone help me?

  6. #6
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Nobody can help? There's thousands of views for this thread, just need some direction here.

  7. #7
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    On the function createES() where you put, _root["boltflyer"+fly]._x;
    I believe it should be addressed as the name: _root["fly"+flyArray.length]._x;

    because in function createFly(), boltflyer is only the linkage name from the library and the addressable name is fly+number for function down below

    PHP Code:
    function createES() {
    currentES++;
    currentES _root.attachMovie("enemyshot""es1"+ESArray.length_root.getNextHighestDepth());
    currentES._x _root["boltflyer"+fly]._x;
    currentES._y _root["boltflyer"+fly]._y;
    ESArray.push(currentES);

    ^ change to V

    PHP Code:
    function createES() {
    currentES++;
    currentES _root.attachMovie("enemyshot""es1"+ESArray.length_root.getNextHighestDepth());
    currentES._x _root["fly"+flyArray.length]._x;
    currentES._y _root["fly"+flyArray.length]._y;
    ESArray.push(currentES);

    Next, If you are creating a bullet using createES() right when the enemy is attached, it will work for the latest enemy that gets dropped each time, but if you are calling createES() at random times for enemies that didn't have bullets, you will have to modify the createES() function to attach the bullet to the enemy you want.

    Or if createES() is only attaching one bullet you can name it "es1"+currentES so that it gets a new unique name.

    PHP Code:
    function createES() {
    currentES++;
    currentES _root.attachMovie("enemyshot""es1"+currentES_root.getNextHighestDepth());
    currentES._x _root["boltflyer"+fly]._x;
    currentES._y _root["boltflyer"+fly]._y;
    ESArray.push(currentES);

    Last edited by AS3.0; 02-02-2022 at 04:59 AM.

  8. #8
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    PHP Code:
    function createES() {
    currentES++;
    currentES _root.attachMovie("enemyshot""es1"+currentES_root.getNextHighestDepth());
    currentES._x _root["fly"+flyArray.length]._x;
    currentES._y _root["fly"+flyArray.length]._y;
    ESArray.push(currentES);


  9. #9
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Thank you for the reply, unfortunately that didn't seem to work.

    For some reason it doesn't attach the bullets through the createES function, I can remove this:
    currentES._x = _root["fly"+flyArray.length]._x;
    currentES._y = _root["fly"+flyArray.length]._y;


    and nothing will change...

    It only attaches to the enemy through this code nested inside the bullet movieclip itself:


    for (f=0; f<_root.flyArray.length; f++) {
    this._x = _root["fly"+f]._x;
    this._y = _root["fly"+f]._y;
    }

  10. #10
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Well to replace the code nested inside the bullet you can maybe pick a random enemy to give the bullet to each time, what you are doing in the loop code thats nested in the bullet is going through the whole loop and at the end giving it to the last enemy in the array list.
    PHP Code:
    var mc=_root.flyArray[randomBetween(0,_root.flyArray.length-1)]

    this._x mc._x;
    this._y mc._y;


    function 
    randomBetween(a,b){
    return(
    Math.floor(Math.random()*(b-a+1))+a);


    And wow I never stopped to realize how many people view these threads huh. HAHAH, maybe people on social media feel that way too & we say woah thats alot of followers & they can say woah I didn't notice that, perhaps
    Last edited by AS3.0; 02-02-2022 at 11:07 AM.

  11. #11
    Junior Member
    Join Date
    Sep 2021
    Posts
    11
    Awesome! That worked! They're all shooting bullets now. I would have never figured that out. Thanks for the help as this was really putting a stop on developing the game in general.

    And yeah it's surprising to see thousands of views for ancient Actionscript 2.0 threads.

  12. #12
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    Yeah now I feel like im standing on the edge of the worlds tallest building thank you xD.

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