class Dzwiek extends Phaser.State {
    preload() {
        this.game.load.audio('music', 'sound/AKKA.mp3');
        this.game.load.image('tlo', 'IMAGEE/TLO2.bmp');
        this.game.load.image('play', 'IMAGEE/PLAY.bmp');
        this.game.load.image('pauza', 'IMAGEE/PAUZA.bmp');
        this.game.load.image('resume', 'IMAGEE/RESUME.bmp');
        this.game.load.image('stop', 'IMAGEE/STOP.bmp');
    }

    create() {
        var tablicaSprite = [];
        var tlo = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY, 'tlo');
        tlo.anchor.setTo(0.5, 0.5);
        tablicaSprite[0] = this.game.add.sprite(100, 400, 'play');
        tablicaSprite[0].anchor.setTo(0.5, 0.5);
        tablicaSprite[1] = this.game.add.sprite(700, 400, 'pauza');
        tablicaSprite[1].anchor.setTo(0.5, 0.5);
        tablicaSprite[2] = this.game.add.sprite(400, 400, 'resume');
        tablicaSprite[2].anchor.setTo(0.5, 0.5);
        tablicaSprite[3] = this.game.add.sprite(400, 500, 'play');
        tablicaSprite[3].anchor.setTo(0.5, 0.5);
        tablicaSprite[4] = this.game.add.sprite(700, 500, 'stop');
        tablicaSprite[4].anchor.setTo(0.5, 0.5);

        tablicaSprite[0].inputEnabled = true;
        tablicaSprite[0].input.useHandCursor = true;
        tablicaSprite[0].events.onInputDown.add(play, this);

        tablicaSprite[1].inputEnabled = true;
        tablicaSprite[1].input.useHandCursor = true;
        tablicaSprite[1].events.onInputDown.add(pause, this);

        tablicaSprite[2].inputEnabled = true;
        tablicaSprite[2].input.useHandCursor = true;
        tablicaSprite[2].events.onInputDown.add(resume, this);

        tablicaSprite[3].inputEnabled = true;
        tablicaSprite[3].input.useHandCursor = true;
        tablicaSprite[3].events.onInputDown.add(play_15, this);

        tablicaSprite[4].inputEnabled = true;
        tablicaSprite[4].input.useHandCursor = true;
        tablicaSprite[4].events.onInputDown.add(stop, this);

        var muzyka = this.game.add.audio('music');

        function play(event, sprite) {
            if(!muzyka.isPlaying && !muzyka.paused)
            muzyka.play('', 0, 0.5, false, true);
        }
        function play_15(event, sprite) {
            if (!muzyka.isPlaying && !muzyka.paused) {
                muzyka.addMarker('1', 1, 2.5, 1, true);
                muzyka.play('1');
            }   
        }
        function pause(event, sprite) {
            if(muzyka.isPlaying)
            muzyka.pause();
        }
        function resume(event, sprite) {
            if(muzyka.paused)
            muzyka.resume();
        }
        function stop(event, sprite) {
            if (muzyka.isPlaying || muzyka.paused)
                muzyka.stop();
        }
    }
}