Flash 中的ActionScript实例 下载本文

}

} } }

实例2:鼠标跟随效果

在鼠标指针的箭头处会不断地冒出大量的白色圆点,这些圆点会随机向不同的方向扩散并逐渐消失。

Math.random()方法:

可用于返回一个伪随机数n,其中0<=n<1。 例如,下面的代码返回的值大于等于0,小于2: Math.randowm()*2;

第1帧上加代码: i=1;

_root.circle.onEnterFrame = function() { this._x=_root._xmouse;

this._y=_root._ymouse;

this._rotation=Math.floor(Math.random()*360); this.duplicateMovieClip(\i++;

}

在circle元件内部做一个运动部件动画,在其第40帧删除自己。 This.removeMovieClip();

3.6时间和日期

知识点:Date对象

Date()构造函数:用于创造一个新Date对象,用于保存指定的日期和时间。用法: My_time=new Date(); Date对象的方法: Date.getMonth()、Date.getDay()(获取星期几0代表星期日,1代表星期一)、Date.getDate(获取月中的某天1-31)、Date.getHours()(0-23小时)、Date.getMinutes()(获得秒0-59)、Date.getSeconds()。

实例1:数字显示时间

用上述Date对象的方法获取时间,用动态文本显示。

_root.onEnterFrame=function(){

clock=new Date(); second=clock.getSeconds(); minute=clock.getMinutes(); hours=clock.getHours(); if(String(hours).length==1){ hours=\ }

Time=hours+\

13

}

实例2:传统时钟

_root.onEnterFrame=function(){ clock=new Date(); second=clock.getSeconds();

minute=clock.getMinutes(); hours=clock.getHours();

arms.sec._rotation=second*6;

arms.minu._rotation=minute*6+second/10; arms.hour._rotation=hours*30+minute/2; }

3.6随机复制

知识点:碰撞检测myMovieClip.hitTest(x, y, shapeFlag)

shapeFlag 一个布尔值,指定是计算指定实例的整个形状 (true) 还是仅计算边框 (false)。只有当用 x 和 y 坐标参数标识点击区域时,才可以指定该参数。

x,y既可以是鼠标的坐标,也可以其他影片剪辑的坐标。

数学函数计算方法:Math.sin()、Math.cos() 实例:落雪效果

onClipEvent (enterFrame) { if(this._name==\ this._x=Math.random()*550; this.duplicateMovieClip(\ i++; }else {

if(_root.block.hitTest(this._x,this._y,true)){ time++; if(time>100){

this.removeMovieClip(); }

}else { this._y+=speed;

sinn+=wavespeed; xpos+=wind;

this._x=xpos+Math.sin(sinn)*10; if(this._y>400||this._x>550||this._x<0){ this.removeMovieClip();

}

}

}

14

}

onClipEvent (load) { var i=1; var wavespeed=(0.2+Math.random())/10; var speed=3+Math.random()*2; }

var wind=0.5; var xpos=this._x; var time=0; var sinn=0;

this._alpha=30+Math.random()*70;

3.7声音控制

知识点:声音的控制相关函数和方法

? Sound() 构造函数

此构造函数可以用来指定的影片剪辑创建新的Sound对象。 my_sound=new Sound(目标);

? Sound.attachSound方法

此方法用于将指定的声音附加到指定的Sound对象。该声音必须位于当前影片的库中,并且必须已经在“链接属性”对话框中指定导出。必须调用Sound.start()才开始播放此声音。

my_sound.attachSound(“id名称”) ? Sound.start方法

此方法用于从开头开始播放(如果未指定声音的播放开始点)最后附加的声音,或者从指定的声音处开始播放。其用法为:

my_sound.start(秒偏移量,循环);

秒偏移量:可选参数,通过该参数可以指定从特点开始播放声音。 ? Sound.stop方法

此方法用于停止当前播放的声音,或者只停止播放指定的声音。其用法为: my_sound.stop(“id名称”);

id名称:可选参数,指定特定声音停止播放。 ? Sound.setVolume方法 用来设置音量。其用法为:

my_sound.setVolume(音量);

音量:一个0-100之间的数字,表示音量级别。100为最大音量,而0为没有音量。 ?

Sound.position属性

该属性为声音已播放的毫秒数。

? Sound.onSoundComplete处理函数 声音播放完时自动调用。其用法:

mysound.onSoundComplete = function() { 程序体

} 实例:“音频播放器.fla”

15

startDrag(target,[lock ,left ,top ,right,bottom]) 参数

target 要拖动的影片剪辑的目标路径。 lock 一个布尔值,指定可拖动影片剪辑是锁定到鼠标位置中央 (true),还是锁定到用户首次点击该影片剪辑的位置上 (false)。此参数是可选的。

left、top、right、bottom 相对于影片剪辑父级坐标的值,这些坐标指定该影片剪辑的约束矩形。这些参数是可选的。

mysound=new Sound();

mysound.attachSound(\starttime=0;

pause_btn.enabled=false; stop_btn.enabled=false; //调整音量用变量

linewidth=_root.bar._width-_root.block._width; blockleft=_root.bar._x; //调整音量用变量

blockright=_root.bar._x+linewidth; play_btn.onRelease=function(){ play_btn.enabled=false;

pause_btn.enabled=true;

stop_btn.enabled=true; mysound.start(starttime); mysound.onSoundComplete = function() { starttime=0; } }

pause_btn.onRelease=function(){ play_btn.enabled=true;

pause_btn.enabled=false;

stop_btn.enabled=true; mysound.stop(); starttime=Math.floor(mysound.position/1000);

}

stop_btn.onRelease=function(){ play_btn.enabled=true;

pause_btn.enabled=false;

stop_btn.enabled=false; mysound.stop(); starttime=0;

}

//调整音量

line.onEnterFrame=function(){

16

this._width=_root.block._x-_root.bar._x;

mysound.setVolume(Math.round(this._width/linewidth*100)); }

block.onPress=function(){ this.startDrag(false,blockleft,this._y,blockright,this._y); }

block.onRelease=function(){

this.stopDrag() } block.onReleaseOutside = function() {

this.stopDrag()

}

17