Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6755213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:22:23+00:00 2026-05-26T13:22:23+00:00

First time poster, long time reader. I’ve been having a problem with figuring this

  • 0

First time poster, long time reader.
I’ve been having a problem with figuring this out. I’ve been stuck in my game for 4 hours now, googling and trying to figure out how to do it.
I have a game where i add some cartoonish ants, that when they are clicked, they need to be removed from stage. There are 4 differend kinds of ants, so im doing a Math.random for picking which one to add. (ant 1+2+3 have 50% chance to spawn and 4th 50%)

rnd_nbr = (Math.random() * 5)+1;

I have a timer doing 10 tick, and i reset the timer to make neverending.
Then i have a math random and if sentences adding mc’ to the stage with movement from Tweener, and event listeners for clicks.
But i cant figure out how to remove them when clicked.
I have done alot of failed tries right inside the click_candy_anty function.
I’ve left them commented out.

I apologize for abit messy coding, but it will be cleaned up when(hopefully) it gets working.

Help is highly appreciated.

import caurina.transitions.Tweener;

var ant_index:Array = new Array(10);    //index for ants
var ant_number:int; 
var ant_temp:int;   
var rnd_nbr:int;                        //var for rnd number
var score:int = 0;
var score_update:String;
var reset_timer:Boolean = false;
var antTimer:Timer = new Timer(800,10); //timer
antTimer.addEventListener(TimerEvent.TIMER, create_ant);

var anty0_:anty_0;                          //creating ant vars for all 5 kind
var anty1_:anty_1;                          
var anty2_:anty_2;
var anty3_:anty_3;
var anty4_:anty_4;
var screen_number:int = 0;

var antyArray:Array = new Array(10);

var main:main_mc = new main_mc;
main.x = 0; //0,0
main.y = 0;
addChild(main);

var score_format:TextFormat = new TextFormat();
score_format.size = 25;
score_format.align = TextFormatAlign.CENTER;

var score_txt:TextField = new TextField();
score_txt.defaultTextFormat = score_format;
score_txt.text = "" + score;
score_txt.x = 600;
score_txt.y = 20;
score_txt.border = true;
score_txt.autoSize =  TextFieldAutoSize.LEFT ;
score_txt.height = 40;
addChild(score_txt);

var score_txt_update:TextField = new TextField();
score_txt_update.defaultTextFormat = score_format;
score_txt_update.text = "0";
score_txt_update.x = 550;
score_txt_update.y = 20;
score_txt_update.border = true;
score_txt_update.autoSize =  TextFieldAutoSize.LEFT ;
score_txt_update.height = 40;
score_txt_update.alpha = 0;
addChild(score_txt_update);

function click_candy_anty(event:MouseEvent):void{
    if (score < 20){
    //trace("evt: " + evt);
    //this.removeChild();
    //this.removeChild(this);
    //removeChild(evt.currentTarget);

    //removeChild(evt.target.name.substr(7));
    //removeChild(this);
    //removeChild(evt.currentTarget.name);


//  trace("The " + evt.target.label + " button was clicked");
//  trace(evt.type)

    score++;
    score_txt.text = "" + score;
    //score_update = "+1";
    score_txt_update.text = "+1";
    //ori position x:570 y:20
    score_txt_update.y = -30;
    Tweener.addTween(score_txt_update, {y: 20, alpha: 1, time: 0.8, transition:"linear", onComplete:score_txt_update_fade});
    }
    else {
        stop_game();
        trace("48");
    }
}

function score_txt_update_fade(Event = null){
    Tweener.addTween(score_txt_update, {y: 50, alpha: 0, time: 0.4, transition:"linear"});
}

function click_leaf_anty(Event = null):void{
    if (score > 0 && score < 20){
    score--;
    score_txt.text = "" + score;
    score_txt_update.text = "-1";
    score_txt_update.y = 50;
    Tweener.addTween(score_txt_update, {y: 20, alpha: 1, time: 0.4, transition:"linear", onComplete:score_txt_update_fade_neg});
    trace("12 wrong");
    }
/*      else {
        stop_game();
        trace("12");
        trace("score: " + score + ", ");
    }*/
}

function score_txt_update_fade_neg(Event = null){
    Tweener.addTween(score_txt_update, {y: -30, alpha: -1, time: 0.8, transition:"linear"});
}

//screen1();
start_timer();

function screen1(Event = null):void {
    screen_number = 2;//slet når event listener til scr1 kommer
    if (screen_number == 2){

    }
    else {
    screen_number = 2;

    }
}

function screen2(Event = null):void {

    if (screen_number == 3){

    }
    else {
    screen_number = 3;
    }
    start_timer();
}

function start_timer(Event = null):void {
    if (score < 20) {
        if (reset_timer == false){
            antTimer.start();
            //trace("antTimer initialized");
        }
        if (reset_timer == true){
            antTimer.reset();
            antTimer.start();
            //trace("antTimer RESETTED & initialized");
        }
    }
    else {
        stop_game();
        trace("57");
    }

}

function stop_game(Event = null):void {
    if (screen_number != 2){
        screen_number = 2;
    trace("Game completed - launching end screen");
    // move to next screen
        for (var i:Number=0; i<=9;i++){

    ant_temp = ant_number;

    if (ant_temp < 9) {
        //trace("ant_temp er lavere end 9::: " + ant_temp );
        ant_temp++;

    }
    else if (ant_temp == 9) {
        //trace("ant_temp lig med 9::: " + ant_temp );
        ant_temp = 0;
    }

    if (ant_index[ant_temp] == 1){
            //removeChild(anty1_);
            if ("anty1_" + ant_temp != null){ 
            removeChild(getChildByName("anty1_" + (ant_temp)));
                }
        }
        if (ant_index[ant_temp] == 2){
            //removeChild(anty2_);  
            if ("anty2_" + ant_temp != null){ 
            removeChild(getChildByName("anty2_" + (ant_temp)));
                }
        }

        if (ant_index[ant_temp] == 3){  
            //removeChild(anty3_);  
            if ("anty3_" + ant_temp != null){ 
            removeChild(getChildByName("anty3_" + (ant_temp)));
                }
        }

        else if (ant_index[ant_temp] == 4){         
            //removeChild(anty4_);  
            if ("anty4_" + ant_temp != null){ 
            removeChild(getChildByName("anty4_" + (ant_temp)));
                }
        }
            }//for loop end
            screen3();
    }
}

function screen3 (Event = null):void{
    var end:end_mc = new end_mc;
    end.x = 0; //0,0
    end.y = 0; //
    addChild(end);
}

function create_ant(Event = null):void {
    //trace("antTimer triggered");
if (score < 20) {   
        rnd_nbr = (Math.random() * 5)+1;
        ant_index[ant_number] = rnd_nbr; 

        trace("ant_number" + ant_number);
        //trace("ant_index[" + ant_number + "]: " + ant_index[ant_number]);
        if (ant_index[ant_number] == 1){
            anty1_ = new anty_1();
            anty1_.name = "anty1_" + (ant_number);

            anty1_.height = 118;
            anty1_.width = 102;
            anty1_.x = 100;
            anty1_.y = 300;         
            addChild(anty1_);   
            Tweener.addTween(anty1_, {x: 541, time: 3, transition:"linear"});
            anty1_.addEventListener(MouseEvent.MOUSE_DOWN, click_candy_anty);
            //trace("anty1_" + ant_number);
            //trace("" + anty_1[ant_number].name);

        }
        if (ant_index[ant_number] == 2){
            anty2_ = new anty_2();
            anty2_.name = "anty2_" + (ant_number);
            anty2_.height = 118;
            anty2_.width = 102;
            anty2_.x = 100;
            anty2_.y = 300;         
            addChild(anty2_);   
            Tweener.addTween(anty2_, {x: 541, time: 3, transition:"linear"});
            anty2_.addEventListener(MouseEvent.MOUSE_DOWN, click_candy_anty);
            //trace("anty2_" + ant_number);
        }

        if (ant_index[ant_number] == 3){
            anty3_ = new anty_3();
            anty3_.name = "anty3_" + (ant_number);
            anty3_.height = 118;
            anty3_.width = 102;
            anty3_.x = 100;
            anty3_.y = 300;         
            addChild(anty3_);   
            Tweener.addTween(anty3_, {x: 541, time: 3, transition:"linear"});
            anty3_.addEventListener(MouseEvent.MOUSE_DOWN, click_candy_anty);
            //trace("anty3_" + ant_number);
        }

        else if (ant_index[ant_number] > 3 && ant_index[ant_number] < 7){
            anty4_ = new anty_4();
            anty4_.name = "anty4_" + (ant_number);
            anty4_.height = 118;
            anty4_.width = 102;
            anty4_.x = 100;
            anty4_.y = 300;         
            addChild(anty4_);   
            Tweener.addTween(anty4_, {x: 541, time: 3, transition:"linear"});
            anty4_.addEventListener(MouseEvent.MOUSE_DOWN, click_leaf_anty);
            //trace("anty4_" + ant_number);
        }

    ant_temp = ant_number;
    if (ant_temp < 9) {
        //trace("ant_temp er lavere end 9::: " + ant_temp );
        ant_temp++;

    }
    else if (ant_temp == 9) {
        //trace("ant_temp lig med 9::: " + ant_temp );
        ant_temp = 0;
    }

    if (ant_index[ant_temp] == 1){
            //removeChild(anty1_);
            removeChild(getChildByName("anty1_" + (ant_temp)));
        }
        if (ant_index[ant_temp] == 2){
            //removeChild(anty2_);  
            removeChild(getChildByName("anty2_" + (ant_temp)));
        }

        if (ant_index[ant_temp] == 3){  
            //removeChild(anty3_);  
            removeChild(getChildByName("anty3_" + (ant_temp)));
        }

        else if (ant_index[ant_temp] == 4){         
            //removeChild(anty4_);  
            removeChild(getChildByName("anty4_" + (ant_temp)));
        }


/*  if (ant_number == 9) { //resets the ant_number - being the end of the 10th ant
            ant_number = 0;
            start_timer();     // launches the timer again
            trace("Timer resetted");

            for (var i:Number=0; i<=9;i++){

        if (ant_index[i] == 1){
            //removeChild(anty1_);
            removeChild(getChildByName("anty1_" + (i)));
        }
        if (ant_index[i] == 2){
            //removeChild(anty2_);  
            removeChild(getChildByName("anty2_" + (i)));
        }

        if (ant_index[i] == 3){ 
            //removeChild(anty3_);  
            removeChild(getChildByName("anty3_" + (i)));
        }

        else if (ant_index[i] == 4){            
            //removeChild(anty4_);  
            removeChild(getChildByName("anty4_" + (i)));
        }
            }//for loop end

    }//ends if=9 reset*/
    if (ant_number == 9) { //resets the ant_number at 10th ant, and restarts the timer
        ant_number = 0;
        reset_timer = true;
        start_timer();
    }
    else {
        ant_number++;
    }

    /*else {
        ant_number++;
    }*/
  }
  else if (score >= 20 && screen_number != 2){
      stop_game();
      trace("14");
  }
} //create_ant func end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T13:22:24+00:00Added an answer on May 26, 2026 at 1:22 pm

    You can also use:

    removeChild( evt.currentTarget as DisplayObject );
    

    Or

    var clicked_ant:DisplayObject = evt.currentTarget as DisplayObject;
    removeChild( clicked_ant );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a long time reader first time poster, glad to start participating in this
Hy everyone, Long time reader, first time poster. This sounds like it should be
first time poster, long time reader so be gentle with me :) See the
Long time reader, first time poster. Any help is greatly appreciated. I have crafted
long time reader, first time poster. I’m coming with an issue that many of
Long time reader, first time poster. Working on a web site at http://www.howardpitch.com/ ,
Long time reader, first time poster. I'm a big noob when it comes to
Long time reader, first time poster asks: After many weeks of google and forum
First time poster, long time reader. I searched and found no questions that answered
Long time reader, first time poster. One day I hope to be answering questions

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.