我正在尝试在Flash 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");
}
答案 0 :(得分:0)
执行函数后删除事件侦听器:
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);
}
答案 1 :(得分:0)
考虑前两行:
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
var及其初始值移至if语句范围内的某处。
由于你没有说明发布的代码在哪里运行(主要时间线?输入帧处理程序?鼠标按下处理程序?影片剪辑时间表?),因此很难特别提供帮助。
如果它是主时间轴,那么代码只会运行一次,所以没有标记。
如果它是鼠标或输入框架事件处理程序,则将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);
}