根据百分比更改影片剪辑颜色

时间:2013-11-15 14:13:52

标签: actionscript-3

我有一个圆圈网格,总共25个,它们代表一个百分比。我将有一系列具有不同百分比的项目,因此我想创建一个模板,我只需输入一个数字(百分比)来改变那部分mcs的颜色。

为了改变颜色,我正在做以下事情:

addEventListener(Event.ENTER_FRAME, dot_changer);
function dot_changer(event:Event):void
{
    import fl.motion.Color;
    var c:Color = new Color();
    c.setTint(0xff0000, 0.8);
    this.dot1.transform.colorTransform = c;
    this.dot2.transform.colorTransform = c;
    this.dot3.transform.colorTransform = c;
    this.dot4.transform.colorTransform = c;
}

我使用enterFrame而不是按钮的原因是这些只需在没有任何用户输入或中断的情况下播放。

我正在考虑使用if语句检查保存百分比的var并为每个点mc分配一个截止值。例如,如果var值低于96

,则dot25只会改变颜色

必须有一种更简单或更合乎逻辑的方式。

1 个答案:

答案 0 :(得分:0)

你可以使用一个对象和一个for循环,并最大限度地减少整个事情:

package {
    import fl.motion.Color;
    public class QuickTint extends Color {
        public function QuickTint(tintColor:uint, tintMultiplier:Number) {
            setTint(tintColor, tintMultiplier);
        }
    }
}

// Do this once - place it outside your function
var thresholds:Object = {
    dot25: 96
};
var defaultThreshold:int = 50;
var colourDefault:QuickTint = new QuickTint(0xff0000, 0.8);
var colourThreshold:QuickTint = new QuickTint(0x0000ff, 0.8);

// Do this whenever you want to check the colours
var dotName:String;
var threshold:int;
for (var i:uint = 1; i <= 25; i++) {
    dotName = "dot" + i;
    threshold = (thresholds.hasOwnProperty(dotName) ? thresholds[dotName] : defaultThreshold);

    if (value >= threshold) {
        this[dotName].transform.colorTransform = colourThreshold;
    } else {
        this[dotName].transform.colorTransform = colourDefault;
    }
}

value需要成为您的价值。 colourThreshold如果高于您的阈值(例如96),则Color将为colourDefault,默认情况下Color将为{{1}}。{/ 1} >

相关问题