0

我被困住了!如果我需要详细说明,请发表评论,我会详细说明。

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

我通常保持代码简单,但我想推进并让代码自己编写,这似乎一直有效,直到事情变得对我来说太复杂了。

我以前从未发帖寻求帮助,但我已经给出了一切,如果无法修复,我将不得不重新开始并简化。

提前感谢,如果有人花时间看这个,我会很谦虚 - 并且愿意更多地使用这个论坛来成为一个更好的编码器。

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

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

1 回答 1

1

OP 已解决该问题,如此评论中所示:

我没有在游戏结束时移除这个对象,我已经隐藏了它,所以当它再次出现时,就会创建一个副本。我已经删除它而不是隐藏它,它工作正常。在舞台上放置两个剪辑会产生一些奇怪的拖放行为,这有点泄露了它。“这不是‘我的’答案。MangoPop 在评论中回答了这个问题,但忘记添加它作为答案。”

于 2013-05-22T16:59:15.757 回答