【AS3代码】滚动的小球
2012-04-19 16:03
176 查看
文档类
package
{
import com.Boll;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.Event;
public class Main extends Sprite
{
//小球的范围内上下左右的极限边界值
private var left:uint = 43;
private var right:uint = 457;
private var top:uint = 43;
private var bottom:uint = 247;
public function Main():void
{
//舞台设为无缩放模式
stage.scaleMode = StageScaleMode.NO_SCALE;
init();
//5个黑色小球与5个白色小球绑定在一起
addEventListener(Event.ENTER_FRAME, bdong);
}
private function init():void
{
//生成5个黑色小球
for(var i:uint = 0; i<5; i++)
{
var blackball:Boll = new Boll(0x000000, 43);
this.addChildAt(blackball, i); //将小球添加到舞台(带上深度位置)
}
//生成5个白色小球(半径小于黑色球)
for(var t:uint = 0; t<5; t++)
{
var wball:Boll = new Boll(0xffffff, 40);
wball.x = 55 * (t+1); //小球横向定位
wball.y = 140; //小球纵向定位
wball.xspeed = Math.random() * 5 + 1; //横向移动速度(在1-6间随机)
wball.yspeed = t + 5; //纵向移动速度
this.addChildAt(wball, t+5); //白色小球深度之所以+5,是因为之前还有5个深度范围在0-4黑色小球
//白色小球运动的事件
wball.addEventListener(Event.ENTER_FRAME, wdong);
//白色小球附带黑色小球运动的事件
}
}
//白色小球运动的事件
private function wdong(evt:Event):void
{
evt.target.x += evt.target.xspeed; //小球每次横向运动的运动量
evt.target.y += evt.target.yspeed; //小球每次纵向运动的运动量
//如果小球碰到了边界时,反向运动
if(evt.target.x <= left || evt.target.x >= right)
{
evt.target.xspeed *= -1;
}
if(evt.target.y <= top || evt.target.y >= bottom)
{
evt.target.yspeed *= -1;
}
}
//5个黑色小球与5个白色小球绑定在一起
private function bdong(evt:Event):void
{
for(var h:int = 0; h < 5; h++)
{
getChildAt(h).x = getChildAt(h+5).x;
getChildAt(h).y = getChildAt(h+5).y;
}
}
}
}
生成小球类
package com
{
//x轴越大,向右运动。y轴越大,向下运动。
import flash.display.Shape;
public class Boll extends Shape
{
private var _xspeed:int; //小球横向运动变化量
private var _yspeed:int; //小球纵向运动变化量
//_color颜色,_r小球半径
public function Boll(_color:uint, _r:uint)
{
init(_color, _r);
}
private function init(_color:uint, _r:uint):void
{
//创建小球
this.graphics.beginFill(_color);
this.graphics.drawCircle(0,0,_r);
this.graphics.endFill();
}
public function get xspeed():int
{
return _xspeed;
}
public function set xspeed(n:int):void
{
_xspeed = n;
}
public function get yspeed():int
{
return _yspeed;
}
public function set yspeed(n:int):void
{
_yspeed = n;
}
}}
package
{
import com.Boll;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import flash.events.Event;
public class Main extends Sprite
{
//小球的范围内上下左右的极限边界值
private var left:uint = 43;
private var right:uint = 457;
private var top:uint = 43;
private var bottom:uint = 247;
public function Main():void
{
//舞台设为无缩放模式
stage.scaleMode = StageScaleMode.NO_SCALE;
init();
//5个黑色小球与5个白色小球绑定在一起
addEventListener(Event.ENTER_FRAME, bdong);
}
private function init():void
{
//生成5个黑色小球
for(var i:uint = 0; i<5; i++)
{
var blackball:Boll = new Boll(0x000000, 43);
this.addChildAt(blackball, i); //将小球添加到舞台(带上深度位置)
}
//生成5个白色小球(半径小于黑色球)
for(var t:uint = 0; t<5; t++)
{
var wball:Boll = new Boll(0xffffff, 40);
wball.x = 55 * (t+1); //小球横向定位
wball.y = 140; //小球纵向定位
wball.xspeed = Math.random() * 5 + 1; //横向移动速度(在1-6间随机)
wball.yspeed = t + 5; //纵向移动速度
this.addChildAt(wball, t+5); //白色小球深度之所以+5,是因为之前还有5个深度范围在0-4黑色小球
//白色小球运动的事件
wball.addEventListener(Event.ENTER_FRAME, wdong);
//白色小球附带黑色小球运动的事件
}
}
//白色小球运动的事件
private function wdong(evt:Event):void
{
evt.target.x += evt.target.xspeed; //小球每次横向运动的运动量
evt.target.y += evt.target.yspeed; //小球每次纵向运动的运动量
//如果小球碰到了边界时,反向运动
if(evt.target.x <= left || evt.target.x >= right)
{
evt.target.xspeed *= -1;
}
if(evt.target.y <= top || evt.target.y >= bottom)
{
evt.target.yspeed *= -1;
}
}
//5个黑色小球与5个白色小球绑定在一起
private function bdong(evt:Event):void
{
for(var h:int = 0; h < 5; h++)
{
getChildAt(h).x = getChildAt(h+5).x;
getChildAt(h).y = getChildAt(h+5).y;
}
}
}
}
生成小球类
package com
{
//x轴越大,向右运动。y轴越大,向下运动。
import flash.display.Shape;
public class Boll extends Shape
{
private var _xspeed:int; //小球横向运动变化量
private var _yspeed:int; //小球纵向运动变化量
//_color颜色,_r小球半径
public function Boll(_color:uint, _r:uint)
{
init(_color, _r);
}
private function init(_color:uint, _r:uint):void
{
//创建小球
this.graphics.beginFill(_color);
this.graphics.drawCircle(0,0,_r);
this.graphics.endFill();
}
public function get xspeed():int
{
return _xspeed;
}
public function set xspeed(n:int):void
{
_xspeed = n;
}
public function get yspeed():int
{
return _yspeed;
}
public function set yspeed(n:int):void
{
_yspeed = n;
}
}}
相关文章推荐
- 【AS3代码】小球拖拽(MouseEvent的方式 和 startDrag的方式)
- 初学UI之小球滚动代码
- 超简单 无缝滚动 漂浮广告 代码属性
- 不间断图片滚动js代码.真正兼容火狐ie
- jquery 页眉单行信息滚动显示实现思路及代码
- jquery 页眉单行信息滚动显示实现思路及代码
- 利用 StarUML 创建 AS3 代码
- 图片滚动代码
- 图片滚动从右向左滚动网页代码
- 对联式网页滚动代码
- 方块滚动代码
- Expression Design与Blend制作滚动的小球动画教程
- 粒子动画-躁动的小球代码全
- 代码实现屏幕滚动!
- 文字不断滚动代码(向上滚动)
- 网页制作--滚动新闻代码(用iframe即时更新网上的新闻)
- 图片无缝滚动代码(向左/向下/向上)
- 原生JS写的一个书架式的图片缩放滚动展示特效代码
- 滚动条代码大集合
- jQuery插件实现文字无缝向上滚动效果代码