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");
}
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);
}