缩短函数代码并转换变量

发布于 2024-07-13 22:49:05 字数 2152 浏览 8 评论 0原文

我有这个 AS2 代码,当我使用 TweenLite 翻转 mc 时,它会执行一些简单的动画。

我感觉我有很多重复的代码。

有没有办法能够只指定像这样的函数

boxLink(a);

并拥有其余的代码,并将目标影片剪辑的路径放在函数中而不是函数变量中?

我怎样才能使这段代码尽可能短?

是否可以将变量:String 转换为变量:MovieClip? 在这个例子中我该怎么做?

 import gs.*;
 import gs.easing.*;


function imageAlpha(mc_target:MovieClip) {
    mc_target.onRollOver = function() {
            TweenLite.to(mc_target,1,{_alpha:100, ease:Back.easeOut});
    };

    mc_target.onRollOut = function() {
            TweenLite.to(mc_target,1,{_alpha:60, ease:Back.easeOut});
    };
 }



  function boxLink(mc_function:MovieClip, mc_target:MovieClip, mc_image:MovieClip, linkURL:String) {
    mc_function.onRollOver = function() {
            TweenLite.to(mc_target,0.5,{_xscale:150, _yscale:150, ease:Back.easeOut});
            TweenLite.to(mc_image,1,{_alpha:100, ease:Back.easeOut});
    };

    mc_function.onRollOut = function() {
            TweenLite.to(mc_target,0.5,{_xscale:100, _yscale:100, ease:Back.easeOut});
            TweenLite.to(mc_image,1,{_alpha:60, ease:Back.easeOut});
    };

    mc_function.onRelease = function() {
            if (linkURL) {
            getURL(linkURL);
            }
    };
 }



 imageAlpha(a_box.image);
 imageAlpha(b_box.image);
 imageAlpha(c_box.image);
 imageAlpha(d_box.image);

 boxLink(a_box.link1,a_box.arrow1,a_box.image,'http://www.google.no');
 boxLink(a_box.link2,a_box.arrow2,a_box.image,'http://www.google.no');
 boxLink(a_box.link3,a_box.arrow3,a_box.image,'http://www.google.no');

 boxLink(b_box.link1,b_box.arrow1,b_box.image,'http://www.google.no');
 boxLink(b_box.link2,b_box.arrow2,b_box.image,'http://www.google.no');
 boxLink(b_box.link3,b_box.arrow3,b_box.image,'http://www.google.no');

 boxLink(c_box.link1,c_box.arrow1,c_box.image,'http://www.google.no');
 boxLink(c_box.link2,c_box.arrow2,c_box.image,'http://www.google.no');
 boxLink(c_box.link3,c_box.arrow3,c_box.image);

 boxLink(d_box.link1,d_box.arrow1,d_box.image,'http://www.google.no');
 boxLink(d_box.link2,d_box.arrow2,d_box.image,'http://www.google.no');
 boxLink(d_box.link3,d_box.arrow3,d_box.image);

I have this AS2 code that does some simple animations when I rollover a mc using TweenLite.

I feel that I have a lot of repeating code.

Is there a way to be able to just specify a function like this

boxLink(a);

and have the rest of the code, with the path to the target movieclip in the function instead of in the function variable?

How can I make this code as short as possible?

Is it possible to convert a variable:String to a variable:MovieClip?
How would i do that in this example?

 import gs.*;
 import gs.easing.*;


function imageAlpha(mc_target:MovieClip) {
    mc_target.onRollOver = function() {
            TweenLite.to(mc_target,1,{_alpha:100, ease:Back.easeOut});
    };

    mc_target.onRollOut = function() {
            TweenLite.to(mc_target,1,{_alpha:60, ease:Back.easeOut});
    };
 }



  function boxLink(mc_function:MovieClip, mc_target:MovieClip, mc_image:MovieClip, linkURL:String) {
    mc_function.onRollOver = function() {
            TweenLite.to(mc_target,0.5,{_xscale:150, _yscale:150, ease:Back.easeOut});
            TweenLite.to(mc_image,1,{_alpha:100, ease:Back.easeOut});
    };

    mc_function.onRollOut = function() {
            TweenLite.to(mc_target,0.5,{_xscale:100, _yscale:100, ease:Back.easeOut});
            TweenLite.to(mc_image,1,{_alpha:60, ease:Back.easeOut});
    };

    mc_function.onRelease = function() {
            if (linkURL) {
            getURL(linkURL);
            }
    };
 }



 imageAlpha(a_box.image);
 imageAlpha(b_box.image);
 imageAlpha(c_box.image);
 imageAlpha(d_box.image);

 boxLink(a_box.link1,a_box.arrow1,a_box.image,'http://www.google.no');
 boxLink(a_box.link2,a_box.arrow2,a_box.image,'http://www.google.no');
 boxLink(a_box.link3,a_box.arrow3,a_box.image,'http://www.google.no');

 boxLink(b_box.link1,b_box.arrow1,b_box.image,'http://www.google.no');
 boxLink(b_box.link2,b_box.arrow2,b_box.image,'http://www.google.no');
 boxLink(b_box.link3,b_box.arrow3,b_box.image,'http://www.google.no');

 boxLink(c_box.link1,c_box.arrow1,c_box.image,'http://www.google.no');
 boxLink(c_box.link2,c_box.arrow2,c_box.image,'http://www.google.no');
 boxLink(c_box.link3,c_box.arrow3,c_box.image);

 boxLink(d_box.link1,d_box.arrow1,d_box.image,'http://www.google.no');
 boxLink(d_box.link2,d_box.arrow2,d_box.image,'http://www.google.no');
 boxLink(d_box.link3,d_box.arrow3,d_box.image);

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

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

发布评论

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

评论(1

独夜无伴 2024-07-20 22:49:05

使用数组代替命名变量

a_box.link_1, a_box.link_2, ..., a_box.link_3 

对于箭头来说也是

a_box.link[ i ] where i = 0 to n-1, n = number of links

类似的。

接下来,为 boxLink() 创建一个包装器。

public function 
boxLinkWrapper(x:BoxType, url:string='http://www.google.no') :ReturnType {
    for (i:int = 0; i < x.link.size; ++i) {
        boxLink(x.link[ i ], x.arrow[ i ], x.image, url);
    }
}

此外,由于您有许多这样的框,因此请将它们放入一个数组中:

var boxes:Array = new Array();
boxes.push(new Box("a")); //a_box
boxes.push(new Box("b")); //b_box

并包装每个框所需的步骤:

public function 
processBox(x:BoxType):SomeReturnType {
    imageAlpha(d_box.image);
    boxLink(a_box.link1,a_box.arrow1,a_box.image,'http://www.google.no');
}

然后运行 ​​for - 遍历数组框,或使用 forEach() 成员函数:

boxes.forEach(processBox);

不要忘记用适当的类型替换 BoxType、SomeReturnType 和 ReturnType。
这有帮助吗? (注意:这是未经测试的代码,但应该可以帮助您入门!)

Use arrays instead of named variables

a_box.link_1, a_box.link_2, ..., a_box.link_3 

becomes

a_box.link[ i ] where i = 0 to n-1, n = number of links

Similarly for arrows.

Next, create a wrapper for boxLink()

public function 
boxLinkWrapper(x:BoxType, url:string='http://www.google.no') :ReturnType {
    for (i:int = 0; i < x.link.size; ++i) {
        boxLink(x.link[ i ], x.arrow[ i ], x.image, url);
    }
}

Also, since you have a number of such boxes, put them in an array:

var boxes:Array = new Array();
boxes.push(new Box("a")); //a_box
boxes.push(new Box("b")); //b_box

and wrap up the steps you'd need for each box:

public function 
processBox(x:BoxType):SomeReturnType {
    imageAlpha(d_box.image);
    boxLink(a_box.link1,a_box.arrow1,a_box.image,'http://www.google.no');
}

and run a for-loop over the array boxes, or use the forEach() member function:

boxes.forEach(processBox);

Don't forget to replace BoxType, SomeReturnType and ReturnType by the appropriate types.
Does that help? (Note: this is untested code, but should get you started!)

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