>我正在尝试在闪存AS3中执行此代码,但它无法正常工作。在我的If
条件的身体中,我设置了myFlag = 2
,但if
条件始终是 True!!
这是我的代码:
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");
}
考虑你的前两行:
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
{
即使您在 if 语句中设置 myFlag,下次运行此代码块时,您只需要使用行 var myFlag:int=1
将其设置回 1 即可。
你需要做的是将你的myFlag
变量和它的初始值移动到你的if语句范围的某个地方。
由于您没有说明发布的代码在哪里运行(主时间线?输入帧处理程序?鼠标按下处理程序?影片剪辑时间轴?),因此很难特别提供帮助。
如果是主时间线,那么代码无论如何只会运行一次,所以有一个标志没有什么意义。
如果是鼠标或 enter 帧事件处理程序,则var myFlag:int=1
移动到主时间线并移出该事件处理程序。
编辑
根据您的评论,您只需要在单击按钮后将其删除即可。 查看代码注释
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);
}
执行事件侦听器的功能后将其删除:
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);
}