-3

我正在编写一个 AE 表达式,它将根据同一图层上效果的关键帧值吐出一个数字。也就是说,如果为 1,则值为 100,如果为 2,则值为 101,如果为 3,则为 99,等等。这就是我的工作:

x = effect("Mouth")("Slider");

if (x == 7 || x == 11 || x == 16) {
103
} else if (x == 6 || x == 10 || x == 15 || x == 25 || x == 26){
102
} else if (x == 5 || x == 9 || x == 12 || x == 14 || x == 19 || x == 24 || x == 27 || x == 28){
101
} else {
100
}

当然有更优雅的方式来做到这一点?我试过写

if (x == 7 || 11 || 16)

但是告诉 After Effects X 绝对等于“这个”或“那个”只是让它假设它也等于“一切”。啊。

4

2 回答 2

0

这有点奇怪(可以说是优雅的?),但如果你只是在寻找更紧凑的东西,你可以这样做:

x = effect("Mouth")("Slider");

if (",7,11,16,".indexOf(","+x+",") != -1) {
 103
} else if (",6,10,15,25,26,".indexOf(","+x+",") != -1) {
 102
} else if (",5,9,12,14,19,24,27,28,".indexOf(","+x+",") != -1) {
 101
} else {
 100
}

但不要忘记两端的逗号!

想过用ECMA的array.indexOf(x),但是AE的JS表达式不支持。[编辑:错误地写了'offsetOf']

于 2013-12-23T19:08:45.437 回答
0

以下是您可以使用的另外两种方法。您可以进一步压缩此代码,但为了便于阅读,我将其展开。

对于较短的列表,您可以使用 switch/case 并让多个选项执行相同的代码。像这样:

thingToTest = effect("Mouth")("Slider");

switch (thingToTest) {
    case 7:
    case 11:
    case 16:
        result = 103;
        break;
    case 6:
    case 10:
    case 15:
    case 25:
    case 26:
        result = 102;
        break;
    case 5:
    case 9:
    case 12:
    case 14:
    case 19:
    case 24:
    case 27:
    case 28:
        result = 101;
        break; 
    default:
        result = 100;
        break;
}

问题是,如果您有很多可能的结果要检查,那可能会变得非常笨拙。在这种情况下,我会将每个结果案例的值设为一个数组并循环遍历它们。

thingToTest = effect("Mouth")("Slider");

mySet1 = [7, 11, 16];
mySet2 = [6, 10, 15, 25, 26];
mySet3 = [5, 9, 12, 14, 19, 24, 27, 28];

result = 100; // Default

foundIt = 0;

for (i = 0; i < mySet1.length; i++) {
    if (mySet1[i] == thingToTest) {
        result = 103;
        foundIt = 1;
        break;
    }
}

if(foundIt) {
    for (i = 0; i < mySet2.length; i++) {
        if (mySet2[i] == thingToTest) {
            result = 102;
            foundIt = 1;
            break;
        }
    }
}

if(foundIt) {
    for (i = 0; i < mySet3.length; i++) {
        if (mySet3[i] == thingToTest) {
            result = 101;
            foundIt = 1;
            break;
        }
    }
}

result;

如果已经找到匹配项,则将连续组放在条件语句中通过不迭代这些数组来略微提高性能。为了提高效率,首先测试最长的数字列表的内容是有意义的,因为它更有可能包含匹配项。

这些解决方案可能不那么紧凑,但绝对是可扩展的。

于 2015-02-04T20:52:14.667 回答