flex_圆球滚动动画代码;

来源:互联网 发布:手机淘宝店铺首页模块 编辑:程序博客网 时间:2024/04/19 04:43

效果:圆红球在浏览器窗体内来回弹动;

=>App.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  xmlns:s="library://ns.adobe.com/flex/spark"
  xmlns:mx="library://ns.adobe.com/flex/mx"
  xmlns:aspackage="aspackage.*"
  minWidth="955" minHeight="600" pageTitle="TheStudioOfCenyebao">
<s:layout>
<s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/>
</s:layout>

<fx:Script>
<![CDATA[

]]>
</fx:Script>


<fx:Declarations>
<!-- 非可视元素  -->
</fx:Declarations>


<s:VGroup width="100%" height="100%">
<aspackage:MyComponent id="myComponent"/>
</s:VGroup>
</s:Application>


=>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 shunShiZhen:Boolean=false;
/*顺时针*/
private var niShiZhen:Boolean=false;
/*计时器*/
private var timer:Timer;
/*alpha值*/
private var alphaValue:int = 5;



/**
* 构造函数
*/
public function MyComponent()
{
/**
* 绘制圆*/
circle=new Sprite();
circle.graphics.beginFill(0x990000);
circle.graphics.drawCircle(50, 50, 50);
circle.graphics.endFill();
circle.useHandCursor=true;
circle.buttonMode=true;
this.addChild(circle);


/*圆_侦听单击事件*/
circle.addEventListener(MouseEvent.CLICK, startAnimationFn);

timer = new Timer(50, 6);
timer.addEventListener(TimerEvent.TIMER, onTimerHandler);
}

/**
* 计时函数
* @param event
*/
private function onTimerHandler(event:TimerEvent):void
{
circle.alpha = alphaValue/10;
if(alphaValue>=10) {
alphaValue = 5;
}else {
alphaValue++;
}
}


/**
* 开始产生动画效果;
* @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);


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) {
leftClick = true;
topClick = false;
rightClick = false;
bottomClick = false;

circle.alpha = 0.5;
if(timer.running) {
timer.reset();
}
timer.start();
}
if (leftClick)
{ // 左边;

if(leftClick && shunShiZhen) {
circle.x += 5;
circle.y -= 2;
}else if(leftClick) {
circle.x += 5;
circle.y += 2;
}

hasReset(circle);
}


if(circle.y <= 0) {
leftClick = false;
topClick = true;
rightClick = false;
bottomClick = false;

circle.alpha = 0.5;
if(timer.running) {
timer.reset();
}
timer.start();
}
if (topClick)
{ // 上边;
if(shunShiZhen) {
circle.x += 3;
circle.y += 4;
}else if(niShiZhen) {
circle.x -= 3;
circle.y += 4;
}

hasReset(circle);
}


/**
* 右边*/
if(circle.x >= initWidth) {
if(leftClick) {
shunShiZhen = true;
}

leftClick = false;
topClick = false;
rightClick = true;
bottomClick = false;

circle.alpha = 0.5;
if(timer.running) {
timer.reset();
}
timer.start();
}
if (rightClick)
{ // 右边;
if(shunShiZhen) {//顺时针;
circle.x -= 5;
circle.y += 3;
}else if(niShiZhen) {
circle.x -= 5;
circle.y -= 3;
}

hasReset(circle);
}


if(circle.y >= initHeight) {
if(leftClick) {// 逆时针;
niShiZhen = true;
}

leftClick = false;
topClick = false;
rightClick = false;
bottomClick = true;

circle.alpha = 0.5;
if(timer.running) {
timer.reset();
}
timer.start();
}
if (bottomClick)
{ // 下边;
if(shunShiZhen){
circle.x -= 4;
circle.y -= 3;
}else if(niShiZhen) {
circle.x += 4;
circle.y -= 3;
}

hasReset(circle);
}
}

/**
* 判断是否需要重新设置显示对象的坐标;
* @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;
}
}
}
}


<!--完美版本-->

=>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, 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;
}
}
}
}

原创粉丝点击