事件监听器不会在AS3周围第二次工作

时间:2012-11-28 12:35:32

标签: actionscript-3

我被困住了!如果我需要了解更多细节,请发表评论,我将详细说明。

我尝试创建一些代码,动态地将链接对象添加到舞台上,然后在放入正确的区域时删除,然后创建下一个,依此类推。信息在一个数组中,它会循环播放,直到游戏完成并且符合 targetScore 。如果计时器用完,游戏将停止,并且会调用 win() lose()函数并显示重试按钮。这可以正常工作,直到游戏停止,我尝试使用 retry()函数重新启动程序。 retry()函数尝试重置程序启动时的所有内容,再次创建 baseball 对象,然后让 releaseToDrop()像第一次那样重复一切。根据我停止的位置,当我第二次到达同一个地方时, clicktoDrag1()函数无法获取当前对象。它可以在从库中动态创建的8个对象中的任何一个上。当舞台在* mouse_up *上听到监听器时,我可以单击并拖动对象,但随后它与数组略微不同步(它们在 retry()中被重置,它会崩溃函数),即使所有跟踪语句都按原样读取,它所放置的目标也无法识别它。我知道这看起来很多,而且我不确定这是否可以通过论坛解决,但是任何一般的提示都会受到赞赏。

我通常保持代码简单,但我想提前编写代码,这似乎有效,直到事情变得太复杂。

我以前从来没有发过求助,但是我已经把这一切都给了这个,如果不能修复,我将不得不重新开始并简化。

如果有人花时间看这个,我会感到谦卑 - 并希望更多地使用这个论坛来成为一个更好的程序员。

一切都在时间轴上,这是代码。

import flash.display.MovieClip;
import flash.ui.Mouse;

var startPosX = 450;
var startPosY = 400;

//setup first clip
var baseball:MovieClip = new Baseball();
baseball.name = "baseball";

addChild(baseball);
baseball.buttonMode = true;
baseball.x = startPosX;
baseball.y = startPosY;

activity_txt.text = "Swinging a baseball bat";

//setup small clips
baseballSmall.visible = false;
golfSmall.visible = false;
swimSmall.visible = false;
boxingSmall.visible = false;
tennisSmall.visible = false;
dartsSmall.visible = false;
powerSmall.visible = false;
marathonSmall.visible = false;

theEnd.visible = false;
retry_btn.visible = false;

fast1.visible = false;
fast2.visible = false;
fast3.visible = false;
fast4.visible = false;
slow1.visible = false;
slow2.visible = false;
slow3.visible = false;
slow4.visible = false;

//setup vars
var counter:int = 0;
var sportCounter:int = 0;
var startingLife:int = 15;
var playerLife = startingLife;
var lifeBoost:int = 3;
var targetScore:int = 8;
var countdownTimer:Timer = new Timer(500,0);
var questionTimer:Timer = new Timer(250,2);
var score:int =  0; 

var smallArray = new Array("baseballSmall","golfSmall", "swimSmall", "boxingSmall","tennisSmall", "dartsSmall", "powerSmall", "marathonSmall");
var sportArray = new Array("baseball","Golf", "Swimming", "Boxing", "Tennis", "Darts", "PowerLifting", "Marathon");
var answersArray = new Array("fast", "fast", "slow", "fast", "fast", "slow", "slow", "slow");
var letArray = new Array("fast1", "fast2", "slow1", "fast3", "fast4", "slow2", "slow3", "slow4");
var activityTXTArray = new Array("Golf swing", "100m swim", "Boxing punch", "Tennis racquet swing", "Darts throw", "Power lifting", "Marathon");

var arraySmall:Array = smallArray;
var arrayLet:Array = letArray;
var arrayActivity:Array = activityTXTArray;

//var draggable = getChildByName(sportArray[0]);

//setup bonus bar
bonusBar.gotoAndStop(2);
bonusBar.x = timeBar.x;
bonusBar.y = timeBar.y - timeBar.height;
bonusBar.height = timeBar.height/startingLife * lifeBoost;

playerLife = startingLife;
timeBar.height = playerLife * (300/startingLife);   

/* listeners */
countdownTimer.addEventListener(TimerEvent.TIMER, timerTick);
countdownTimer.start();
retry_btn.addEventListener(MouseEvent.CLICK, retry);
baseball.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);


function clickToDrag1(event:MouseEvent):void
{   
    var draggable = getChildByName(sportArray[sportCounter]);
    trace("the counter name is "+ sportArray[sportCounter]);
    trace("the baseball name is "+ baseball.name);
    trace("the draggable name is "+ draggable.name);
    trace("the answer array is "+ answersArray[counter]);
    trace("the sport array is "+ sportArray[sportCounter]);
    this.setChildIndex(draggable,this.numChildren-1);
    draggable.startDrag();
}

function releaseToDrop(event:MouseEvent):void
{
        //get current obj name from sportArray
    var draggable = getChildByName(sportArray[sportCounter]);
        //check if this obj is dropped on the Fast or Slow MovieClip
    if(draggable.hitTestObject(getChildByName(answersArray[counter])))
    {           
            //move on to the next F/S answer
        counter++;
        score++;

        var tick = new Tick();
        addChild(tick);     
        tick.x = 370;
        tick.y = 200;

        activity_txt.text = activityTXTArray.shift();

        playerLife += lifeBoost;
        timeBar.height = playerLife * (300/startingLife);
        bonusBar.gotoAndPlay(2);        

        var smallName = getChildByName(smallArray.shift());
        smallName.visible = true;

        var letters = getChildByName(letArray.shift());
        letters.visible = true;

            //remove the drag listenter on the current object (name assigned via sportArray)
        draggable.removeEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
            //stage.removeEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
            //remove the current object
        removeChild(getChildByName(sportArray[sportCounter]));
            //delete ref
        var deleteRef = getChildByName(sportArray[sportCounter]);
        deleteRef = null;
            //move on to the next one
        sportCounter++;
            //add a new object
        var obj:Class = getDefinitionByName(sportArray[sportCounter]) as Class;
        var myMclip = new obj();
            //name it
        myMclip.name = sportArray[sportCounter];
            //var clipName = getChildByName(sportArray[0]);
        myMclip.x = myMclip.y = 400;
        myMclip.buttonMode = true;
        trace("myClip name "+myMclip.name);
        addChild(myMclip);
            //add listener to new obj (is this removed via draggable?)
        myMclip.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
        //stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
    }
    else{
        draggable.x = startPosX;
        draggable.y = startPosY;
        draggable.stopDrag();
        var cross = new Cross();
        addChild(cross);
        cross.x = 370;
        cross.y = 200
    }   
}


function timerTick(e:TimerEvent):void {
    //removes from 40(life) every half a second
    playerLife -= 1;
    //bar height = % of whats left of life
    timeBar.height = playerLife * (300/startingLife);
    bonusBar.y = timeBar.y - timeBar.height;

    if(playerLife == 0) {                                       
        loseGame(); 
    } else if(playerLife>0 && score > targetScore-1) {
        winGame();
    }
}


function loseGame():void
{
    var removeCurrent = getChildByName(sportArray[sportCounter]);
    removeCurrent.visible = false;  
    hideStuff();
    theEnd.visible = true;
    theEnd.end_txt.text = "sorry you lost"
    retry_btn.visible = true;
    //baseball.removeEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
    //stage.removeEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
    //baseball = null;
    trace(baseball);    
}

function winGame():void
{
    var removeCurrent = getChildByName(sportArray[sportCounter]);
    removeCurrent.visible = false;
    hideStuff();
    theEnd.visible = true;
    theEnd.end_txt.text = "You've won!"
    retry_btn.visible = true;
    baseball.removeEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
    //stage.removeEventListener(MouseEvent.MOUSE_UP, releaseToDrop);
}

function retry(e:MouseEvent):void
{
    playerLife = startingLife;
    timeBar.height = playerLife * (300/startingLife);

    score = 0;
    counter = 0;
    sportCounter = 0;

    countdownTimer.reset();
    countdownTimer.start();

    var baseball:MovieClip = new Baseball();
    baseball.name = "baseball";
    trace("the type is "+baseball);
    trace("the name is " + baseball.name);
    addChild(baseball);
    baseball.buttonMode = true;
    baseball.x = startPosX;
    baseball.y = startPosY; 

    baseball.addEventListener(MouseEvent.MOUSE_DOWN, clickToDrag1);
    //stage.addEventListener(MouseEvent.MOUSE_UP, releaseToDrop);

    activity_txt.text = "Swinging a baseball bat";

    smallArray = arraySmall;
    letArray = arrayLet;
    activityTXTArray = arrayActivity;

    retry_btn.visible = false;
    theEnd.visible = false;
    showStuff();
    smallArray = new Array("baseballSmall","golfSmall", "swimSmall", "boxingSmall","tennisSmall", "dartsSmall", "powerSmall", "marathonSmall");
var sportArray = new Array("baseball","Golf", "Swimming", "Boxing", "Tennis", "Darts", "PowerLifting", "Marathon");
var answersArray = new Array("fast", "fast", "slow", "fast", "fast", "slow", "slow", "slow");
var letArray = new Array("fast1", "fast2", "slow1", "fast3", "fast4", "slow2", "slow3", "slow4");
var activityTXTArray = new Array("Golf swing", "100m swim", "Boxing punch", "Tennis racquet swing", "Darts throw", "Power lifting", "Marathon");
}

function showStuff():void
{
    activity_txt.visible = true;
    fast.visible = true;
    slow.visible = true;
    timeBar.visible = true;
    bonusBar.visible = true;
}

function hideStuff():void
{
    fast1.visible = false;
    fast2.visible = false;
    fast3.visible = false;
    fast4.visible = false;
    slow1.visible = false;
    slow2.visible = false;

    baseballSmall.visible = false;
    golfSmall.visible = false;
    swimSmall.visible = false;
    boxingSmall.visible = false;
    tennisSmall.visible = false;
    dartsSmall.visible = false;

    activity_txt.visible = false;
    fast.visible = false;
    slow.visible = false;
    timeBar.visible = false;
    bonusBar.visible = false;
}

1 个答案:

答案 0 :(得分:1)

OP解决了问题,如in this comment所示:

  

我没有删除游戏端的对象,我隐藏了它,所以当它再次出现时,会创建一个副本。我删除它而不是隐藏它,它工作正常。在舞台上有两个剪辑创建了一些奇怪的拖放行为,这种行为让它消失了。 “这不是'我的'答案.MangoPop在评论中回答了这一点,但忘记将其作为答案添加。”