学习 Flash 中的 ActionScript 2.0 |
|
|
|
| 用 ActionScript 创建交互操作 > 创建交互性和视觉效果 > 创建声音控件 | |||
可以使用内置 Sound 类控制 SWF 文件中的声音。若要使用 Sound 类的方法,必须先创建一个 Sound 对象。然后可以使用 attachSound() 方法在 SWF 文件运行时将库中的声音插入该 SWF 文件。
若要查看声音控件的动画演示,可单击"播放"按钮,然后调节音量和左右平衡。
Sound 类的 setVolume() 方法控制着音量,而 setPan() 方法则调节声音的左右平衡。
以下过程演示如何创建声音控制。
将声音附加到时间轴上: a_thousand_ways。play_btn。 stop_btn。 将以下代码添加到"动作"面板中:
var song_sound:Sound = new Sound();
song_sound.attachSound("a_thousand_ways");
play_btn.onRelease = function() {
song_sound.start();
};
stop_btn.onRelease = function() {
song_sound.stop();
};
该代码首先停止扬声器影片剪辑。然后创建一个新的 Sound 对象 (song_sound),并向该对象附加链接标识符为 a_thousand_ways 的声音。与 playButton 和 stopButton 对象关联的 onRelease 事件处理函数通过使用 Sound.start() 和 Sound.stop() 方法启动和停止该声音,并且还播放和停止附加的声音。
创建滑动音量控件: 选择影片剪辑行为时要小心。这将创建一个在第一帧中带有按钮的影片剪辑。
this.createTextField("volume_txt", 10, 30, 30, 200, 20);
volume_mc.top = volume_mc._y;
volume_mc.bottom = volume_mc._y;
volume_mc.left = volume_mc._x;
volume_mc.right = volume_mc._x + 100;
volume_mc._x += 100;
volume_mc.handle_btn.onPress = function() {
startDrag(this._parent, false, this._parent.left, this._parent.top, this._parent.right, this._parent.bottom);
};
volume_mc.handle_btn.onRelease = function() {
stopDrag();
var level:Number = Math.ceil(this._parent._x - this._parent.left);
this._parent._parent.song_sound.setVolume(level);
this._parent._parent.volume_txt.text = level;
};
volume_mc.handle_btn.onReleaseOutside = slider_mc.handle_btn.onRelease;
startDrag() 参数 left、top、right 和 bottom 是在影片剪辑动作中设置的变量。
创建滑动的平衡控件: 选择影片剪辑行为时要小心。这将创建一个在第一帧中带有按钮的影片剪辑。
balance_mc.top = balance_mc._y;
balance_mc.bottom = balance_mc._y;
balance_mc.left = balance_mc._x;
balance_mc.right = balance_mc._x + 100;
balance_mc._x += 50;
balance_mc.handle_btn.onPress = function() {
startDrag(this._parent, false, this._parent.left, this._parent.top, this._parent.right, this._parent.bottom);
};
balance_mc.handle_btn.onRelease = function() {
stopDrag();
var level:Number = Math.ceil((this._parent._x - this._parent.left - 50) * 2);
this._parent._parent.song_sound.setPan(level);
};
balance_mc.handle_btn.onReleaseOutside = balance_mc.handle_btn.onRelease;
startDrag() 参数 left、top、right 和 bottom 是在影片剪辑动作中设置的变量。
有关 Sound 类的方法的更多信息,请参见"ActionScript 2.0 语言参考"中的 Sound。
|
|
|
|