como colocar a função dormir em javascript?

user6434012

Estou criando um jogo em html5 usando javascript e estou tendo problemas com a função dormir. Quero suspender uma função isWin por alguns segundos antes de mostrar um pop-up declarando que a pessoa ganhou o jogo. aqui está parte do código.

modal: function (isWin) {
	var bgDarker = this.game.make.bitmapData(this.game.width, this.game.height);
	bgDarker.fill(50, 50, 50);
	bgDarker = this.game.add.button(0, 0, bgDarker, function () { }, this);
	bgDarker.tint = 0x000000;
	bgDarker.alpha = 0.5;

	var modalGroup = this.game.add.group();

	var bg = this.game.add.sprite(this.game.world.centerX, this.game.world.centerY - 40,  (isWin ? "picWinPopup" : "picLostPopup"));
	bg.scale.setTo(1);
	bg.anchor.setTo(0.5);
	modalGroup.add(bg);
	
	var labelLevel2 = this.game.add.text(this.game.world.centerX + 100, this.game.world.centerY + 5, MatchingGame.indexImage, {
		font: "48px " + MatchingGame.fontFaces[3],
		fill: "#1a40ff",
		boundsAlignH: "center",
		boundsAlignV: "middle"
	});
	labelLevel2.anchor.setTo(0.5);
	modalGroup.add(labelLevel2);
    
	var labelTotalScore = this.game.add.text(this.game.world.centerX, this.game.world.centerY + 110, MatchingGame.score, {
		font: "34px " + MatchingGame.fontFaces[3],
		fill: "#ff2a5c",
		boundsAlignH: "center",
		boundsAlignV: "middle"
	});
	labelTotalScore.anchor.setTo(0.5);
	modalGroup.add(labelTotalScore);
	
	var btnReplay = this.makeButton("btnReplay", function (close) {
		btnReplay.bgDarker.destroy();
		btnReplay.modalGroup.destroy();
		if(MatchingGame.indexImage==6){
			MatchingGame.indexImage = 1;
		}
		this.game.stateTransition.to('Game');
    	this.imgLive1.visible = true;
    	this.imgLive2.visible = true;
    	this.imgLive3.visible = true;
    	MatchingGame.live = 3;
    	MatchingGame.levelscore = 0;
	},this);
    //btnReplay.anchor.setTo(0.5);
	btnReplay.position.setTo(bg.position.x - 200, bg.position.y + 260)
	btnReplay.bgDarker = bgDarker;
	btnReplay.modalGroup = modalGroup;
	modalGroup.add(btnReplay);
	
	if (!isWin) {
		var btnEnd = this.makeButton("btnEnd", function (close) {
			btnEnd.bgDarker.destroy();
			btnEnd.modalGroup.destroy();
			
        	this.imgLive1.visible = true;
        	this.imgLive2.visible = true;
        	this.imgLive3.visible = true;
        	MatchingGame.live = 3;
        	
        	this.end();
		},this);
		btnEnd.position.setTo(bg.position.x + 200, bg.position.y + 260)
		btnEnd.bgDarker = bgDarker;
		btnEnd.modalGroup = modalGroup;
		modalGroup.add(btnEnd);
    }
    else {
    	//setTimeout(3000);
    	//var TimeHandler = setTimeout(function(){clearTimeout(TimeHandler);},3000);

    	var btnNextLevel = this.makeButton("btnNextLevel", function (close) {
			btnNextLevel.bgDarker.destroy();
			btnNextLevel.modalGroup.destroy();
			if(MatchingGame.indexImage==6){
				MatchingGame.indexImage = 1;
			} else {
				MatchingGame.indexImage = MatchingGame.indexImage+1;																	}
			this.game.stateTransition.to('Game');
        	this.imgLive1.visible = true;
        	this.imgLive2.visible = true;
        	this.imgLive3.visible = true;
        	MatchingGame.live = 3;
		},this);
		btnNextLevel.position.setTo(bg.position.x + 200, bg.position.y + 260)
		btnNextLevel.bgDarker = bgDarker;
		btnNextLevel.modalGroup = modalGroup;
		modalGroup.add(btnNextLevel); 
    }
    
    MatchingGame.live = 3;
	
	this.game.add.tween(modalGroup).from({ y: -800 }, 600, Phaser.Easing.Bounce.Out, true);
	this.game.world.bringToTop(modalGroup);
}, 

Eu tentei usar a função sleep () e setTimeout (), mas continuo falhando em implementá-la em javascript. você pode me ajudar a resolver esse problema de colocar um temporizador na função javascript? porque estou tendo problemas para integrar setTimeout no código javascript que usa html5.

kawadhiya21

Use assim

setTimeout(function() {
var btnNextLevel = this.makeButton("btnNextLevel", function (close) {
                btnNextLevel.bgDarker.destroy();
                btnNextLevel.modalGroup.destroy();
                if(MatchingGame.indexImage==6){
                    MatchingGame.indexImage = 1;
                } else {
                    MatchingGame.indexImage = MatchingGame.indexImage+1;                                                                    }
                this.game.stateTransition.to('Game');
                this.imgLive1.visible = true;
                this.imgLive2.visible = true;
                this.imgLive3.visible = true;
                MatchingGame.live = 3;
            },this);
            btnNextLevel.position.setTo(bg.position.x + 200, bg.position.y + 260)
            btnNextLevel.bgDarker = bgDarker;
            btnNextLevel.modalGroup = modalGroup;
            modalGroup.add(btnNextLevel); }, 3000 );

Este artigo é coletado da Internet.

Se houver alguma infração, entre em [email protected] Delete.

editar em
0

deixe-me dizer algumas palavras

0comentários
loginDepois de participar da revisão

Artigos relacionados

como colocar a função laravel echo em javascript

Função dormir em javascript / node.js

Como ter várias funções com a função dormir em execução?

Como faço para dormir a cada N iterações em um loop de Javascript?

Como colocar um ator para dormir?

Como colocar a coordenação gerada em Javascript em formato html?

Como colocar uma função com dois parâmetros em um Array (JavaScript)?

Como colocar uma função com dois parâmetros em um Array (JavaScript)?

Como colocar uma função como argumento em C?

Como colocar o código PHP em JavaScript?

Como dormir ou atrasar o tópico em Haskell?

Como faço para colocar o título de uma frase em javascript usando a função de substring simples?

Knockout: Como colocar $ .get em uma função?

Python Como posso colocar loops For aninhados em uma função?

Como colocar a resposta da função .then () em module.exports?

Como colocar args em uma função onreadystatechange

Como colocar minha função em um thread Qt Concurrent

Se a declaração junto com o Swing não funcionar como esperado, como colocar o EDT para dormir?

como posso colocar a função input () em uma função de definição em python 3

Como colocar arquivo PHP em JavaScript?

como colocar outros dados json em javascript

Como colocar javascript em arquivo html

função dormir em um script em lote

Como colocar o vetor numérico em função em vez de caractere em R?

A função dormir não está funcionando como esperado

A função dormir não está funcionando como esperado

Como posso colocar uma função matemática em uma função python?

Como colocar em fila a impressão de várias seções do arquivo HTML em javascript?

como podemos colocar em escala de cinza uma imagem padrão usada em canvas (javascript)