将一个对象实例添加到舞台上

时间:2012-11-01 16:22:00

标签: actionscript-3 flash instance

晚安,

我似乎无法让我的代码只将一个对象的实例添加到舞台上。它似乎在舞台上添加了至少3个对象实例。添加的对象是r2,r3和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;
 }

提前致谢

2 个答案:

答案 0 :(得分:2)

使用switch语句而不是所有if语句(如果您只想要满足其中一个且只满足其中一个条件)会更高效,更清晰。

你的问题我认为与你跟踪老鼠的方式有关 - 例如。使用stage.getChildByName
以下是一个示例,以及一些其他潜在问题的注释说明。

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
}

最好不要使用stage.getChidByName,而是创建一个这样的方法:

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

然后使用该新函数而不是stage.getChildByName:

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

除了您的问题之外,创建一个方法/函数来进行鼠标定位和添加而不是一遍又一遍地复制代码会更加清晰......

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

答案 1 :(得分:0)

您添加对象的语句没有else条件,因此最有可能发生的是所有语句一次满足条件。添加else语句可以防止这种情况发生:

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

通过这种方式,它首先会检查p5是否已经命中了c1,并且只有在没有它的情况下才会检查它是否已经命中c2,然后是c3的相同内容。