Crossman
Crossman

Reputation: 278

Adding a single instance of an object to the stage

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

Upvotes: 0

Views: 159

Answers (2)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

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);    

Upvotes: 2

Pixel Elephant
Pixel Elephant

Reputation: 21403

You don't have an else condition for the your statements where you add the objects, so most likely what is happening is that all the statements are satisfying the condition at once. Adding an else statement should prevent this from happening:

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;
    }
}
else 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;
    }
}
else 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;
    }
}

This way it will first check to see if p5 has hit c1 and only if it hasn't will it check if it has hit c2 and then the same thing for c3.

Upvotes: 0

Related Questions