pic_mc.addEventListener(MouseEvent.CLICK, onToOrig );
a_btn.addEventListener(MouseEvent.CLICK, onToA );
b_btn.addEventListener(MouseEvent.CLICK, onToB );
c_btn.addEventListener(MouseEvent.CLICK, onToC );
function onToOrig(evt:MouseEvent) {
TweenLite.to(pic_mc, 0.5, {x:homeX, y:homeY, ease:Cubic.easeInOut});
}
function onToA(evt:MouseEvent) {
TweenLite.to(pic_mc, 0.5, {x:pic1X, y:pic1Y, ease:Cubic.easeInOut});
}
function onToB(evt:MouseEvent) {
TweenLite.to(pic_mc, 0.5, {x:pic2X, y:pic2Y, ease:Cubic.easeInOut});
}
function onToC(evt:MouseEvent) {
TweenLite.to(pic_mc, 0.5, {x:pic3X, y:pic3Y, ease:Cubic.easeInOut});
}
代码优化一:
上面的代码中,事件处理函数中的内容几乎相同,可以考虑用同一个函数来实现,switch 结构终于可以派上用场了。
代码修改如下:
import gs.TweenLite;
import gs.easing.*;
var homeX = 230;
var homeY = 170;
var pic1X = 641;
var pic1Y = 479.6;
var pic2X = 352.8;
var pic2Y =-150.3;
var pic3X = -196.4;
var pic3Y = 178.6;
pic_mc.addEventListener(MouseEvent.CLICK, onMovePic );
a_btn.addEventListener(MouseEvent.CLICK, onMovePic );
b_btn.addEventListener(MouseEvent.CLICK, onMovePic );
c_btn.addEventListener(MouseEvent.CLICK, onMovePic );
function onMovePic(evt:MouseEvent) {
var destX,destY:Number;
switch ( evt.target ) {
case a_btn :
destX = pic1X;
destY = pic1Y;
break;
case b_btn :
destX = pic2X;
destY = pic2Y;
break;
case c_btn :
destX = pic3X;
destY = pic3Y;
break;
default :
destX = homeX;
destY = homeY;
}
TweenLite.to(pic_mc, 0.5, {x:destX, y:destY, ease:Cubic.easeInOut});
}
代码优化二:
原本想将位置信息直接保存到按钮中去,改成如下的形式,无奈按钮组件不能直接添加新属性,这个尝试算是失败了。(解决此问题的办法是继承按钮类,在子类中增加新属性,时间关系暂不去做这一步
import gs.TweenLite;
import gs.easing.*;
pic_mc.destX = 230;
pic_mc.destY = 170;
a_btn.destX = 641;//系统会报错,不能给组件动态添加新内容。
a_btn.destY = 479.6;
b_btn.destX = 352.8;
b_btn.destY =-150.3;
c_btn.destX = -196.4;
c_btn.destY = 178.6;
pic_mc.addEventListener(MouseEvent.CLICK, onMovePic );
a_btn.addEventListener(MouseEvent.CLICK, onMovePic );
b_btn.addEventListener(MouseEvent.CLICK, onMovePic );
c_btn.addEventListener(MouseEvent.CLICK, onMovePic );
function onMovePic(evt:MouseEvent) {
var destX = evt.target.destX;
var destY = evt.target.destY;
TweenLite.to(pic_mc, 0.5, {x:destX, y:destY, ease:Cubic.easeInOut});
}
优化三:空空的主背景有些不大好看,加上个新背景实例名为 bg_mc,并加入让背景淡入淡出的效果,
代码如下:
import gs.TweenLite;
import gs.easing.*;
var homeX = 230;
var homeY = 170;
var pic1X = 641;
var pic1Y = 479.6;
var pic2X = 352.8;
var pic2Y =-150.3;
var pic3X = -196.4;
var pic3Y = 178.6;
pic_mc.addEventListener(MouseEvent.CLICK, onMovePic );
a_btn.addEventListener(MouseEvent.CLICK, onMovePic );
b_btn.addEventListener(MouseEvent.CLICK, onMovePic );
c_btn.addEventListener(MouseEvent.CLICK, onMovePic );
function onMovePic(evt:MouseEvent) {
var destX,destY:Number;
switch ( evt.target ) {
case a_btn :
destX = pic1X;
destY = pic1Y;
break;
case b_btn :
destX = pic2X;
destY = pic2Y;
break;
case c_btn :
destX = pic3X;
destY = pic3Y;
break;
default :
destX = homeX;
destY = homeY;
TweenLite.to(pic_mc, 0.5, {x:destX, y:destY, ease:Cubic.easeInOut});
TweenLite.to( bg_mc, 0.5, {alpha:1, ease:Cubic.easeIn});
return;
}
TweenLite.to(pic_mc, 0.5, {x:destX, y:destY, ease:Cubic.easeInOut});
TweenLite.to( bg_mc, 0.5, {alpha:0, ease:Cubic.easeOut});
}
完整的效果如下:
点击按钮显示相关图片,点击图片回到初始位置,显示背景图:
相关文章:
TweenFilterLite 参数说明 中文翻译
http://blog.5d.cn/user12/dzxz/200809/500545.html
TweenMax 参数说明 中文翻译
http://blog.5d.cn/user12/dzxz/200809/500547.html
TweenLite 参数说明 中文翻译
http://blog.5d.cn/user12/dzxz/200808/497831.html
TweenLite 的一个应用
http://blog.5d.cn/user12/dzxz/200808/497862.html
个性签名 时钟版
http://blog.5d.cn/user12/dzxz/200810/504465.html