Flash As3 静音按钮问题

发布于 2024-08-24 14:17:52 字数 957 浏览 7 评论 0原文

我正在尝试创建一个可以在不同场景中使用的 UI 影片剪辑。 它使用根范围中的变量来确定状态。

当我按下静音按钮时工作正常,但是当我尝试取消静音时事情会变得很奇怪。有时需要点击 2 次才能取消静音,有时需要点击更多次。看起来是随机的。 然而,静音似乎第一次起作用......

有什么想法吗?


Main Timeline: 

var mute:Boolean = false;
var playerName = "Fred";


function setMute(vol)
{
    var sTransform:SoundTransform = new SoundTransform(1,0);
    sTransform.volume = vol;
    SoundMixer.soundTransform = sTransform;
}

function toggleMuteBtn(event:Event)
{
    if (mute)
    {
        // Sound On, Mute Off
        mute = false;
        setMute(1);
        ui_mc.muteCross_mc.visible = false;
    }
    else
    {
        // Sound Off, Mute On
        mute = true;
        setMute(0);
        ui_mc.muteCross_mc.visible = true;
    }
}

ui_mc Action Script:

if (MovieClip(parent).mute == false)
{
    muteCross_mc.visible = false;
}

mute_btn.addEventListener(MouseEvent.CLICK, MovieClip(parent).toggleMuteBtn);

I am trying to create a UI movie clip that can be used across different scenes.
It uses variables from the root scope to determine states.

When i press the mute button is works fine, however when i try to un-mute things go weird. Sometimes it takes 2 clicks to unmute, sometimes more. It seems random.
Muting however seems to work first time..

Any ideas?


Main Timeline: 

var mute:Boolean = false;
var playerName = "Fred";


function setMute(vol)
{
    var sTransform:SoundTransform = new SoundTransform(1,0);
    sTransform.volume = vol;
    SoundMixer.soundTransform = sTransform;
}

function toggleMuteBtn(event:Event)
{
    if (mute)
    {
        // Sound On, Mute Off
        mute = false;
        setMute(1);
        ui_mc.muteCross_mc.visible = false;
    }
    else
    {
        // Sound Off, Mute On
        mute = true;
        setMute(0);
        ui_mc.muteCross_mc.visible = true;
    }
}

ui_mc Action Script:

if (MovieClip(parent).mute == false)
{
    muteCross_mc.visible = false;
}

mute_btn.addEventListener(MouseEvent.CLICK, MovieClip(parent).toggleMuteBtn);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

∞觅青森が 2024-08-31 14:17:52

我假设您的代码位于时间线的第一帧上。

我不会深入讨论有关 as3 中的实践的问题,但您最好使用文档类。这样代码只初始化一次。我猜当你在场景之间切换时,你会不断重新初始化,所以你最终将静音重置为 false。

要检查是否发生这种情况,只需向该脚本添加 trace("init") ,然后查看调用该脚本的频率。您还可以向您的toggleMuteBtn 函数添加跟踪以查看发生了什么静音变量是在您更改它之前。

顺便说一句,我很好奇为什么你要对父MovieClip(parent)进行类型转换,尽管它不会导致任何问题,但没有必要,因为你可以使用父级

I'm assuming that your code is on the first frame of your timeline.

I won't go into depth with the issues regarding that practice in as3, but you might be better off using a document class. That way the code is initialized only once. I'm guessing when you're switching between the scenes, you keep re-initializing, so you end up resetting mute to false.

To check if this is happening, just add a trace("init") to that script, and see how often you call that., you can also add a trace to your toggleMuteBtn function to see what the mute variable is before you change it.

on a side note, I'm curious why you're typecasting the parentMovieClip(parent) though it won't cause any problems, there is no need for it as you could just use parent

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文