[ActionScript 3.0] 模拟win7彩色气泡屏保效果

主文件:

package
{
    import com.views.BubbleView;
    import com.views.ColorfulBubble;

    import flash.display.Sprite;
    import flash.display.StageDisplayState;
    import flash.events.Event;

    /**
     * @author Frost.Yen
     * @E-mail [email protected]
     * @create 2016-12-28 下午2:08:51
     *
     */
    [SWF(width="1920",height="1080",backgroundColor="0x000000")]
    public class BubbleCollision extends Sprite
    {

        private var _bubbleView:BubbleView;
        public function BubbleCollision()
        {
            if(stage){
                init(null);
            }else{
                addEventListener(Event.ADDED_TO_STAGE,init);
            }
        }
        private function init(e:Event):void
        {
            removeEventListener(Event.ADDED_TO_STAGE,init);
            stage.displayState = StageDisplayState.FULL_SCREEN;
            _bubbleView = new BubbleView();
            this.addChild(_bubbleView);
            _bubbleView.start();
        }

    }
}

气泡运动控制类:

package com.views
{
    import flash.display.DisplayObject;
    import flash.display.Sprite;
    import flash.events.Event;

    /**
     * @author Frost.Yen
     * @E-mail [email protected]
     * @create 2016-12-28 下午3:01:15
     *
     */
    public class BubbleView extends Sprite
    {

        private var _bubbles:Array=[];
        private var _checkBubbles:Array=[];
        public var viewWidth:Number = 1920;
        public var viewHeight:Number = 1080;
        //弹性
        public var bo:Number = -0.6;
        public var len:int = 16;
        public function BubbleView()
        {
            super();
        }
        private function initBubbles():void
        {
            for (var i:int = 0; i < len; i++)
            {
                var bubble:ColorfulBubble = new ColorfulBubble();
                bubble.name = "bubble_" + i;
                bubble.color = rgb();
                bubble.radius = 129*0.5;
                this.addChild(bubble);
                _bubbles.push(bubble);
            }

        }
        /**
         * 开始运动
         */
        public function start():void
        {
            initBubbles();
            rank(this);
            addEventListener(Event.ENTER_FRAME, enterFrame);
        }
        /**
         * 随机排列,不重叠
         * @param parent 父容器
         */
        private function rank(parent:Sprite):void{
            for(var j:int = 0;j<parent.numChildren;j++){
                setPosition(parent.getChildAt(j),parent);
            }
        }

        /**
         * 递归调用,设置对象随机排列不重叠
         * @param obj 显示对象
         * @param parent 父级
         * @param 对象之间的间距
         */
        private function setPosition(obj:DisplayObject,parent:Sprite,dis:Number = 20):void
        {
            obj.x = Math.random()*(viewWidth-obj.width) ;
            obj.y = Math.random()*(viewHeight-obj.height) ;
            for(var i:int = 0;i<parent.numChildren;i++){
                if(obj != parent.getChildAt(i)){
                    //不重叠,没有间距
                    /*if(obj.hitTestObject(parent.getChildAt(i))){
                    setPosition(obj);
                    return;
                    }*/
                    //不重叠,并有一定间距dis,间距为0效果同上
                    if(Math.abs(obj.x-parent.getChildAt(i).x)<obj.width+dis&&Math.abs(obj.y-parent.getChildAt(i).y)<obj.height+dis){
                        setPosition(obj,parent);
                        return;
                    }
                }
            }
        }
        private function enterFrame(evt:Event):void
        {
            var c:int = _bubbles.length;
            for (var i:int = 0; i < c; i++)
            {
                var bubble:ColorfulBubble = _bubbles[i];

                move(bubble);

                if (bubble.isHold)
                {
                    var res:Boolean = false;
                    for (var j:int = i+1; j < c; j++)
                    {
                        var bubble1:ColorfulBubble = _bubbles[j];
                        var dx:Number = bubble1.x - bubble.x;
                        var dy:Number = bubble1.y - bubble.y;
                        var dis:Number = Math.sqrt(dx * dx + dy * dy);
                        if (dis < bubble.radius + bubble1.radius)
                        {
                            res = true;
                            break;
                        }
                    }
                    bubble.isHold = res;
                }
                else
                {
                    for (j = i + 1; j < c; j++)
                    {
                        checkTouch(_bubbles[i], _bubbles[j]);
                    }
                }
                bubble.addEventListener(Event.CONNECT,onConnect);
            }

        }
        private function onConnect(event:Event):void
        {
            var num:int = event.target.name.split("_")[1];
            this.dispatchEvent(new Event(Event.CONNECT));
        }
        /**
         * 泡泡运动边界控制
         */
        private function move(bubble:ColorfulBubble):void
        {

            var radius:Number = bubble.radius;

            if (bubble.x + radius > viewWidth)
            {
                bubble.dragStop();
                bubble.x = viewWidth - radius;
                bubble.vx *= bo;
            }
            else if (bubble.x - radius < 0)
            {
                bubble.dragStop();
                bubble.x = radius;
                bubble.vx *= bo;
            }

            if (bubble.y + radius > viewHeight)
            {
                bubble.dragStop();
                bubble.y = viewHeight - radius;
                bubble.vy *= bo;
            }
            else if (bubble.y - radius < 0)
            {
                bubble.dragStop();
                bubble.y = radius;
                bubble.vy *= bo;
            }
        }
        /**
         * 检测两个彩色泡泡是否有碰撞
         */
        private function checkTouch(bubble0:ColorfulBubble, bubble1:ColorfulBubble):void
        {

            var dx:Number = bubble1.x - bubble0.x;
            var dy:Number = bubble1.y - bubble0.y;
            var dis:Number = Math.sqrt(dx * dx + dy * dy);
            if (dis < bubble0.radius + bubble1.radius)
            {
                var angle:Number = Math.atan2(dy, dx);
                var sin:Number = Math.sin(angle);
                var cos:Number = Math.cos(angle);

                var x0:Number = 0;
                var y0:Number = 0;

                var x1:Number = dx * cos + dy * sin;
                var y1:Number = dy * cos - dx * sin;

                var vx0:Number = bubble0.vx * cos + bubble0.vy * sin;
                var vy0:Number = bubble0.vy * cos - bubble0.vx * sin;

                var vx1:Number = bubble1.vx * cos + bubble1.vy * sin;
                var vy1:Number = bubble1.vy * cos - bubble1.vx * sin;

                var vxTotal:Number = vx0 - vx1;
                vx0 = ((bubble0.mass - bubble1.mass) + vx0 + 2 * bubble1.mass * vx1) / (bubble0.mass + bubble1.mass);
                vx1 = vxTotal + vx0;

                ////////////
                //x0 += vx0;
                //x1 += vx1;
                var absV:Number = (vx0 < 0? -vx0 : vx0) + (vx1 < 0? -vx1 : vx1);

                var xd:Number = x0 - x1;
                xd = xd < 0? -xd : xd;

                var overlap:Number = (bubble0.radius + bubble1.radius) - xd;

                x0 += vx0 / absV * overlap;
                x1 += vx1 / absV * overlap;                

                ////////////

                var x0F:Number = x0 * cos - y0 * sin;
                var y0F:Number = y0 * cos + x0 * sin;

                var x1F:Number = x1 * cos - y1 * sin;
                var y1F:Number = y1 * cos + x1 * sin;

                bubble1.x = bubble0.x + x1F;
                bubble1.y = bubble0.y + y1F;
                bubble0.x = bubble0.x + x0F;
                bubble0.y = bubble0.y + y0F;

                bubble0.vx = vx0 * cos - vy0 * sin;
                bubble0.vy = vy0 * cos + vx0 * sin;
                bubble1.vx = vx1 * cos - vy1 * sin;
                bubble1.vy = vy1 * cos + vx1 * sin;

                bubble0.dragStop();
                bubble1.dragStop();
            }
        }
        private function rgb():uint {
            return (Math.random() * 0xffffff);
        }
    }
}

单个气泡控制类

package com.views
{
    import com.res.BubbleRes;

    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.geom.ColorTransform;

    /**
     * @author Frost.Yen
     * @E-mail [email protected]
     * @create 2016-12-28 下午2:12:09
     *
     */
    public class ColorfulBubble extends Sprite
    {
        private var _bubble:BubbleRes;//彩色泡泡元件
        private var _downX:Number;
        private var _downY:Number;
        /**
         * 摩擦力
         */
        private var _fr:Number = 0.99;
        private var _radius:Number = 129;
        private var _tmpx:Number;
        private var _tmpy:Number;
        private var _ct:ColorTransform = new ColorTransform();
        private var _color:uint = 0xff00ff;
        public var vx:Number = 0;
        public var vy:Number = 0;
        public var ax:Number = 0;
        public var ay:Number = 0;
        public var max_speed:Number = 1;
        /**
         * 质量
         */
        public var mass:Number = 50;
        public var isHold:Boolean = true;
        public var move:Function;
        public function ColorfulBubble()
        {
            initViews();
            initEventListeners();

        }
        private function initViews():void
        {
            _bubble = new BubbleRes();
            this.addChild(_bubble);
            ax = getRandom();
            ay = getRandom();
            move = usualMove;

        }
        private function initEventListeners():void
        {
            _bubble.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
            addEventListener(Event.ENTER_FRAME, enterFrame);
        }
        private function enterFrame(evt:Event):void
        {
            move();
        }

        private function onStartDrag(evt:MouseEvent):void
        {
            _downX=mouseX;
            _downY=mouseY;
            stage.addEventListener(MouseEvent.MOUSE_UP, onStageUp);

            vx = vy = 0;
            ax = ay = 0;

            _tmpx = stage.mouseX;
            _tmpy = stage.mouseY;

            move = dragMove;

            //this.startDrag();
        }
        private function onStageUp(evt:MouseEvent):void
        {
            if(Math.sqrt((mouseX-_downX)*(mouseX-_downX)+(mouseY-_downY)*(mouseY-_downY))<10)
            {
                this.dispatchEvent(new Event(Event.CONNECT));
            }else
            {

            }
            dragStop();
        }
        public function dragStop():void
        {
            if(stage)
            {
                stage.removeEventListener(MouseEvent.MOUSE_UP, onStageUp);
            }            

            move = usualMove;
        }
        private function usualMove():void
        {
            var box:Boolean = vx < 0;
            var boy:Boolean = vy < 0;                

            vx = box ? -vx : vx;
            vy = boy ? -vy : vy;

            vx *= _fr;
            vy *= _fr;

            //bubble.ax *= fr;
            //bubble.ay *= fr;

            if (vx < .1)
            {
                //vx = Math.random() * 8 - 4;
                ax = getRandom();
            }
            else if (vx > max_speed)
            {
                ax = 0;
            }

            if (vy < .1)
            {
                //vy = Math.random() * 8 - 4;
                ay = getRandom();
            }
            else if (vy > max_speed)
            {
                ay = 0;
            }                

            vx = (box ? -vx : vx) + ax;
            vy = (boy ? -vy : vy) + ay;    

            this.x += vx;
            this.y += vy;
        }
        private function dragMove():void
        {
            vx = stage.mouseX - _tmpx;
            vy = stage.mouseY - _tmpy;    

            _tmpx = stage.mouseX;
            _tmpy = stage.mouseY;

            this.x += vx;
            this.y += vy;

            vx *= .5;
            vy *= .5;
        }
        private function getRandom():Number
        {
            return Math.random() / (Math.random()*3-3) - .05;
        }

        public function set radius(r:Number):void
        {
            var scale:Number = r * 2 / _bubble.width;
            this.width *= scale;
            this.height *= scale;
            _radius = this.width / 2;
            this.cacheAsBitmap = true;
        }

        public function get radius():Number
        {
            return _radius;
        }
        private function rgb():uint {
            return (Math.random() * 0xffffff);
        }

        public function get color():uint
        {
            return _color;
        }

        public function set color(value:uint):void
        {
            _ct.color = value;
            _bubble.transform.colorTransform = _ct;
            _color = value;
        }

    }
}
时间: 2024-11-03 22:44:52

[ActionScript 3.0] 模拟win7彩色气泡屏保效果的相关文章

Winform中实现自定义屏保效果(附代码下载)

场景 效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建form窗体,窗体中设计布局如下,一个Panel中拖拽四个label,然后添加两个Button,再添加一个Timer控件. 在窗体的Load事件中将Label进行隐藏 private void Form1_Load(object sender, EventArgs e) { LabelVisible(fal

手机端实现fullPage——全屏滚动效果

封装了一个小插件模拟fullPage的全屏滚动效果,比较简单. 特点: 1.  纯js实现,小巧轻便. 2.  兼容性好.苹果.安卓都没问题,暂时没遇到问题机型. 缺点: 1.  仅封装了基础功能,HTML.css么有去封装.个人觉得不封装更方便大家使用. 接下来看看效果图: 相比效果大家都看到了,接下来看看代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title&

【Android】一段时间不操作弹出【屏保】效果

仿屏保效果 最近项目的一个小Demo,和大家分享一下,本篇文章主要展示的是一个仿屏保的效果,用户在一段时间不操作会自动弹出屏保页面,当处于屏保页面的时候,[两秒内连续]按两次键盘[回车键]就会退出屏保页面,效果图如下: 下面具体介绍一下实现过程: 一 .5秒不操作自动锁屏界面 这个界面非常简单,就一个TextView,直接贴代码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

IOS 屏保动画

前言 最近公司没项目,我们码农处于"农闲"的时期,主管说要我找个动画渲染的引擎,找了许久都没找合适的,于是就自己动手写了个自我感觉比较酷炫的屏保效果. 一.控制界面,这个界面没什么技术含量,主要作用是用来控制动画到底有多炫的,比如动画色彩的丰富度和动画速度的.温馨提示,按照提示填入相应的数字,我没有做越界的处理,你要是crash了请不要找我. - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [

ActionScript 3.0 for the Lunder Algorithm

package com.feiruo.Calendar.LunderCalendar { /* *@ClassName: package:com.feiruo.Calendar.LunderCalendar::LunarDate *@INTRO: Calculating lunar and solar terms and other settings. *@Author: feiruo *@Language: ActionScript 3.0 *@SDKVersion: Flash Player

在 Flash ActionScript 2.0 中调用 Javascript 方法

本篇文章由:http://xinpure.com/call-the-javascript-method-in-flash-actionscript-2-0/ 在 Flash ActionScript 2.0 中调用 Javascript 方法 最近在工作中,有个这样的需求: 要从 Flash ActionScript 2.0 中调用网页上的 Javascript 方法 这是一个关于 Flash 和 Javascript 交互的问题. 在 ActionScript 2.0 中调用外部 javasc

关于VS2010帮助文档的使用和VC6.0在Win7 64位下的使用

由于购置了新的电脑,安装的是Win7 64位的操作系统,这两天我在重新安装编程环境的时候遇到一些问题,现在都解决掉了,分享出来以供需要的人参考. 一.以前使用的是VS2008,从VC6到2008这么多年了一只使用的MSDN是带索引的本地帮助,到了现在使用VS2010了,发现VS2010采用的是Web版形式的帮助. 并且也没有了索引.书签等非常的不习惯. 后来找到一个名字为:"h3viewer"的工具,在前提:已经安装了VS2010帮助文档的情况下.安装h3viewer,运行后界面如下:

ActionScript 3.0日期与时间管理(Date类)

/*Flash中,Date类管理着日期和时间信息.要获取   当前日期,可以新建一个Date对象   */var now:Date =new Date();trace (now);     /*输出当前时间和日期,格式如:                  Sun Jul 27 19:45:57 GMT+0800 2014  */ /*以上代码中,Date对象中包括了当前的秒.分.小时.  星期数.日.月.年.时区等信息...如果仅需要返  回星期值和日期值,而不返回时间或时区,可以使用Date

Win7逼真的雨景屏保

00.精彩视图 逼真的雨景屏保win7.rar 屏幕下雨...... 逼真的雨景屏保win7.rar 百度云盘:http://pan.baidu.com/s/1hqIdhUO