如何判断按钮是否在不同的关键帧中被按下?

How to tell if a button was pressed in a different key-frame?

我是 Flash 开发的新手,我正在努力做到当我在游戏中按下模式按钮时,它会改变障碍物的速度。问题在于障碍物在不同的关键帧中,而速度代码在影片剪辑本身中。它如何告诉不同关键帧中的障碍物它们的速度应该是多少??

您需要在用户单击模式按钮时设置一个变量。

    var speed:Number = 5;

    modeButton.onRelease = function() {
        speed = 10;
    }

然后在稍后为您的障碍引用该变量...

    trace("speed: " + speed);

这是一个超级简单的示例,希望对您有所帮助!

在动画片段中声明的变量在定义它们之后的任何帧中都可用(即使当它循环回到第 1 帧时它们仍然可用,即使它们没有在第 1 帧上声明,尽管最佳做法是在第一帧中声明)。

所以基本上...您的问题不应该真的存在。只需将您的数据存储在第 1 帧上声明的变量中。您的问题存在的唯一方法是,如果您只在函数本身内部拥有该变量,那么它仅对该函数是本地的……无论影片剪辑如何,这都是一个问题,并且关键帧。

只是不要那样做。在函数外声明变量,在函数内使用。

// put this on frame 1
var speed = 1; // this will be accessible anywhere in the clip

// put this wherever your button appears (I assume on frame 1 as well)
yourBtnName.onRelease = function() {
    speed = 1.5; // the change will be refleced anywhere that uses it
}

// put this on some other frame and call it at any point
function doSomething() {
    trace(speed); // before clicking the button this will trace 1, after 1.5
}