A Flash Developer Resource Site

Results 1 to 13 of 13

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,218
    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
        


  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,218
    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.

  5. #5
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,218
    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.

  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,218
    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.

  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,218
    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.

  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,218
    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


  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,218
    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.

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