A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [RESOLVED] Stuck with 'while' function

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    24

    resolved [RESOLVED] Stuck with 'while' function

    Hi there, I have four dynamic text fields, each saying 'Left', 'Right', 'Up' and 'Down'. When my main character hits an object, the respective directional text is blanked out (i.e. if the character hit an object on its right, the 'Right' text would display nothing).
    Here is the function below:
    Actionscript Code:
    hero.mc.addEventListener(Event.ENTER_FRAME, hithomeblocks);
    function hithomeblocks(event:Event):void{
        hero.mc.point = new Point(hero.mc.x, hero.mc.y);
        while(level.mc.mcHomeBlocks.hitTestPoint(hero.mc.point.x, (hero.mc.point.y - (hero.mc.height)), true)) {
            up = false;
            mctxtUp.txtUp.text = "";
            break;
        }
        while(level.mc.mcHomeBlocks.hitTestPoint((hero.mc.point.x - (hero.mc.width)), hero.mc.point.y, true)) {
            left = false;
            mctxtLeft.txtLeft.text = "";
            break;
        }
        while(level.mc.mcHomeBlocks.hitTestPoint(hero.mc.point.x, (hero.mc.point.y + (hero.mc.height)), true)) {
            down = false;
            mctxtDown.txtDown.text = "";
            break;
        }
        while(level.mc.mcHomeBlocks.hitTestPoint((hero.mc.point.x + (hero.mc.width)), hero.mc.point.y, true)) {
            right = false;
            mctxtRight.txtRight.text = "";
            break;
        }
    }
    Which works fine. However, I would like to add a function where whenever the character isn't touching an object on one side, the text linked to that side is displayed again. So if the character walked away from an object on his right, the 'Right' text would display 'Right' again.
    Does anyone know how to do this?

    P.S. I tried this:
    Actionscript Code:
    while(level.mc.mcHomeBlocks.hitTestPoint((hero.mc.point.x + (hero.mc.width)), hero.mc.point.y, false)) {
        mctxtRight.txtRight.text = "Right";
        break;
    }
    But that just made the text permanently display 'Right'.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    First, while isn't a function. It's a loop.

    Second, you're not even using it as a loop. You could replace "while" with "if" in your code and it would work exactly the same way. Let's look at why:

    If the condition of the while is true, then you enter the body of the loop. Since the loop body contains "break;", it will exit the loop. So you only execute a maximum of once per while (per call to the hithomeblocks function). If the while condition is NOT true, you never enter the body of the loop at all. Tada! You just invented a hard-to-read, verbose way of writing "if". Which, leads to the answer to your second question.

    Replace the whiles with ifs. Remove the break statements. And add else clauses which restore the texts and set your up/down/right/left variables back to their correct values.

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    24
    Thank you so much, that worked! I can't believe I didn't see that...
    Also, thanks for explaining it too, I'm still new to this and that's really helped

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center