Main.ts
var music = new Music;
this.addChild(music)
this.once(egret.TouchEvent.TOUCH_BEGIN,function(){
music.stop();
music.play();
},this)
====
Music.ts
class Music extends egret.Sprite{
private musicUrl = "resource/assets/music.mp3"
private pic = new egret.Bitmap(RES.getRes('m'));
private sound: egret.Sound
private channel: egret.SoundChannel
private _pauseTime = 0;
public constructor() {
super();
this.touchEnabled = true
this.x = 900;
this.y = 100;
this.pic.anchorOffsetX = 50;
this.pic.anchorOffsetY = 50;
this.pic.width = 100;
this.pic.height = 100;
this.addChild(this.pic);
this.sound = new egret.Sound;
this.sound.addEventListener(egret.Event.COMPLETE,function(){
this.play();
},this)
this.sound.load(this.musicUrl);
this.addEventListener(egret.TouchEvent.TOUCH_TAP,function(){
if(this.channel){
this.stop();
}else{
this.play();
}
},this)
egret.Tween.get(this.pic,{
loop: true
}).to({
rotation: 360
},3000)
egret.Tween.pauseTweens(this.pic);
}
public play(){
this.channel = this.sound.play(this._pauseTime,1)
egret.Tween.resumeTweens(this.pic);
}
public stop(){
egret.Tween.pauseTweens(this.pic);
this._pauseTime = this.channel.position;
this.channel.stop();
this.channel = null;
}
}