2013-03-27 120 views
0

需要通过单击事件来完成几件事情。我是一个初学者,所以有没有其他的方式来编写这段代码?通过点击这个按钮,它会进入下一个框架,根据陈述,几个按钮将会显示或不显示。我用这种方式编写代码,它说有语法错误,但我找不到任何代码。希望你们明白这一点,并会帮助我。 :) 谢谢!操作脚本3.0 if else语句

review_btn.addEventListener(MouseEvent.CLICK, review1) 
function review1(event:MouseEvent):void{ 

if(rvw1 == "Correct"){ 
    gotoAndStop(3627); 
    help1.visible = false 

    } 
else{ 
    gotoAndStop(3627); 
    help1.visible = true 

} 
} 

review_btn.addEventListener(MouseEvent.CLICK, review2) 
function review2(event:MouseEvent):void{ 

if(rvw2 == "Correct"){ 
    gotoAndStop(3627); 
    help2.visible = false 

    } 
else{ 
    gotoAndStop(3627); 
    help2.visible = true 

} 
} 

review_btn.addEventListener(MouseEvent.CLICK, review3) 
function review3(event:MouseEvent):void{ 

if(rvw3 == "Correct"){ 
    gotoAndStop(3627); 
    help3.visible = false 

    } 
else{ 
    gotoAndStop(3627); 
    help3.visible = true 

} 
} 

review_btn.addEventListener(MouseEvent.CLICK, review4) 
function review4(event:MouseEvent):void{ 

if(rvw4 == "Correct"){ 
    gotoAndStop(3627); 
    help4.visible = false 

    } 
else{ 
    gotoAndStop(3627); 
    help4.visible = true 

} 
} 

review_btn.addEventListener(MouseEvent.CLICK, review5) 
function review5(event:MouseEvent):void{ 

if(rvw5 == "Correct"){ 
    gotoAndStop(3627); 
    help5.visible = false 

    } 
else{ 
    gotoAndStop(3627); 
    help5.visible = true 

} 
} 
+0

您至少应该检查一下是否首先键入某个操作符。如果没有,或者你看不到它,那么将错误报告添加到问题中。 – Vesper 2013-03-27 08:58:01

+0

实际上没有编译错误,在输出选项卡中显示语法错误... 是否有反正写这个代码? – user2214782 2013-03-27 09:19:18

+2

如果它写入语法错误,它还会写入错误的位置。检查它和它之前和之后的行,是否有任何类型的未关闭或额外的闭括号等。关于代码 - 是的,它可以被简化,因为您将侦听器添加到单个按钮。下面的答案有一些简化。 – Vesper 2013-03-27 09:49:45

回答

1

我会尝试一下。看起来唯一的区别在于,在每种方法中,您需要匹配“helpX”.visible和“rvwX”等于字符串“Correct”,其中X是1-5的数字。无论如何,gotoAndStop()帧都是一样的。此外,所有五个都意味着脱离同一个按钮。我将假设剪辑的“帮助”是在舞台上定义的电影剪辑,如果它们来自别的东西,我会将它们存储在数组中以便循环使用,而不是“构建”名称并通过这种方式找到参考只是为了清楚。

function review(event:MouseEvent):void { 
    for(var counter:int = 1; counter < 6; counter++){ 
     this["help" + counter].visible = (this["rvw" + counter] != "Correct"); 
    } 
    this.gotoAndStop(3627); 
} 
review_btn.addEventListener(MouseEvent.CLICK, review); 
+0

如果rvwX不等于正确的helpX的不应该是可见的...并且这不符合你的上述代码。 :/ 我是代码世界的初学者,每次都搞乱代码:p:D btw thanx您的合作。 如果您有时间,请考虑下面的例子重新编写代码。 :D if(rvwX ==“Correct”) gotoAndStop(X); helpX.visible = true else gotoAndStop(X); helpX.visible = false 非常感谢您为我浪费宝贵的时间。 :) – user2214782 2013-03-27 10:30:22

+0

哎呀,我的错。如果字符串等于“正确”,正如您所指出的那样,我已经获得了帮助X的可见性,但应该是相反的。编辑我的代码来解决这个问题。 – mitim 2013-03-27 21:43:46

0

我认为你必须做2个字段的类:“帮助”和“rvw”(让我称之为“Switcher”)。此外,它可能包含设置知名度的功能(可能,不是必须,这个功能也可以在你的主类):

Switcher.as:

import flash.display.MovieClip; 

public class Switcher { 
    private var help:MovieClip; 
    private var rvw:String; 

    public function setVisibility() { 
     help.visible = !(rvw == "Correct"); 
    } 
} 

然后,你必须做出切换的数组在你的主类和对象只使用一个“审查”处理程序:

function review(event:MouseEvent):void { 
    for each(var sw:Switcher in switchersArray) { 
     sw.setVisibility(); 
    } 
    this.gotoAndStop(3627); 
} 

从以前的答案的代码将正常工作,但恕我直言,创建类似对象的数组(或向量)比做很多更好help1,help2,help3等变量。