flex_圆球滚动动画代码;
效果:圆红球在浏览器窗体内来回弹动; =>App.mxml <?xml version="1.0" encoding="utf-8"?> =>MyComponent.as package aspackage <!--完美版本--> =>MyComponent .as package aspackage { import flash.display.DisplayObject; import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; import flash.events.TimerEvent; import flash.utils.Timer; import mx.core.UIComponent; public class MyComponent extends UIComponent { /** * 属性*/ /*圆*/ private var circle:Sprite; /*是否已经单击*/ private var hasClick:Boolean=false; /*初始宽度*/ private var initWidth:Number; /*初始高度*/ private var initHeight:Number; /*左边*/ private var leftClick:Boolean=true; /*上边*/ private var topClick:Boolean=false; /*右边*/ private var rightClick:Boolean=false; /*下边*/ private var bottomClick:Boolean=false; /*计时器*/ private var timer:Timer; /*alpha值*/ private var alphaValue:int=5; /*原接触点坐标*/ private var oldX:Number=0; private var oldY:Number=0; /*左出*/ private var leftLeave:Boolean=false; /*右出*/ private var rightLeave:Boolean=false; /*x增量*/ private var incrementX:Number = 0; /*y增量*/ private var incrementY:Number = 0; /** * 构造函数 */ public function MyComponent() { /** * 绘制圆*/ circle=new Sprite(); circle.graphics.beginFill(0x990000); circle.graphics.drawCircle(50,50); circle.graphics.endFill(); circle.useHandCursor=true; circle.buttonMode=true; this.addChild(circle); /*圆_侦听单击事件*/ circle.addEventListener(MouseEvent.CLICK,startAnimationFn); timer=new Timer(50,5); timer.addEventListener(TimerEvent.TIMER,onTimerHandler); } /** * 计时函数 * @param event */ private function onTimerHandler(event:TimerEvent):void { alphaValue++; circle.alpha=alphaValue / 10; if (alphaValue >= 10) { alphaValue=5; } } /** * 开始产生动画效果; * @param event */ private function startAnimationFn(event:MouseEvent):void { initWidth=circle.stage.width - circle.width; initHeight=circle.stage.height - circle.height; trace("=>舞台宽高_" + circle.stage.width + "_" + circle.stage.height); incrementX = Math.round(Math.random()*10); incrementY = Math.round(Math.random()*10); trace("=>incrementX_"+incrementX+"; incrementY_"+incrementY); if(timer.running) { timer.reset(); } if (!hasClick) { circle.addEventListener(Event.ENTER_FRAME,fadeCircleFn); hasClick=true; } } /** * 圆动画效果 * @param event * About_When this animation starts,this function is called every frame(每帧). * The change made by this function (updated to the screen every frame) is what causes the animation to occur. */ private function fadeCircleFn(event:Event):void { /** * 左边*/ if (circle.x <= 0) { startTimer(); if (circle.y < oldY) { //左出,既向上滚动; leftLeave=true; rightLeave=false; } else if (circle.y >= oldY) { leftLeave=false; rightLeave=true; } /** * 存储当前接触点坐标*/ oldX=circle.x; oldY=circle.y; leftClick=true; topClick=false; rightClick=false; bottomClick=false; } if (leftClick) { if (leftLeave) { circle.x+=incrementX; circle.y-=incrementY; } else if (rightLeave) { circle.x+=incrementX; circle.y+=incrementY; } } /** * 右边*/ if (circle.x >= initWidth) { startTimer(); if (circle.y >= oldY) { // 左出,既向下滚动; leftLeave=true; rightLeave=false; } else if (circle.y < oldY) { leftLeave=false; rightLeave=true; } /** * 存储当前接触点坐标*/ oldX=circle.x; oldY=circle.y; leftClick=false; topClick=false; rightClick=true; bottomClick=false; } if (rightClick) { if (leftLeave) { circle.x-=incrementX; circle.y+=incrementY; } else if (rightLeave) { circle.x-=incrementX; circle.y-=incrementY; } } /** * 下边*/ if (circle.y >= initHeight) { startTimer(); if (circle.x >= oldX) { leftLeave=false; rightLeave=true; } else if (circle.x < oldX) { // 左出,既向左滚动; leftLeave=true; rightLeave=false; } /** * 存储当前接触点坐标*/ oldX=circle.x; oldY=circle.y; leftClick=false; topClick=false; rightClick=false; bottomClick=true; } if (bottomClick) { if (leftLeave) { circle.x-=incrementX; circle.y-=incrementY; } else if (rightLeave) { circle.x+=incrementX; circle.y-=incrementY; } } /** * 上边*/ if (circle.y <= 0) { startTimer(); if (circle.x >= oldX) { // 左出,既向右滚动; leftLeave=true; rightLeave=false; } else if (circle.x <= oldX) { leftLeave=false; rightLeave=true; } /** * 存储当前接触点坐标*/ oldX=circle.x; oldY=circle.y; leftClick=false; topClick=true; rightClick=false; bottomClick=false; } if (topClick) { if(leftLeave) { circle.x+=incrementX; circle.y+=incrementY; }else if(rightLeave) { circle.x-=incrementX; circle.y+=incrementY; } } } /** * 开始计时 */ private function startTimer():void { circle.alpha=0.5; if (timer.running) { timer.reset(); } timer.start(); } /** * 判断是否需要重新设置显示对象的坐标; * @param obj */ private function hasReset(obj:DisplayObject):void { //if((circle.x == 0 && circle.y == initHeight) ||? //(circle.x == initWidth && circle.y == 0) || (circle.x == initWidth && circle.y == initHeight)) { //circle.x = 0; //circle.y = 0; //} if ((obj.x == 0 && obj.y == initHeight) || (obj.x == initWidth && obj.y == 0) || (obj.x == initWidth && obj.y == initHeight)) { obj.x=0; obj.y=0; } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |