主文件:

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 871979853@qq.com
* @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 871979853@qq.com
* @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 871979853@qq.com
* @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;
} }
}

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

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

    场景 效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建form ...

  2. [ActionScript 3.0] AS3实现图像径向转旋效果

    原图    效果     import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Blen ...

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

    封装了一个小插件模拟fullPage的全屏滚动效果,比较简单. 特点: 1.  纯js实现,小巧轻便. 2.  兼容性好.苹果.安卓都没问题,暂时没遇到问题机型. 缺点: 1.  仅封装了基础功能,H ...

  4. 3D屏保: 彩色盘子

    一个彩色盘子的屏保 记得小时候在电视上看过一个科普节目,由多个颜色组成的盘子,如果快速旋转起来,会看上去是白色的.于是我就写了这个屏保程序,但发现在计算机上模拟并不是这样的. "RollPl ...

  5. 3D屏保:魔方2.0版本

    一个三维魔方的屏保软件,可支持2级到72级的魔方.启动后魔方会自动旋转,并最终回到初始状态.有很多人问我这是怎么做到的,用的什么解魔方的算法,其实我自己根本就不会玩魔方,别人用技巧解魔方,我这程序中用 ...

  6. ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调

    近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0,现把学习结果分享一下,希望对新手有帮助. 目录 ActionScript 3.0简介 Hello ...

  7. 开启macbook win7触控屏右键

    开启macbook win7触控屏右键,如下图

  8. ActionScript 3.0 for the Lunder Algorithm

    package com.feiruo.Calendar.LunderCalendar { /* *@ClassName: package:com.feiruo.Calendar.LunderCalen ...

  9. [转]ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调

    本文转自:http://www.cnblogs.com/artwl/p/3396330.html 近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0, ...

随机推荐

  1. java中将数字的字符串表示转化为数字

    int a = new Integer("1234").intValue() 或 int b = Integer.parseInt("1234") System ...

  2. Shiro01 功能点框图、架构图、身份认证逻辑、身份认证代码实现

    基本功能点 功能点框图 功能点说明 1.Authentication:身份认证/登录,验证用户是不是拥有相应的身份: 2.Authorization:授权,即权限验证,验证某个已认证的用户是否拥有某个 ...

  3. RemoteExt 远程验证

    public class RemoteExtAttribute : RemoteAttribute { private string _resourceKey; public RemoteExtAtt ...

  4. 关于简单的三层的简化(bll,dal,model)的封装这里全部都在一个文件主要在于明白意思

    using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace 封装泛型CRU ...

  5. 在Eclipse中使用Struts和Hibernate框架搭建Maven Web项目

    前言 学习使用Java还是2012年的事情,刚开始学习的Java的时候,使用的是MyEclipse工具和SSH框架.初学者适合使用MyEclipse,因为他将struts.Spring和Hiberna ...

  6. Windows sql语句正则匹配导出数据到本地 The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

    尝试使用 into outfile导出数据的时候出现错误: The MySQL server is running with the --secure-file-priv option so it c ...

  7. struct 和union的区别

    union ( 共用体):构造数据类型,也叫联合体  用途:使几个不同类型的变量共占一段内存(相互覆盖) struct ( 结构体 ):是一种构造类型 用途: 把不同的数据组合成一个整体——自定义数据 ...

  8. Android代码实现求和运算

    Android代码实现求和运算 实验要求: 用Android语言设计一个界面,点击某按钮实现求和运算. 代码实现 码云链接 核心代码 以上为求和按钮的代码截图,解析如图标注. 实验结果 当输入为空值时 ...

  9. MySQL数据库Query性能定位

    1.SQL前面加 EXPLAIN 定位到sql级别 各个属性的含义 id select查询的序列号 select_type select查询的类型,主要是区别普通查询和联合查询.子查询之类的复杂查询. ...

  10. (巴什博弈 sg函数入门1) Brave Game -- hdu -- 1846

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1846 首先来玩个游戏,引用杭电课件上的: (1) 玩家:2人:(2) 道具:23张扑克牌:(3) 规则: ...