A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: [RESOLVED] Enemy Spawn

  1. #1

    resolved [RESOLVED] Enemy Spawn

    It's me again. I'm making a game like Gnat Attack from Mario Paint except you hit fairies instead of flies

    The problem is that I got the code to make flash spawn multiple fairies but it keeps overwriting them because they're at the same depth. So I used _root.getNextHighestDeph and all that did was break the spawn and have one fairy teleport to random places while another spawns on the top left unable to be affected by the hit check triggered by the fly swatter

    Code:
    setInterval(SpawnFairy, 2000);
    
    function SpawnFairy() {
    	
    	attachMovie("Fairy", "Fairy", _root.getNextHighestDepth());
    	Fairy._x = random(1024);
    	Fairy._y = random(768); 
    }
    This is the code I have down right now. Is there anything I'm doing wrong?

  2. #2
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,227
    PHP Code:
    setInterval(SpawnFairy2000);
    var 
    depth_num=0;
    var 
    fairy_container=[]
    function 
    SpawnFairy() {
        
        
    _root.attachMovie("fairy""fairy"+depth_numdepth_num);
        
    _root["fairy"+depth_num]._x random(1024);
        
    _root["fairy"+depth_num]._y random(768); 
         
    fairy_container.push(_root["fairy"+depth_num])//add fairy to the next array spot of fairy_container
        
    depth_num++//increase number
        
        //extra commands begin
            
    for(var i=0;i<fairy_container.length;i++){//loop through each fairy 
        
    fairy_container[i]._x+=5//move x of each fairy in container
        
    fairy_container[i]._y+=5//move y of each fairy in container
        
    }    
        
    //extra commands end
        

    Its cheesasaurous rex

  3. #3
    So, it's working now but the hit test I put in anytime the fly swatter hits a fairy doesn't work.

    The hit test is in it's own function that's called when you press the "P" Key which spawns in a hitbox "hit" to collide with the fairy. If the collision is true it calls for the Fairy movie to despawn and attach a death animation in it's place

    PHP Code:
    function kill() {
        if (
    hit.hitTest(Fairy)) {
            
    _root.score += 3;
            
    Swatter.gotoAndPlay("2");
            
    Fairy.gotoAndPlay("13");
            
    unloadMovie(hit);
            
    unloadMovie(Fairy);
            
    attachMovie("Fairy_death""Fairy_death"6);
            
    Fairy_death._x Fairy._x;
            
    Fairy_death._y Fairy._y;
        } } 
    should I replace "Fairy" with "_root["Fairy"+depth_num]"?

  4. #4
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,227
    The depth_num was increased after creating the last fairy so it wouldn't be accessed like that because you are calling fairy1 when the one created was called fairy0, a good option is to go through each fairy stored in the container array called fairy_container 1 by 1 like fairy_container[0], fairy_container[1] ... in the for loop & check if one of them is touching the hit movieclip.

    PHP Code:
    setInterval(SpawnFairy2000);
    var 
    depth_num=0;
    var 
    fairy_container=[]
    var 
    fairy_death_container=[]
    function 
    SpawnFairy() {
     
     
    _root.attachMovie("fairy""fairy"+depth_numdepth_num);
     
    _root["fairy"+depth_num]._x random(1024);
     
    _root["fairy"+depth_num]._y random(768); 
    fairy_container.push(_root["fairy"+depth_num])//add fairy to the next array spot of fairy_container
     
    depth_num++//increase number
     
     //extra commands begin
    for(var i=0;i<fairy_container.length;i++){//loop through each fairy 
     
    fairy_container[i]._x+=5//move x of each fairy in container
     
    fairy_container[i]._y+=5//move y of each fairy in container
     

     
    //extra commands end





    function 
    kill() {
    // hit._x=_root._xmouse
    // hit._y=_root._ymouse
    for(var i=0;i<fairy_container.length;i++){//loop through each fairy  fairy_container[0] , fairy_container[1] etc...
    if (hit.hitTestfairy_container[i])) {// if the current fairy being checked in the loop is touching hit
    _root.score += 3;
    Swatter.gotoAndPlay("2");
    fairy_container[i].gotoAndPlay("13");//current fairy in the loop that hit, goto frame 13
    _root.attachMovie("Fairy_death""Fairy_death"+depth_numdepth_num);//attach depth fairy
    fairy_death_container.push(_root["Fairy_death"+depth_num])//add death fairy to fairy_death_container array to be accessed as fairy_death_container[0] , fairy_death_container[1] etc...

    _root["Fairy_death"+depth_num]._x fairy_container[i]._x;//before removing the old fairy, give the fairy death the same coordinates as the old fairy
    _root["Fairy_death"+depth_num]._y fairy_container[i]._y;//before removing the old fairy, give the fairy death the same coordinates as the old fairy

    unloadMovie(hit);//unload the hit object during hit test with a fairy
    unloadMoviefairy_container[i]);//unload current fairy in the array position [i] that touched the hit movieclip 
     

     }
     
    depth_num++//increase the depth after you are done using anything with _root["Fairy_death"+depth_num] because the next call would be for a different name 

    Last edited by AS3.0; 10-21-2021 at 10:38 AM.
    Its cheesasaurous rex

  5. #5
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,227
    If you are going to have alot of fairys I added fairy_container.splice to save processing power.

    PHP Code:
    setInterval(SpawnFairy2000);
    var 
    depth_num=0;
    var 
    fairy_container=[]
    var 
    fairy_death_container=[]
    function 
    SpawnFairy() {
     
     
    _root.attachMovie("fairy""fairy"+depth_numdepth_num);
     
    _root["fairy"+depth_num]._x random(1024);
     
    _root["fairy"+depth_num]._y random(768); 
    fairy_container.push(_root["fairy"+depth_num])//add fairy to the next array spot of fairy_container
     
    depth_num++//increase number
     
     //extra commands begin
    for(var i=0;i<fairy_container.length;i++){//loop through each fairy 
     
    fairy_container[i]._x+=5//move x of each fairy in container
     
    fairy_container[i]._y+=5//move y of each fairy in container
     

     
    //extra commands end





    function 
    kill() {
    // hit._x=_root._xmouse
    // hit._y=_root._ymouse
    for(var i=0;i<fairy_container.length;i++){//loop through each fairy  fairy_container[0] , fairy_container[1] etc...
    if (hit.hitTestfairy_container[i])) {// if the current fairy being checked in the loop is touching hit
    _root.score += 3;
    Swatter.gotoAndPlay("2");
    fairy_container[i].gotoAndPlay("13");//current fairy in the loop that hit, goto frame 13
    _root.attachMovie("Fairy_death""Fairy_death"+depth_numdepth_num);//attach depth fairy
    fairy_death_container.push(_root["Fairy_death"+depth_num])//add death fairy to fairy_death_container array to be accessed as fairy_death_container[0] , fairy_death_container[1] etc...

    _root["Fairy_death"+depth_num]._x fairy_container[i]._x;//before removing the old fairy, give the fairy death the same coordinates as the old fairy
    _root["Fairy_death"+depth_num]._y fairy_container[i]._y;//before removing the old fairy, give the fairy death the same coordinates as the old fairy

    unloadMovie(hit);//unload the hit object during hit test with a fairy
    unloadMoviefairy_container[i]);//unload current fairy in the array position [i] that touched the hit movieclip 
    fairy_container.splice(i,1);//remove fairy that was unloaded from its array position
     

     }
     
    depth_num++//increase the depth after you are done using anything with _root["Fairy_death"+depth_num] because the next call would be for a different name 

    Last edited by AS3.0; 10-21-2021 at 10:52 AM.
    Its cheesasaurous rex

  6. #6
    One last question, How do I perform a frame check for _root["Fairy_death"+depth_num]

  7. #7
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,227
    This is the line to put inside of the for() loop thats going through each fairy
    PHP Code:
    if( _root["Fairy_death"+depth_num]._currentframe==1){//if the fairy frame is at frame one...
    trace(true);//trace true in the output panel

    Hey would you be interested in downloading the AS3 game I just released on Android, I will be waiting for users in the lobby as a way to tend to users while its empty, I put a little ringtone for when someone comes in... xP thats what you will be doing later in multiplayer fairy land for Android hahaha: download
    Last edited by AS3.0; 11-14-2021 at 09:15 PM.
    Its cheesasaurous rex

  8. #8
    Still running into problems

    I have trace actions set to on yet nothing is happening when I trigger the death animation on the fairies. I thinking it's something with an event listener or an onEnterFrame() function

    I have death(); which will call the function for the death animation. Mostly to make programming easier.

    PHP Code:
    setInterval(SpawnFairy2000);
    var 
    depth_num=0;
    var 
    fairy_container=[]
    var 
    fairy_death_container=[]
    function 
    SpawnFairy() {
     
     
    _root.attachMovie("fairy""fairy"+depth_numdepth_num);
     
    _root["fairy"+depth_num]._x random(1024);
     
    _root["fairy"+depth_num]._y random(768); 
    fairy_container.push(_root["fairy"+depth_num])//add fairy to the next array spot of fairy_container
     
    depth_num++//increase number
     
     //extra commands begin
    for(var i=0;i<fairy_container.length;i++){//loop through each fairy 
        
    death(); //call the function for the fairy's death animation
     
    fairy_container[i]._x+=5//move x of each fairy in container
     
    fairy_container[i]._y+=5//move y of each fairy in container
     

     
    //extra commands end

    }  

    function 
    death() {
        if( 
    _root["Fairy_death"+depth_num]._currentframe==5){
            
    trace(true);
            
    Key.removeListener(Attack_Fairy);
        }


  9. #9
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,227
    PHP Code:
    setInterval(SpawnFairy2000);
    var 
    depth_num=0;
    var 
    fairy_container=[]
    var 
    fairy_death_container=[]
    function 
    SpawnFairy() {
     
     
    _root.attachMovie("fairy""fairy"+depth_numdepth_num);
     
    _root["fairy"+depth_num]._x random(1024);
     
    _root["fairy"+depth_num]._y random(768); 
    fairy_container.push(_root["fairy"+depth_num])//add fairy to the next array spot of fairy_container
     
    depth_num++//increase number
     
     //extra commands begin
    for(var i=0;i<fairy_container.length;i++){//loop through each fairy 
        
    death(i); //call the function for the fairy's death animation & send value i to construct the full name of fairy_death at the death function
     
    fairy_container[i]._x+=5//move x of each fairy in container
     
    fairy_container[i]._y+=5//move y of each fairy in container
     

     
    //extra commands end

    }  

    function 
    death(a) {
        if( 
    _root["Fairy_death"+a]._currentframe==5){//the loop is for fairy though so I would use:  _root["fairy"+a]
            
    trace(true);
            
    Key.removeListener(Attack_Fairy);
        }

    Last edited by AS3.0; 11-14-2021 at 11:17 PM.
    Its cheesasaurous rex

  10. #10
    Nothing seems to be working. I even changed it to where the kill() function would just have the fairy movieclip jump to there frames containing the death animation instead of spawning in a separate one.

    Something definitely broke

  11. #11
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,227
    Ok so when fairy reaches a certain frame it will be removed and a fairy_death will be added to replace its spot, at the top function in "extra commands 2", fairy death can keep moving if the container for fairy_death has any fairy_death movieclips stored in it:


    PHP Code:
    setInterval(SpawnFairy2000);
    var 
    depth_num=0;
    var 
    fairy_container=[]
    var 
    fairy_death_container=[]
    function 
    SpawnFairy() {
     
     
    _root.attachMovie("fairy""fairy"+depth_numdepth_num);
     
    _root["fairy"+depth_num]._x random(1024);
     
    _root["fairy"+depth_num]._y random(768); 
    fairy_container.push(_root["fairy"+depth_num])//add fairy to the next array spot of fairy_container
     
    depth_num++//increase number
     
     //extra commands begin
    for(var i=0;i<fairy_container.length;i++){//loop through each fairy 
        
    death(i); //call the function for the fairy's death animation & send value i to construct the full name of fairy_death at the death function
     
    fairy_container[i]._x+=1//move x of each fairy in container
     
    fairy_container[i]._y+=1//move y of each fairy in container
     

     
    //extra commands end


     //extra commands begin 2
    for(var i=0;i<fairy_death_container.length;i++){//loop through each fairy 
      
     
    fairy_death_container[i]._x+=1//move x of each fairy in container
     
    fairy_death_container[i]._y+=1//move y of each fairy in container
     

     
    //extra commands end 2
     
    }  

    function 
    death(a) {
        if( 
    _root["fairy"+a]._currentframe==3){//the loop is for fairy though so I would use:  _root["fairy"+a]
            
    trace(true);//takes a while to get to frame 5 with a slow interval

    _root.attachMovie("Fairy_death""Fairy_death"+depth_numdepth_num);//attach depth fairy
    fairy_death_container.push(_root["Fairy_death"+depth_num])//add death fairy to fairy_death_container array to be accessed as fairy_death_container[0] , fairy_death_container[1] etc...

    _root["Fairy_death"+depth_num]._x fairy_container[a]._x;//before removing the old fairy, give the fairy death the same coordinates as the old fairy
    _root["Fairy_death"+depth_num]._y fairy_container[a]._y;//before removing the old fairy, give the fairy death the same coordinates as the old fairy
     
    unloadMoviefairy_container[a]);//unload current fairy in the array position [a] that touched the hit movieclip 
    fairy_container.splice(a,1);//remove fairy that was unloaded from its array position

     
    Key.removeListener(Attack_Fairy);
    }
         
    depth_num++//increase the depth after you are done using anything with attach

    Its cheesasaurous rex

  12. #12
    Crap, It's still not working.
    I have an Idea though, I think there's a tutorial on how to make an asteroids game. Maybe I can look into that and see if I can get the spawning working

  13. #13
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,227
    Alright, try that, or maybe explain it more detailed. If the setInterval happens every 2 seconds,
    it would take 10 seconds to view a fairy death replace the fairy.

  14. #14
    Is it okay if I share the .FLA file to you?

  15. #15
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,227
    Yes
    Its cheesasaurous rex

  16. #16
    https://noirville.neocities.org/flashkit_share.zip

    I tried a different approach to the spawn code. Following a tutorial on how to make asteroids in flash which had instructions on how to make a spawn cap as well.

    However, the fairies spawn all at once and every time I try to add the setInterval, it crashes flash.

    PHP Code:
    function init() {
        
    //Settings for the game
        
    SPEEDER 6;
        
    //positive speed for the fairy
        
    NEGSPEEDER = -6;
        
    //negative speed for the fairy
        
    LEFT 0;
        
    //x = 0
        
    TOP 0;
        
    //y = 0
        
    BOTTOM Stage.height-50;
        
    //y = 718
        
    RIGHT Stage.width-50;
        
    //x = 974
        
    MAXFAIRY 10;
        
    //Spawncap for the fairies
        
    fairy_array = new Array();
        
    //Fairy Array
        
    createEmptyMovieClip("fairy_spawn"0);
        
    //movieclip where fairies spawn in
        
    fairyinit();
    }
    function 
    fairyinit() {
        
    //settings for the fairies
        
    FAIRYINDEX 0;
        
    //Set the current spawn to 0
        
    while (FAIRYINDEX<MAXFAIRY) {
            
    //when the fairy number is below the spawn cap, spawn faries periodically. However, flash crashes when I try to use the interval, perhaps an issue with flash trying to count how many movieclips are on the canvas and running the setInterval code at the same time
            //setInterval(SpawnFairy, 1000);
            
    spawnFairy();
        }
    }
    function 
    spawnFairy() {
        
    //Code for when the fairy is spawned. This determines the speed, the coordinates, and runs the function for the movement
        
    fairy_clip fairy_spawn.attachMovie("Fairy""Fairy"+FAIRYINDEXFAIRYINDEX++);
        
    trace(fairy_clip);
        
    fairy_clip.NEGVELX Math.random()*NEGSPEEDER;
        
    fairy_clip.NEGVELY Math.random()*NEGSPEEDER;
        
    fairy_clip.VELX Math.random()*SPEEDER;
        
    fairy_clip.VELY Math.random()*SPEEDER;
        
    fairy_clip._x Math.random()*RIGHT;
        
    fairy_clip._y Math.random()*BOTTOM;
        
    fairy_clip.onEnterFrame fairymove;
        
    fairy_array.push(fairy_clip);//The fairy enemy
    }
    function 
    fairymove() {//The movement of the fairy. This is the basics, I'll probably write something different later once I get the spawn and death animations done.
        
    if (this.hitTest(Wall) == true) {
            
    this._x += this.VELX;
            
    this._y += this.VELY;
            
    this._x += this.NEGVELX;
            
    this._y += this.NEGVELY;
        }
        if (
    this.hitTest(Wall) == false) {//The goal of the faries is to swat them down before they leave the field. Planning on having this deducting 3 points from the score.
            
    unloadMovie(this);
        }


  17. #17
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,227
    Oh yes, the crash is because FAIRYINDEX++ wasn't implemented in the function "spawnFairy()" to eventually be able to end the while() loop.

    Here is the file in AS3.0 version because I no longer have an IDE that supports AS2.0: Download

    That shouldn't be a problem right?
    Last edited by AS3.0; 01-11-2022 at 03:22 AM.

  18. #18
    Is it easy to convert it into AS2?

  19. #19
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,227
    If you take a minute to download & Pre-Register an account to my AS3.0 game I'll rewrite it in AS2.0, can you let me know if the process went smooth, Thanks. I don't have anyone to test register.

    Or else if that is not possible you can just ask me certain parts that you need converted. It shouldn't be that much of an issue to convert. You need to use proper as2 enter frame, remove event listeners, addchild for as2 is attach.
    Its cheesasaurous rex

  20. #20
    I just need the important parts converted, if that's okay with you.

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