Skip to content

Instantly share code, notes, and snippets.

@riskers
Last active June 8, 2016 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riskers/eb728220e4679966a2a8cb4731d4730a to your computer and use it in GitHub Desktop.
Save riskers/eb728220e4679966a2a8cb4731d4730a to your computer and use it in GitHub Desktop.
egret-music

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;
	    
	}
	
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment