来源:
中国IT实验室
作者:
somesun
2008-03-18/10:01
拖一个按钮在主场景中,实例名为_btn,在帧上写如下代码:
//载入外部声音
my_sound = new Sound();
my_sound.loadSound("http://61.134.25.99/images/upfile/2006-3/2006315131424.mp3", true);
//创建显示声音时间的动态文本
this.createTextField("time_txt", this.getNextHighestDepth(), 100, 100, 0, 0);
time_txt.autoSize = true;
//创建按钮上的动态文本
this.createTextField("btn_txt", this.getNextHighestDepth(), 0, 0, 0, 0);
btn_txt.autoSize = true;
btn_txt._x = _btn._x;
btn_txt._y = _btn._y;
btn_txt.selectable = false;
btn_txt.text = "暂停";
//按钮按下并释放时执行以下函数
_btn.onRelease = function() {
this.id = !this.id;
if (this.id) {
btn_txt.text = "播放";
my_sound.stop();
pausetime = my_sound.position/1000;
}
if (!this.id) {
btn_txt.text = "暂停";
my_sound.start(pausetime);
}
};
//运行每一帧时执行以下函数(显示声音的总时间和已播放的时间)
onEnterFrame = function () {
var totalseconds:Number = my_sound.duration/1000;
var minutes:Number = Math.floor(totalseconds/60);
var seconds = Math.floor(totalseconds)%60;
if (seconds<10) {
seconds = "0"+seconds;
}
var playedseconds:Number = my_sound.position/1000;
var minutesed:Number = Math.floor(playedseconds/60);
var secondsed = Math.floor(playedseconds)%60;
if (secondsed<10) {
secondsed = "0"+secondsed;
}
time_txt.text = "离家的孩子"+minutesed+":"+secondsed+"—"+minutes+":"+seconds+"(刘常制作)";
};
开始时声音停止,点击按钮播放,再点击按钮时暂停
同样也拖一按钮到主场景,实例名为_btn
my_sound = new Sound();
my_sound.loadSound("http://61.134.25.99/images/upfile/2006-3/2006315131424.mp3", true);
my_sound.stop();
this.createTextField("time_txt", this.getNextHighestDepth(), 100, 100, 0, 0);
time_txt.autoSize = true;
this.createTextField("btn_txt", this.getNextHighestDepth(), 0, 0, 0, 0);
btn_txt.autoSize = true;
btn_txt._x = _btn._x;
btn_txt._y = _btn._y;
btn_txt.selectable = false;
btn_txt.text = "播放";
_btn.onRelease = function() {
this.id = !this.id;
if (this.id) {
btn_txt.text = "暂停";
my_sound.start(pausetime);
}
if (!this.id) {
btn_txt.text = "播放";
my_sound.stop();
pausetime = my_sound.position/1000;
}
};
onEnterFrame = function () {
var totalseconds:Number = my_sound.duration/1000;
var minutes:Number = Math.floor(totalseconds/60);
var seconds = Math.floor(totalseconds)%60;
if (seconds<10) {
seconds = "0"+seconds;
}
var playedseconds:Number = my_sound.position/1000;
var minutesed:Number = Math.floor(playedseconds/60);
var secondsed = Math.floor(playedseconds)%60;
if (secondsed<10) {
secondsed = "0"+secondsed;
}
time_txt.text = "离家的孩子"+minutesed+":"+secondsed+"—"+minutes+":"+seconds+"(刘常制作)";
};
以上语句中比较难理解的是
_btn.onRelease = function() {
this.id = !this.id;
if (this.id) {
btn_txt.text = "播放";
my_sound.stop();
pausetime = my_sound.position/1000;
}
if (!this.id) {
btn_txt.text = "暂停";
my_sound.start(pausetime);
}
};
this.id理解成真,this.id=!this.id理解成把真传递给非真(即假)
如果还不理解,干脆把语句改写成如下:
var d:Number = 0;
_btn.onRelease = function() {
d++;
if (d%2 == 1) {//d除以2的余数为1,即奇数次点击按钮
btn_txt.text = "播放";
my_sound.stop();
pausetime = my_sound.position/1000;
}
if (d%2 == 0) {//d除以2的余数为0,即偶数次点击按钮
btn_txt.text = "暂停";
my_sound.start(pausetime);
}
};