[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 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彩色气泡屏保效果的更多相关文章
- Winform中实现自定义屏保效果(附代码下载)
场景 效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 获取编程相关电子书.教程推送与免费下载. 实现 新建form ...
- [ActionScript 3.0] AS3实现图像径向转旋效果
原图 效果 import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Blen ...
- 手机端实现fullPage——全屏滚动效果
封装了一个小插件模拟fullPage的全屏滚动效果,比较简单. 特点: 1. 纯js实现,小巧轻便. 2. 兼容性好.苹果.安卓都没问题,暂时没遇到问题机型. 缺点: 1. 仅封装了基础功能,H ...
- 3D屏保: 彩色盘子
一个彩色盘子的屏保 记得小时候在电视上看过一个科普节目,由多个颜色组成的盘子,如果快速旋转起来,会看上去是白色的.于是我就写了这个屏保程序,但发现在计算机上模拟并不是这样的. "RollPl ...
- 3D屏保:魔方2.0版本
一个三维魔方的屏保软件,可支持2级到72级的魔方.启动后魔方会自动旋转,并最终回到初始状态.有很多人问我这是怎么做到的,用的什么解魔方的算法,其实我自己根本就不会玩魔方,别人用技巧解魔方,我这程序中用 ...
- ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调
近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0,现把学习结果分享一下,希望对新手有帮助. 目录 ActionScript 3.0简介 Hello ...
- 开启macbook win7触控屏右键
开启macbook win7触控屏右键,如下图
- ActionScript 3.0 for the Lunder Algorithm
package com.feiruo.Calendar.LunderCalendar { /* *@ClassName: package:com.feiruo.Calendar.LunderCalen ...
- [转]ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调
本文转自:http://www.cnblogs.com/artwl/p/3396330.html 近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0, ...
随机推荐
- open File Browser in shell
[maxosx] open /usr/include [ubuntu] 发现三个,如下: xdg-open xxxx.pdf gnome-open . nautilus . 喜欢把它alias一下 . ...
- 服务级别协议(SLA)与运行水平协议(OLA)
服务级别协议(SLA)与运行水平协议(OLA): 服务级别管理和服务级别协议在国内已被广泛接受并成用.本文试图讨论服务级别协议(SLA)和运行水平协议(OLA)的异同. 1. SLA a.定义 服务级 ...
- ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2)
ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var mysql 启动不了 ERROR 2002 ( ...
- shiro 和spring集合 实现登录时输入验证码并校验(七)
编写实现验证码的主体实现类:CaptchaCode import java.util.UUID; import javax.servlet.http.HttpServletRequest; impor ...
- HttpClient详解
HttpClient:是一个接口 首先需要先创建一个DefaultHttpClient的实例 HttpClient httpClient=new DefaultHttpClient(); 发送GET请 ...
- qt5.7 安装
http://blog.csdn.net/liang19890820/article/details/53931813#安装-qt57 安装运行出错:qt vstool 指定qt安装路径 http:/ ...
- [SoapUI] 如何让gzip和chunked的response显示出来 [设置Accept-Encoding为deflate]
如果response的Content-Encoding是gzip或者Transfer-Encoding是chunked,在SoapUI里面是无法显示出来的. 解决办法:在Request的Header里 ...
- JSONResult引用某博客
http://www.cnblogs.com/JerryWang1991/archive/2013/03/08/2950457.html 最近开始用MVC做项目,在使用 JsonResult返回数据的 ...
- msys2 + clion安装所需的mingw64编译环境
pacman -S mingw-w64-x86_64-toolchain 输出结果为: $ pacman -S mingw-w64-x86_64-toolchain:: 在组 mingw-w64-x8 ...
- 数据挖掘分类算法之决策树(zz)
决策树(Decision tree) 决策树是以实例为基础的归纳学习算法. 它从一组无次序.无规则的元组中推理出决策树表示形式的分类规则.它采用自顶向下的递归方式,在决策树的内部结点进行属性值 ...