1

For some reason my value is not being updated outside of the function. I'm trying to make a button, instanced "plus", move a movie clip "topArrow" constantly upward. I figured the boolean would be an easy way to trigger this, but it isn't being updated outside of the function. Why is this?

import flash.events.Event;
import flash.events.MouseEvent;

var speed:Number = 1;

plus.addEventListener(MouseEvent.MOUSE_DOWN, arrow_up);
plus.addEventListener(MouseEvent.MOUSE_UP, arrow_stop);
minus.addEventListener(MouseEvent.MOUSE_DOWN, arrow_down);
minus.addEventListener(MouseEvent.MOUSE_UP, arrow_stop);

var move_up:Boolean = false;
var move_down:Boolean = false;



function arrow_up(event:MouseEvent):void
{
    trace("button pressed");
    move_up = true;
}


function arrow_stop(event:MouseEvent):void
{

    move_up = false;
    move_down = false;
}

function arrow_down(event:MouseEvent):void
{

    move_down = true;
}

while (move_up==true)
{
    topArrow.y +=  speed;
}

while (move_down==true)
{
    topArrow.y -=  speed;
}

if(move_up)
{
    trace("true");
}

2 Answers2

1

Those while loops are scary, once move_up is true it will go into that loop and never exit?

I would do something like the below instead to animate the movie clip :

var speed:Number = 1;

plus.addEventListener(MouseEvent.MOUSE_DOWN, arrow_up);
plus.addEventListener(MouseEvent.MOUSE_UP, arrow_stop);
minus.addEventListener(MouseEvent.MOUSE_DOWN, arrow_down);
minus.addEventListener(MouseEvent.MOUSE_UP, arrow_stop);

stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);

var move_up:Boolean = false;
var move_down:Boolean = false;

function arrow_up(event:MouseEvent):void
{
    trace("button pressed");
    move_up = true;
}

function arrow_stop(event:MouseEvent):void
{
    move_up = false;
    move_down = false;
}

function arrow_down(event:MouseEvent):void
{
    move_down = true;
}

function onEnterFrame(event:Event):void
{
   if(move_up)
       topArrow.y += speed;
   else if(move_down)
       topArrow.y -=speed;

   if(move_up)
   {
      trace("true");
   }
}
Barış Uşaklı
  • 13,440
  • 7
  • 40
  • 66
  • I agree... and those while loops would never even start, because when the script first runs, move_up and move_down are false. While loops don't just run forever while your program is running. – bwroga Mar 03 '13 at 06:50
  • Also, loops (`while`, `for`) are executed in one frame - they are not intended to span across multiple frames, so can't be used to animate things. That's what `ENTER_FRAME` event is for - it fires once on every frame, so it can be used to animate stuff. – strah Mar 04 '13 at 13:16
  • I would use enter_frame but it is stuck on one frame, because of stop(). Maybe I will make everything dynamic, but I was hoping to cheat a little. – user2128095 Mar 05 '13 at 00:35
-1

Decompile binary and take a look at move_up setter function.