1

I am trying to execute this code in flash AS3, but it's not working correctly . In the body of my If condition, I set myFlag = 2, but the if condition always is True!!

Here is my code:

var myFlag:int = 1;
if (myFlag==1)
{
    s1.addEventListener(MouseEvent.MOUSE_UP,dars1);
    function dars1(e:MouseEvent):void
    {
        myFlag= 2;
        s1.gotoAndStop(25);
        s1.mouseEnabled = false;
        var darsRequest:URLRequest = new URLRequest("dars1.swf");
        var darsLoader:Loader = new Loader();
        darsLoader.load(darsRequest);
        addChild(darsLoader);
    }

}
else
{
    trace("NO-CLICK");

}
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • 1
    When flag = 1 you setup an event listener... Afterwards even if the flag equals 2, the listener is still present and still fires dars1 on mouseup... Dont you think so ? – JBA Apr 10 '15 at 21:17
  • As JBA wrote: your event listener is added already. You are not checking IN the event listener the condition - you should. – Fygo Apr 10 '15 at 22:44
  • Do you figure out a solution? upvote helpful answers, and accept an answer if it led to your solution. If you solution was different, answer the question yourself and accept that. – BadFeelingAboutThis Apr 14 '15 at 18:48

2 Answers2

0

You remove the event listener after its function is executed:

s1.addEventListener(MouseEvent.MOUSE_UP,dars1);
function dars1(e:MouseEvent):void
{
    myFlag= 2;
    s1.gotoAndStop(25);
    s1.mouseEnabled = false;
    var darsRequest:URLRequest = new URLRequest("dars1.swf");
    var darsLoader:Loader = new Loader();
    darsLoader.load(darsRequest);
    addChild(darsLoader);
    s1.removeEventListener(MouseEvent.MOUSE_UP,dars1);
}
Cilan
  • 13,101
  • 3
  • 34
  • 51
0

Consider your first two lines:

var myFlag:int = 1;  //You are setting the var to 1
if (myFlag==1) //Since you just set it to 1 on the preceding line, this will ALWAYS be true
{

Even though you set myFlag inside your if statement, the next time this block of code runs, you'll just be setting it back to 1 with the line var myFlag:int=1.

What you need to do, is move your myFlag var and it's initial value somewhere up the scope of your if statement.

Since you don't say where the code posted is running (main timeline? enter frame handler? mouse down handler? movie clip timeline?), it's hard to specifically help.

If it's the main timeline, then the code will only run once anyway so there is little point in having a flag.

If it's a mouse or enter frame event handler, then move var myFlag:int=1 to the main timeline and out of that event handler.


EDIT

Based off your comment, you just need to remove your button once it's clicked. See the code comments

s1.addEventListener(MouseEvent.MOUSE_UP,dars1,false,0,true); //best to use a few more parameters and make it a weak listener
function dars1(e:MouseEvent):void
{
    //load you swf
    var darsRequest:URLRequest = new URLRequest("dars1.swf");
    var darsLoader:Loader = new Loader();
    darsLoader.load(darsRequest);
    addChild(darsLoader);

    if(s1.parent) s1.parent.removeChild(s1); //if you want the button totally gone from the stage

    //or if your gotoAndStop(25) does something along the lines of not showing the button, keep that:

    s1.gotoAndStop(25);
    s1.mouseEnabled = false;
    s1.mouseChildren - false; //you might need this too

    //or remove the listener so the button doesn't dispatch a mouse up anymore
    s1.removeEventListener(MouseEvent.MOUSE_UP, dars1,false);
}
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40