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

  • SEARCH
  • Home
  • 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 8768185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:00:38+00:00 2026-06-13T17:00:38+00:00

Good Evening, I can’t seem to get my code to only add one instance

  • 0

Good Evening,

I can’t seem to get my code to only add one instance of an object to the stage. It seems to add atleast 3 instances of the object to the stage. The objects added is r2, r3 and r4.

stop();

import flash.events.Event;
stage.focus=stage;
var upKeyDown5:Boolean = false;
var rightKeyDown5:Boolean = false;
var downKeyDown5:Boolean = false;
var leftKeyDown5:Boolean = false;
var enterkey:Boolean = false;
var interaction:Boolean = false;
p5.addEventListener(Event.ENTER_FRAME, moveChar5);
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown5);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp5);
Mouse.hide();
function moveChar5(e:Event):void{
if(downKeyDown5 && !upKeyDown5 && !rightKeyDown5 && !leftKeyDown5)
{
    p5.gotoAndStop("walk_down");
    if(p5.y < 492.75)
        p5.y += 6;
}
if(upKeyDown5 && !downKeyDown5 && !rightKeyDown5 && !leftKeyDown5)
{
    p5.gotoAndStop("walk_up");
    if(p5.y > 202.85)
        p5.y -= 6;
}
if(rightKeyDown5 && !upKeyDown5 && !downKeyDown5 && !leftKeyDown5)
{
    p5.gotoAndStop("walk_right");
    if(p5.x < 871.5)
        p5.x += 6;
}
if(leftKeyDown5 && !upKeyDown5 && !rightKeyDown5 && !downKeyDown5)
{
    p5.gotoAndStop("walk_left");
    if(p5.x > 203.65)
        p5.x -= 6;
}
if(enterkey && interaction && p5.hitTestObject(c1)){
    if (!(Boolean(stage.getChildByName('r2')))) {
        var rat2:r2;
        rat2 = new r2();
        addChild(rat2);
        rat2.y=38;
        rat2.x=32;
    }
}
if(enterkey && interaction && p5.hitTestObject(c2)){
    if (!(Boolean(stage.getChildByName('r3')))) {
        var rat3:r3;
        rat3 = new r3();
        addChild(rat3);
        rat3.y=38;
        rat3.x=32;
    }
}
if(enterkey && interaction && p5.hitTestObject(c3)){
    if (!(Boolean(stage.getChildByName('r4')))) {
        var rat4:r4;
        rat4 = new r4();
        addChild(rat4);
        rat4.y=38;
        rat4.x=32;
    }
}
 }


 function checkKeysDown5(event:KeyboardEvent):void{
if(event.keyCode == 87){
    upKeyDown5 = true;
}
if(event.keyCode == 68){
    rightKeyDown5 = true;
}
if(event.keyCode == 83){
    downKeyDown5 = true;
}
if(event.keyCode == 65){
    leftKeyDown5 = true;
}
if (event.keyCode == 13){
    enterkey = true;
}
 }


 function checkKeysUp5(event:KeyboardEvent):void{
if(event.keyCode == 87){
    upKeyDown5 = false;
    p5.gotoAndStop("still_up");
}

if(event.keyCode == 68){
    rightKeyDown5 = false;
    p5.gotoAndStop("still_right");
}

if(event.keyCode == 65){
    leftKeyDown5 = false;
    p5.gotoAndStop("still_left");
}

if(event.keyCode == 83){
    downKeyDown5 = false;
    p5.gotoAndStop("still_down");
}

if (event.keyCode == 13){
    enterkey = false;
}

if(p5.hitTestObject(c1) || p5.hitTestObject(c2) || p5.hitTestObject(c3)){
    p5.gotoAndStop("interaction");
    interaction = true;
}
else
    interaction = false;
 }

Thanks in advance

  • 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-06-13T17:00:39+00:00Added an answer on June 13, 2026 at 5:00 pm

    It would be more efficient and cleaner to use a switch statement instead all those if statements (if you only ever want one and only one of those conditions to be met)

    You’re issue I think has to do with the way you’re tracking your rats – eg. using the stage.getChildByName.
    Below is an example, plus some commented notes on some of your other potential issues.

    switch(true){
        case (enterkey && interaction && p5.hitTestObject(c1)):
            if (!(Boolean(stage.getChildByName('r2')))) {  //your not giving your rat a name so this will always return false, plus you're not adding the rat to the stage but to this class
                var rat2:r2;
                rat2 = new r2();
                addChild(rat2); //use stage.addChild if you want the above check (stage.getChildByName) to work
                rat2.y=38;
                rat2.x=32;
                rat2.name = "r2"; //if you want the above check to work
    
                break;  //break out of the switch if you don't want any of the others evaluate since this rat got added
            }
    
        //do a case for your other blocks
    }
    

    It would be better to not use stage.getChidByName at all, and instead create a method like this:

    function checkRatExists(ratClass:Class):Boolean {
        var tmp:DisplayObject;
        var i:int = numChildren;
        while(i--){
            tmp = getChildAt(i);
            if(tmp is ratClass){
                return true;
            }
        }
        return false;
    }
    

    Then use that new function instead of stage.getChildByName:

    if (!checkRatExists(r2)) //r2 is the class of rat you want to check
    

    As an aside from your issue, it would be much cleaner to create a method/function to do your rat positioning and adding instead of duplicated the code over and over and over…

    function addRat(rat:RatCommonBaseClass, ratName:String):void {
            addChild(rat);
            rat.y=38;
            rat.x=32;
            rat.name = ratName;
    }
    
    //now you can just do this for all your rats:
    addRat(new r2(),"r2);    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening, i need to know how can i have one .h file that
Hi and good evening, hope someone can help. I am trying to get some
Good evening guys. I'm currently trying to get started on development of a project
Good evening, I'm having an issue with Masonry. This is all my relevant code:
Good evening, i hope you can help me with this problem, as I'm struggling
good evening. ingore all other elements, I want get first image in each foreach.
Good Evening All, I've created the following stored procedure: CREATE PROCEDURE AddQuote -- Add
Good evening StackOverflow This time I'm fighting with a ListView Containing TextViews. I add
Good evening, I have one table, with a timestamp column, then I have a
Good Evening StackOverflowers! I am running into what seems to be a catch 22

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.