Čeká se na dokončení funkce přidělené časovači? – JavaScript, AJAX, jQuery – Fórum – Programujte.com
 x   TIP: Přetáhni ikonu na hlavní panel pro připnutí webu

Čeká se na dokončení funkce přidělené časovači? – JavaScript, AJAX, jQuery – Fórum – Programujte.comČeká se na dokončení funkce přidělené časovači? – JavaScript, AJAX, jQuery – Fórum – Programujte.com

 

Toto vlákno bylo označeno za vyřešené.
Matěj Andrle+1
Grafoman
22. 9. 2014   #1
-
0
-

Dobrý den,
potřebuji-li předpřipravit další kolo hry, mohu se spolehnout, že funkce pro přípravu tohoto kola zavolaná z funkce přidělené časovači bude moci v klidu nastavit nové hodnoty všem proměnným i za pomoci cyklů atp., přičemž do té doby nebude proveden žádný další tick? Či mám před začátkem této initializace raději časovač vypnout a pak znovu zapnout?
Děkuji.

Nahlásit jako SPAM
IP: 78.136.164.–
peter
~ Anonymní uživatel
4005 příspěvků
23. 9. 2014   #2
-
0
-

casovace mas dva. Jeden opakujici, druhy se vykona jednou. setTimeout, setinterval.

Nahlásit jako SPAM
IP: 2001:718:2601:1f7:2d89:49...–
Matěj Andrle+1
Grafoman
23. 9. 2014   #3
-
0
-

#2 peter
Nevím, kdy kolo skončí!

Nahlásit jako SPAM
IP: 78.136.162.–
peter
~ Anonymní uživatel
4005 příspěvků
23. 9. 2014   #4
-
0
-

Normalne to funguje tak, ze konec kola zavisi na kliknuti na tlacitko nebo vyprseni casoveho limitu a nebo nejakem kliknuti, treba polozeni posledni kosticku puzzle na spravne misto. Na tu udalost muzes navazat funkci nextLevel();

onclick = "hotovo(); nextLevel();"
onclick = "if (hotovo()==true) {nextLevel();}"
onclick = "hotovoNext();"
function hotovoNext() {if (hotovo()==true) {nextLevel();})
function hotovoNext() {var bool = hotovo(); if (bool==true) {nextLevel();})

Nevim, jak vypada tvuj kod. Takze to zkus popsat tak, aby tomu clovek rozumel, jak to ted vlastne funguje a jak to ma fungovat. Klidne i smyslenym kodem, neco jako:
 

start();
hrac1();
x = vyhral(hrac1);
if (x) {konec(hrac1); start();}
else {hrac2();}
x = vyhral(hrac2);
if (x) {konec(hrac2); start();}
else {hrac1();}
...

A ty bys treba chtel, aby se to opakovalo cyklem, dokud se nezaplni hraci plocha. 
Nebo mas jinou podminku, ktera urcuje, ze hra skoncila?
Nahlásit jako SPAM
IP: 2001:718:2601:1f7:2d89:49...–
peter
~ Anonymní uživatel
4005 příspěvků
23. 9. 2014   #5
-
0
-

Mozna se pokousis udelat neco takovehoto, jako vetsina zacatecniku, co nechapou casovace...

start();
setTimeout("kolo",1000)
setTimeout("kolo",1000)
konec();

Tak takhle nee.
 

function classHra()
{
this.start = function() {...}
this.startKolo = function {setTimeout("hra.kolo",1000);}
this.kolo = function() {... if (konec_kola) this.startKolo();}
this.konec = function() {...}
}

var hra = new classHra();
hra.start();


Cili, v casovaci je funkce, ktera ma na konci if-else rozhodovani, co ma delat. Bud ukonci hru a nebo spusti znovu funkci pro nove kolo.

Nahlásit jako SPAM
IP: 2001:718:2601:1f7:2d89:49...–
Matěj Andrle+1
Grafoman
23. 9. 2014   #6
-
0
-

#5 peter
Co to?

var
	x = 0,
	y = 0,
	canvas,
	lines = 0,
	level = 0,
	score = 0,
	run = false,
	levelElement,
	scoreElement,
	gameInterval,
	interval = 900,
	startLevel = 0,
	accelerated = false,
	actualTetromino = 0,
	rowScore = [ 40, 100, 300, 1200 ],
	tetromino =
	[
		[ "", "", "", "", "-24px 0px", "-24px 0px", "-24px 0px", "-24px 0px", "", "", "", "", "", "", "", "" ],
		[ "", "", "", "", "", "-48px 0px", "-48px 0px", "", "", "-48px 0px", "-48px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "", "-72px 0px", "", "", "-72px 0px", "-72px 0px", "-72px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "", "-96px 0px", "-96px 0px", "", "-96px 0px", "-96px 0px", "", "", "", "", "", "" ],
		[ "", "", "", "", "-120px 0px", "-120px 0px", "", "", "", "-120px 0px", "-120px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "-144px 0px", "", "", "", "-144px 0px", "-144px 0px", "-144px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "", "", "-168px 0px", "", "-168px 0px", "-168px 0px", "-168px 0px", "", "", "", "", "" ]
	];

window.onload = function()
{
	aside = document.getElementsByTagName("aside")[0];
	levelElement = document.getElementById("level");
	scoreElement = document.getElementById("score");
	canvas = document.getElementById("canvas");

	document.getElementById("newGame").onclick = newGame;

	pageY = window.innerHeight / 2 - 240;
	pageX = window.innerWidth / 2 - 170;
	canvas.style.left = pageX + "px";
	canvas.style.top = pageY + "px";
	aside.style.left = (pageX + 240) + "px";
	aside.style.top = pageY + "px";

	canvas.onkeydown = function(event)
	{
		switch((event || window.event).keyCode)
		{
			case 37:

				if(run && displayTetromino(x - 1, y, -1, 0))
					resetTetromino();

			break;

			case 38:

				if(run)
					tetrominoFilp();

			break;

			case 39:

				if(run && displayTetromino(x + 1, y, 1, 0))
					resetTetromino();

			break;

			case 40:

				if(run && !accelerated)
				{
					accelerated = true;
					clearInterval(gameInterval);
					gameInterval = setInterval(tetrominoFall, 25);
				}

			break;
		}
	};
	canvas.onkeyup = function(event)
	{
		switch((event || window.event).keyCode)
		{
			case 40:

				if(run)
				{
					accelerated = false;
					clearInterval(gameInterval);
					gameInterval = setInterval(tetrominoFall, interval);
				}

			break;

			case 113:

				newGame();

			break;
		}
	};

	for(height = 0; height < 20; height++)
	{
		row = canvas.insertRow(0);

		for(width = 0; width < 10; width++)
			row.insertCell(0);
	}

	canvas.focus();
}

function newGame()
{
	clearInterval(gameInterval);

	score = 0;
	run = true;
	canvas.focus();
	resetTetromino();
	level = startLevel;
	levelElement.innerHTML = level;
	scoreElement.innerHTML = score;
	interval = 900 - startLevel * 30;

	for(height = 0; height < 20; height++)
	{
		cells = canvas.rows[height].cells;

		for(width = 0; width < 10; width++)
			cells[width].style.backgroundPosition = "";
	}

	gameInterval = setInterval(tetrominoFall, interval);
}

function resetTetromino()
{
	x = 4;
	n = -1;
	actualTetromino = Math.floor(Math.random() * 7);

	for(row = 1; row < 20; row++)
		if(testRow(row))
		{
			removeRow(row);
			lines++;
			n++;
		}

	if(lines > 9)
	{
		level++;
		lines -= 10;
		interval -= 30;
		clearInterval(gameInterval);
		levelElement.innerHTML = level;
	}

	if(n > -1)
	{
		score += rowScore[n] * level + rowScore[n];
		scoreElement.innerHTML = score;
	}

	for(flip = Math.floor(Math.random() * 4); flip > 0; flip--)
	{
		newTetromino =
		[
			tetromino[actualTetromino][12],
			tetromino[actualTetromino][8],
			tetromino[actualTetromino][4],
			tetromino[actualTetromino][0],
			tetromino[actualTetromino][13],
			tetromino[actualTetromino][9],
			tetromino[actualTetromino][5],
			tetromino[actualTetromino][1],
			tetromino[actualTetromino][14],
			tetromino[actualTetromino][10],
			tetromino[actualTetromino][6],
			tetromino[actualTetromino][2],
			tetromino[actualTetromino][15],
			tetromino[actualTetromino][11],
			tetromino[actualTetromino][7],
			tetromino[actualTetromino][3]
		];
		tetromino[actualTetromino] = newTetromino;
	}

	for(currentY = 3; currentY >= 0; currentY--)
	{
		cells = tetromino[actualTetromino];

		if(cells[0] || cells[1] || cells[2] || cells[3])
		{
			y = currentY * -1;
			break;
		}
	}
}

function displayTetromino(newX, newY, xWay, yWay)
{
	for(currentY = newY, index = 0; currentY < newY + 4; currentY++)
		for(currentX = newX; currentX < newX + 4; currentX++, index++)
			if(tetromino[actualTetromino][index] && currentY >= 0)
			{
				nextIndex = index + xWay + yWay * 4;

				if
				(
					currentX < 0 || currentX > 9 || currentY == 20 ||
					(
						canvas.rows[currentY].cells[currentX].style.backgroundPosition
						&&
						(
							nextIndex > 15
							||
							!tetromino[actualTetromino][nextIndex]
						)
					)
				)
					return yWay;
			}

	clearTetromino();

	x = newX;
	y = newY;

	for(currentY = y, index = 0; currentY < y + 4; currentY++)
		for(currentX = x; currentX < x + 4; currentX++, index++)
			if(tetromino[actualTetromino][index] && currentY >= 0)
				canvas.rows[currentY].cells[currentX].style.backgroundPosition = tetromino[actualTetromino][index];

	return false;
}

function clearTetromino()
{
	for(currentY = y, index = 0; currentY < y + 4; currentY++)
		for(currentX = x; currentX < x + 4; currentX++, index++)
			if(tetromino[actualTetromino][index] && currentY > -1)
				canvas.rows[currentY].cells[currentX].style.backgroundPosition = "";
}

function tetrominoFall()
{
	if(displayTetromino(x, y + 1, 0, 1))
	{
		if(testFirstRow())
		{
			clearInterval(gameInterval);
			run = false;
		}
		else
			resetTetromino();
	}
}

function tetrominoFilp()
{
	can = true;
	clearTetromino();

	newTetromino =
	[
		tetromino[actualTetromino][12],
		tetromino[actualTetromino][8],
		tetromino[actualTetromino][4],
		tetromino[actualTetromino][0],
		tetromino[actualTetromino][13],
		tetromino[actualTetromino][9],
		tetromino[actualTetromino][5],
		tetromino[actualTetromino][1],
		tetromino[actualTetromino][14],
		tetromino[actualTetromino][10],
		tetromino[actualTetromino][6],
		tetromino[actualTetromino][2],
		tetromino[actualTetromino][15],
		tetromino[actualTetromino][11],
		tetromino[actualTetromino][7],
		tetromino[actualTetromino][3]
	];

	for(currentY = y, index = 0; currentY < y + 4; currentY++)
		for(currentX = x; currentX < x + 4; currentX++, index++)
			if(newTetromino[index])
				if
				(
					currentX < 0 || currentX > 9 || currentY == 20 ||
					canvas.rows[currentY].cells[currentX].style.backgroundPosition
				)
				{
					can = false;
					break;
				}

	if(can)
		tetromino[actualTetromino] = newTetromino;

	displayTetromino(x, y, 0, 0);
}

function testRow(row)
{
	cells = canvas.rows[row].cells;
	return cells[0].style.backgroundPosition && cells[1].style.backgroundPosition &&
	cells[2].style.backgroundPosition && cells[3].style.backgroundPosition &&
	cells[4].style.backgroundPosition && cells[5].style.backgroundPosition &&
	cells[6].style.backgroundPosition && cells[7].style.backgroundPosition &&
	cells[8].style.backgroundPosition && cells[9].style.backgroundPosition;
}

function testFirstRow()
{
	cells = canvas.rows[0].cells;
	return cells[0].style.backgroundPosition || cells[1].style.backgroundPosition ||
	cells[2].style.backgroundPosition || cells[3].style.backgroundPosition ||
	cells[4].style.backgroundPosition || cells[5].style.backgroundPosition ||
	cells[6].style.backgroundPosition || cells[7].style.backgroundPosition ||
	cells[8].style.backgroundPosition || cells[9].style.backgroundPosition;
}

function removeRow(rowIndex)
{
	canvas.deleteRow(rowIndex);
	newRow = canvas.insertRow(0);

	for(x = 0; x < 10; x++)
		newRow.insertCell(0);
}

Občas se to zdvojí - pročež předpokládám, že časovač nečeká na dokončení ze sebe volané funkce...

Nahlásit jako SPAM
IP: 78.136.162.–
peter
~ Anonymní uživatel
4005 příspěvků
23. 9. 2014   #7
-
0
-

Chtelo by to neco, co se da spustit :) Je tu moznost treba jsfiddle.net. Takhle fakt nemuzu hadat chybu, kdyz mi konzola hlasi neexistujici htm elementy.
Zkousel jsi cyklus pro cells[i].style.backgroundPosition?
Na to startovani a ukonceni intervalu bych si udelal funkci.
Casovac bys mel nejdriv zastavit a az pak menit promenne, jako treba accelerated.
Co se stane, kdyz na keydown klavesy 40 a keyup se spusti setinterval? Maler, protoze tyto dve udalosti mohou navazovat kratce na sebe a interval nema cas dokoncit prvni funkci.

<div id=log style="height:20em; overflow:auto;"></div>
<script>
function loguj(str)
{
document.getElementById('log').innerHTML+= str + "<br>";
}
</script>


<aside></aside>
<div id=level></div>
<div id=score></div>
<canvas id=canvas></canvas>
<input type=button id=newGame value=newGame>


<script>
function fA() {loguj(1); clearInterval(gameInterval);}
function fB() {loguj(2); gameInterval = setInterval(tetrominoFall, interval);}
var
	x = 0,
	y = 0,
	canvas,
	lines = 0,
	level = 0,
	score = 0,
	run = false,
	levelElement,
	scoreElement,
	gameInterval,
	interval = 900,
	startLevel = 0,
	accelerated = false,
	actualTetromino = 0,
	rowScore = [ 40, 100, 300, 1200 ],
	tetromino =
	[
		[ "", "", "", "", "-24px 0px", "-24px 0px", "-24px 0px", "-24px 0px", "", "", "", "", "", "", "", "" ],
		[ "", "", "", "", "", "-48px 0px", "-48px 0px", "", "", "-48px 0px", "-48px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "", "-72px 0px", "", "", "-72px 0px", "-72px 0px", "-72px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "", "-96px 0px", "-96px 0px", "", "-96px 0px", "-96px 0px", "", "", "", "", "", "" ],
		[ "", "", "", "", "-120px 0px", "-120px 0px", "", "", "", "-120px 0px", "-120px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "-144px 0px", "", "", "", "-144px 0px", "-144px 0px", "-144px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "", "", "-168px 0px", "", "-168px 0px", "-168px 0px", "-168px 0px", "", "", "", "", "" ]
	],
	aside;

window.onload = function()
{
	var aside = document.getElementsByTagName("aside")[0]; //pp
	levelElement = document.getElementById("level");
	scoreElement = document.getElementById("score");
	canvas = document.getElementById("canvas");

	canvas.insertRow = function() {return {insertCell:function(){}};}; //pp
	canvas.rows = [0,1,2,3,4,5,6,7,8,9,10, 11,12,13,14,15,16,17,18,19,20]; //pp

	document.getElementById("newGame").onclick = newGame;

	var pageY = window.innerHeight / 2 - 240; //pp
	var pageX = window.innerWidth / 2 - 170; //pp
	canvas.style.left = pageX + "px";
	canvas.style.top = pageY + "px";
	aside.style.left = (pageX + 240) + "px";
	aside.style.top = pageY + "px";

	canvas.onkeydown = function(event)
	{
		switch((event || window.event).keyCode)
		{
			case 37:

				if(run && displayTetromino(x - 1, y, -1, 0))
					resetTetromino();

			break;

			case 38:

				if(run)
					tetrominoFilp();

			break;

			case 39:

				if(run && displayTetromino(x + 1, y, 1, 0))
					resetTetromino();

			break;

			case 40:

				if(run && !accelerated)
				{
					accelerated = true;
					fA();
loguj(3);
					fB();
				}

			break;
		}
	};
	canvas.onkeyup = function(event)
	{
		switch((event || window.event).keyCode)
		{
			case 40:

				if(run)
				{
					accelerated = false;
					fA();
loguj(4);
					fB();
				}

			break;

			case 113:

				newGame();

			break;
		}
	};

	for(var height = 0; height < 20; height++) //pp
	{
		var row = canvas.insertRow(0); //pp

		for(var width = 0; width < 10; width++)
			row.insertCell(0);
	}


	canvas.focus();
}

function newGame()
{
	fA();
loguj(5);

	score = 0;
	run = true;
	canvas.focus();
	resetTetromino();
	level = startLevel;
	levelElement.innerHTML = level;
	scoreElement.innerHTML = score;
	interval = 900 - startLevel * 30;

	for(height = 0; height < 20; height++)
	{
		cells = canvas.rows[height].cells;

		for(width = 0; width < 10; width++)
			cells[width].style.backgroundPosition = "";
	}

	fB();
}

function resetTetromino()
{
	x = 4;
var	n = -1;
	actualTetromino = Math.floor(Math.random() * 7);

	for(var row = 1; row < 20; row++)
		if(testRow(row))
		{
			removeRow(row);
			lines++;
			n++;
		}

	if(lines > 9)
	{
		level++;
		lines -= 10;
		interval -= 30;
		fA();
loguj(6);
		levelElement.innerHTML = level;
	}

	if(n > -1)
	{
		score += rowScore[n] * level + rowScore[n];
		scoreElement.innerHTML = score;
	}

	for(flip = Math.floor(Math.random() * 4); flip > 0; flip--)
	{
		newTetromino =
		[
			tetromino[actualTetromino][12],
			tetromino[actualTetromino][8],
			tetromino[actualTetromino][4],
			tetromino[actualTetromino][0],
			tetromino[actualTetromino][13],
			tetromino[actualTetromino][9],
			tetromino[actualTetromino][5],
			tetromino[actualTetromino][1],
			tetromino[actualTetromino][14],
			tetromino[actualTetromino][10],
			tetromino[actualTetromino][6],
			tetromino[actualTetromino][2],
			tetromino[actualTetromino][15],
			tetromino[actualTetromino][11],
			tetromino[actualTetromino][7],
			tetromino[actualTetromino][3]
		];
		tetromino[actualTetromino] = newTetromino;
	}

	for(currentY = 3; currentY >= 0; currentY--)
	{
		cells = tetromino[actualTetromino];

		if(cells[0] || cells[1] || cells[2] || cells[3])
		{
			y = currentY * -1;
			break;
		}
	}
}

function displayTetromino(newX, newY, xWay, yWay)
{
	for(currentY = newY, index = 0; currentY < newY + 4; currentY++)
		for(currentX = newX; currentX < newX + 4; currentX++, index++)
			if(tetromino[actualTetromino][index] && currentY >= 0)
			{
				nextIndex = index + xWay + yWay * 4;

				if
				(
					currentX < 0 || currentX > 9 || currentY == 20 ||
					(
						canvas.rows[currentY].cells[currentX].style.backgroundPosition
						&&
						(
							nextIndex > 15
							||
							!tetromino[actualTetromino][nextIndex]
						)
					)
				)
					return yWay;
			}

	clearTetromino();

	x = newX;
	y = newY;

	for(currentY = y, index = 0; currentY < y + 4; currentY++)
		for(currentX = x; currentX < x + 4; currentX++, index++)
			if(tetromino[actualTetromino][index] && currentY >= 0)
				canvas.rows[currentY].cells[currentX].style.backgroundPosition = tetromino[actualTetromino][index];

	return false;
}

function clearTetromino()
{
	for(currentY = y, index = 0; currentY < y + 4; currentY++)
		for(currentX = x; currentX < x + 4; currentX++, index++)
			if(tetromino[actualTetromino][index] && currentY > -1)
				canvas.rows[currentY].cells[currentX].style.backgroundPosition = "";
}

function tetrominoFall()
{
	if(displayTetromino(x, y + 1, 0, 1))
	{
		if(testFirstRow())
		{
			fA();
loguj(7);
			run = false;
		}
		else
			resetTetromino();
	}
}

function tetrominoFilp()
{
	can = true;
	clearTetromino();

	newTetromino =
	[
		tetromino[actualTetromino][12],
		tetromino[actualTetromino][8],
		tetromino[actualTetromino][4],
		tetromino[actualTetromino][0],
		tetromino[actualTetromino][13],
		tetromino[actualTetromino][9],
		tetromino[actualTetromino][5],
		tetromino[actualTetromino][1],
		tetromino[actualTetromino][14],
		tetromino[actualTetromino][10],
		tetromino[actualTetromino][6],
		tetromino[actualTetromino][2],
		tetromino[actualTetromino][15],
		tetromino[actualTetromino][11],
		tetromino[actualTetromino][7],
		tetromino[actualTetromino][3]
	];

	for(currentY = y, index = 0; currentY < y + 4; currentY++)
		for(currentX = x; currentX < x + 4; currentX++, index++)
			if(newTetromino[index])
				if
				(
					currentX < 0 || currentX > 9 || currentY == 20 ||
					canvas.rows[currentY].cells[currentX].style.backgroundPosition
				)
				{
					can = false;
					break;
				}

	if(can)
		tetromino[actualTetromino] = newTetromino;

	displayTetromino(x, y, 0, 0);
}

function testRow(row)
{
	var cells = canvas.rows[row].cells;
	return cells[0].style.backgroundPosition && cells[1].style.backgroundPosition &&
	cells[2].style.backgroundPosition && cells[3].style.backgroundPosition &&
	cells[4].style.backgroundPosition && cells[5].style.backgroundPosition &&
	cells[6].style.backgroundPosition && cells[7].style.backgroundPosition &&
	cells[8].style.backgroundPosition && cells[9].style.backgroundPosition;
}

function testFirstRow()
{
	var cells = canvas.rows[0].cells;
	return cells[0].style.backgroundPosition || cells[1].style.backgroundPosition ||
	cells[2].style.backgroundPosition || cells[3].style.backgroundPosition ||
	cells[4].style.backgroundPosition || cells[5].style.backgroundPosition ||
	cells[6].style.backgroundPosition || cells[7].style.backgroundPosition ||
	cells[8].style.backgroundPosition || cells[9].style.backgroundPosition;
}

function removeRow(rowIndex)
{
	canvas.deleteRow(rowIndex);
	newRow = canvas.insertRow(0);

	for(x = 0; x < 10; x++)
		newRow.insertCell(0);
}


</script>


Vypsalo mi to 1 a 5, pak to skoncilo dalsi chybou v konzoli (uz asi 20, co jsem opravoval, abych se pokusil z toho udelat neco funkcniho) a prestalo mne to bavit.

Nahlásit jako SPAM
IP: 2001:718:2601:1f7:2d89:49...–
Matěj Andrle+1
Grafoman
23. 9. 2014   #8
-
0
-

#7 peter
Však vždyť přesně to řeším! Jenže jak jinak na to jít? Ano, udělal jsem při přechodu na další level to, že jsem prvně ukončil interval a na konci jej znovu spustil, jenže intervaly se pak neuvěřitelně kupí! Po předělání na tvar:

function resetTetromino()
{
	x = 4;
	n = -1;
	clearInterval(gameInterval);
	actualTetromino = Math.floor(Math.random() * 7);

	for(row = 1; row < 20; row++)
		if(testRow(row))
		{
			removeRow(row);
			lines++;
			n++;
		}

	if(lines > 9)
	{
		level++;
		lines -= 10;
		interval -= 30;
		levelElement.innerHTML = level;
	}

	if(n > -1)
	{
		score += rowScore[n] * level + rowScore[n];
		scoreElement.innerHTML = score;
	}

	for(flip = Math.floor(Math.random() * 4); flip > 0; flip--)
	{
		newTetromino =
		[
			tetromino[actualTetromino][12],
			tetromino[actualTetromino][8],
			tetromino[actualTetromino][4],
			tetromino[actualTetromino][0],
			tetromino[actualTetromino][13],
			tetromino[actualTetromino][9],
			tetromino[actualTetromino][5],
			tetromino[actualTetromino][1],
			tetromino[actualTetromino][14],
			tetromino[actualTetromino][10],
			tetromino[actualTetromino][6],
			tetromino[actualTetromino][2],
			tetromino[actualTetromino][15],
			tetromino[actualTetromino][11],
			tetromino[actualTetromino][7],
			tetromino[actualTetromino][3]
		];
		tetromino[actualTetromino] = newTetromino;
	}

	for(currentY = 3; currentY >= 0; currentY--)
	{
		cells = tetromino[actualTetromino];

		if(cells[0] || cells[1] || cells[2] || cells[3])
		{
			y = currentY * -1;
			break;
		}
	}
	gameInterval = setInterval(tetrominoFall, interval);
}

Se intervaly hromadí a hra se nedá hrát...

BTW:

http://leteckaposta.cz/447625174

Nahlásit jako SPAM
IP: 78.136.162.–
peter
~ Anonymní uživatel
4005 příspěvků
24. 9. 2014   #9
-
0
-

Mno, hele, zapni si uz v prohlizeci tu konzolu a vsechna varovani a dopln vary pro promenne :) Stalo mne to asi 10 min.

Kdyz uz jsem to vycistil, tak jsem to zkousel a vsiml si, ze tesne pred objevenim nove kostky, kdyz mackas klavesy, tak se objevi hned dole. Tam bych mozna hledal jeden problem.

Pak jsem si v kodu vsiml, ze na klavesu spoustis casovac. To je chyba. Klavesu uloz do promenne a nech zpracovat az dalsim casovanim. Ja bych si udelal funkci casovac, ktera pricita +1 do promene, po 20 nuluje a udelal tam rozhodovani prom = 5, prom=20 atd. Casovac pak spustis jednou pri zapnuti hry a ukoncis pri konci. Interval bych dal treba 50 ms (nebo mozna bude stacit 100 ms).
Treba, kdyz se zmackne klavesa, tak po 0-100 ms zareaguje casovac, ktery na ni nejak zareaguje ihned, cili bez prom=neco. Ale, kdyz uplyne 1s, tak se spusti posun dolu, v casovaci na to zareaguje prom==10 (pri intervalu casovace 100ms * 10 cyklu = 1s).
Pozdeji to omrknu vic a zkusmi mozna prepsat...

var timer, timerInterval, timerIncrement;
timerInterval = 50

function timerStart()
{
timer = setInterval(timerFunc, timerInterval);
}

function timerStop()
{
clearInterval(timer);
}

function timerFunc()
{
if (key!=-1) {pohyb(key);}
else
  {
  if {timerIncrement == 20} {tetrominoFall();}
  }
...
timerIncrement++;
timerIncrement%=20; // omezeni 20 pomoci ziskani zbytku po deleni
}
Nahlásit jako SPAM
IP: 2001:718:2601:1f7:302a:fa...–
Matěj Andrle+1
Grafoman
24. 9. 2014   #10
-
0
-

#9 peter
Blbost - konzoli používám celou dobu -> var je blbost - úplně k ničemu, bez významu... Ve třech prohlížečích nemám ani jeden error, varování atp. Var je v JS jen navíc! K tomu, abych dělal todo list pro interval - neviděl jsem tetris, kde se na zrychlením čeká... (Hlavně na pomalých levelech to bude jako trn v oku.) Okamžitý pád souvisí s novou fičurou, co jsem tam dal. A sice postupné vyjetí, namísto okamžitého skoku. Bez toho cyklu, který hledá spodek kostky:

	for(currentY = 3; currentY >= 0; currentY--)
	{
		cells = tetromino[actualTetromino];

		if(cells[0] || cells[1] || cells[2] || cells[3])
		{
			y = currentY * -1;
			break;
		}
	}

nepadají kostky naráz, ovšem zase nefunguje konec hry. Tam zápolím s tím, že když se kostka mrdne doprostřed pole, tak není jak zjistit, zda je konec a navíc to pak vypadá esteticky divně. Načež zápolím spíše s tím, aby fungovalo postupné najetí a nehodilo občas kostku rovnou dospodu. Větší starosti mi dělají právě časovače. Jak jsem psal - todo list bude vypadat komicky, ovšem když to jinak nevymyslím, tak to použiji...

Nahlásit jako SPAM
IP: 78.136.140.–
peter
~ Anonymní uživatel
4005 příspěvků
24. 9. 2014   #11
-
0
-

"var je blbost - úplně k ničemu, bez významu..."
:) Ma vyznam.
- definuje rozdil mezi globalni promenou a vnitrni, jinak si to musi zjistit prohlizec sam, coz chvilku trva
- tez je dobre treba pred cyklem promenou naplnit, aby si vybral spravny typ, coz opet prohlizeci chvilku trva pretypovat z nejakeho default typu.
- soucasne tim reknes tomu, kdo to po tobe bude cist, zda je ta promenna vyznamna vne nebo uvnitr. Kdyz ji uvidim ve var tak ji nebudu hledat po celem programu.

resit to nemusis, jen ti rikam, ze mi to dela strasne potize pri hledani chyb. A je celkem mozne, ze se ti priste na to vybodnu. Mozna budes i rad, kdo vi :)

Nahlásit jako SPAM
IP: 2001:718:2601:1f7:302a:fa...–
Matěj Andrle+1
Grafoman
24. 9. 2014   #12
-
0
-

Globální proměnné deklaruji vždy na prvním řádku - tak co to píšeš? Pořád netuším proč se intervaly zdvojují - pakliže nepoužívám zrychlení, či cokoliv jiného, co krom resetTetromino ovlivňuje interval...

Nahlásit jako SPAM
IP: 78.136.140.–
peter
~ Anonymní uživatel
4005 příspěvků
24. 9. 2014   #13
-
0
-

Zdvojovani sipky dolu jsem ti psal v #6.
Zmacknuti klavesy je udalost onkeydown + onkeyup. Ty mas v programu onkeydown : spustCasovac a onkeyup: spustcasovac podruhe. takze na jednu zmacknutou a pustenou sipku dolu se 2x spusti casovac. Tu udalost bys mel mit navazanou jen na onkeydown, si myslim. Ale treba to tak mas zamerne, tezko rici.

Nahlásit jako SPAM
IP: 2001:718:2601:1f7:302a:fa...–
peter
~ Anonymní uživatel
4005 příspěvků
24. 9. 2014   #14
-
0
-

Dalsi problem jsem ted nasel treba v newgame. Pri kliknuti na newgame se spusti casovac 2x. Mozna to mas podobne i jinde.

function newGame()
{
timerStop();
resetTetromino()
  {
  timerStop();
  timerStart();
  }
timerStart();
}
Nahlásit jako SPAM
IP: 2001:718:2601:1f7:302a:fa...–
peter
~ Anonymní uživatel
4005 příspěvků
24. 9. 2014   #15
-
0
-

Moje posl. verze vypada takto a zda se, ze funguje podle ocekavani. jestli jsi teda nemel jina ocekavani :) Ale mozna tam mam par veci navic. Total commander ma takovou peknou funkci porovnavani obsahu souboru a ocerveni tam zmeny.

var
key_down = -1,
key_up = -1,
	x = 0,
	y = 0,
	canvas,
	lines = 0,
	level = 0,
	score = 0,
	run = false,
	levelElement,
	scoreElement,
	gameInterval = null,
	interval = 700,
	startLevel = 0,
	accelerated = false,
	actualTetromino = 0,
	rowScore = [ 40, 100, 300, 1200 ],
	tetromino =
	[
		[ "", "", "", "", "-24px 0px", "-24px 0px", "-24px 0px", "-24px 0px", "", "", "", "", "", "", "", "" ],
		[ "", "", "", "", "", "-48px 0px", "-48px 0px", "", "", "-48px 0px", "-48px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "", "-72px 0px", "", "", "-72px 0px", "-72px 0px", "-72px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "", "-96px 0px", "-96px 0px", "", "-96px 0px", "-96px 0px", "", "", "", "", "", "" ],
		[ "", "", "", "", "-120px 0px", "-120px 0px", "", "", "", "-120px 0px", "-120px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "-144px 0px", "", "", "", "-144px 0px", "-144px 0px", "-144px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "", "", "-168px 0px", "", "-168px 0px", "-168px 0px", "-168px 0px", "", "", "", "", "" ]
	];

function timerStart()
{
gameInterval = setInterval(tetrominoFall, interval);
}

function timerStart2()
{
gameInterval = setInterval(tetrominoFall, 50);
}

function timerStop()
{
clearInterval(gameInterval);
}

function keyDownAction()
{
		switch(key_down)
		{
			case 37: // left
				if(run && displayTetromino(x - 1, y, -1, 0))
					resetTetromino();
			break;
			case 38: // up
				if(run)
					tetrominoFilp();
			break; // right
			case 39:
				if(run && displayTetromino(x + 1, y, 1, 0))
					resetTetromino();
			break;
			case 40: // down
				if(run && !accelerated)
				{
					accelerated = true;
					timerStop();
key_down = -1;
					timerStart2();
//displayTetromino(x, y+1, 0, 1)
////resetTetromino();
//loguj('40 down')
				}
			break;
		}
key_down = -1;
}

function keyUpAction()
{
		switch(key_up)
		{
			case 40:
				if(run)
				{
					accelerated = false;
					timerStop();
key_up = -1;
					timerStart();
//loguj('40 up')
				}
			break;
			case 113:
				newGame();
//loguj('newgame')
			break;
		}
key_up = -1;
}


window.onload = function()
{
	var pageY, pageX, height, row, width, aside;

	aside = document.getElementsByTagName("aside")[0];
	levelElement = document.getElementById("level");
	scoreElement = document.getElementById("score");
	canvas = document.getElementById("canvas");

	document.getElementById("newGame").onclick = newGame;

	pageY = window.innerHeight / 2 - 240;
	pageX = window.innerWidth / 2 - 170;
	canvas.style.left = pageX + "px";
	canvas.style.top = pageY + "px";
	aside.style.left = (pageX + 240) + "px";
	aside.style.top = pageY + "px";

	canvas.onkeydown = function(event)
		{
		key_down = (event || window.event).keyCode;
		keyDownAction();
		};
	canvas.onkeyup = function(event)
		{
		key_up = (event || window.event).keyCode;
		keyUpAction();
		};

	for(height = 0; height < 20; height++)
	{
		row = canvas.insertRow(0);

		for(width = 0; width < 10; width++)
			row.insertCell(0);
	}

	canvas.focus();
}

function newGame()
{
var height, width, cells;
loguj('newgame timer stop')
	timerStop();

	score = 0;
	run = true;
	canvas.focus();
	resetTetromino();
	level = startLevel;
	levelElement.innerHTML = level;
	scoreElement.innerHTML = score;
	interval = 900 - startLevel * 30;

	for(height = 0; height < 20; height++)
	{
		cells = canvas.rows[height].cells;

		for(width = 0; width < 10; width++)
			cells[width].style.backgroundPosition = "";
	}

	timerStart();
loguj('newgame timer start')
}

function resetTetromino()
{
//	timerStop();
	var n, row, flip, newTetromino, currentX,currentY, height, width, index, newIndex, cells;
	x = 4;
	n = -1;
	actualTetromino = Math.floor(Math.random() * 7);

	for(row = 1; row < 20; row++)
		if(testRow(row))
		{
			removeRow(row);
			lines++;
			n++;
		}

	if(lines > 9)
	{
		level++;
		lines -= 10;
		interval -= 30;
		levelElement.innerHTML = level;
	}

	if(n > -1)
	{
		score += rowScore[n] * level + rowScore[n];
		scoreElement.innerHTML = score;
	}

	for(flip = Math.floor(Math.random() * 4); flip > 0; flip--)
	{
		newTetromino =
		[
			tetromino[actualTetromino][12],
			tetromino[actualTetromino][8],
			tetromino[actualTetromino][4],
			tetromino[actualTetromino][0],
			tetromino[actualTetromino][13],
			tetromino[actualTetromino][9],
			tetromino[actualTetromino][5],
			tetromino[actualTetromino][1],
			tetromino[actualTetromino][14],
			tetromino[actualTetromino][10],
			tetromino[actualTetromino][6],
			tetromino[actualTetromino][2],
			tetromino[actualTetromino][15],
			tetromino[actualTetromino][11],
			tetromino[actualTetromino][7],
			tetromino[actualTetromino][3]
		];
		tetromino[actualTetromino] = newTetromino;
	}

	for(currentY = 3; currentY >= 0; currentY--)
	{
		cells = tetromino[actualTetromino];

		if(cells[0] || cells[1] || cells[2] || cells[3])
		{
			y = currentY * -1;
			break;
		}
	}
//	timerStart();
loguj('reset timer')
}

function displayTetromino(newX, newY, xWay, yWay)
{
var currentX, currentY, nextIndex, index;
	for(currentY = newY, index = 0; currentY < newY + 4; currentY++)
		for(currentX = newX; currentX < newX + 4; currentX++, index++)
			if(tetromino[actualTetromino][index] && currentY >= 0)
			{
				nextIndex = index + xWay + yWay * 4;

				if
				(
					currentX < 0 || currentX > 9 || currentY == 20 ||
					(
						canvas.rows[currentY].cells[currentX].style.backgroundPosition
						&&
						(
							nextIndex > 15
							||
							!tetromino[actualTetromino][nextIndex]
						)
					)
				)
					return yWay;
			}

	clearTetromino();

	x = newX;
	y = newY;

	for(currentY = y, index = 0; currentY < y + 4; currentY++)
		for(currentX = x; currentX < x + 4; currentX++, index++)
			if(tetromino[actualTetromino][index] && currentY >= 0)
				canvas.rows[currentY].cells[currentX].style.backgroundPosition = tetromino[actualTetromino][index];

	return false;
}

function clearTetromino()
{
var currentX, currentY, index;
	for(currentY = y, index = 0; currentY < y + 4; currentY++)
		for(currentX = x; currentX < x + 4; currentX++, index++)
			if(tetromino[actualTetromino][index] && currentY > -1)
				canvas.rows[currentY].cells[currentX].style.backgroundPosition = "";
}

function tetrominoFall()
{

	if(displayTetromino(x, y + 1, 0, 1))
	{
		if(testFirstRow())
		{
	timerStop();
			run = false;
		}
		else
			resetTetromino();
	}
//keyUpAction();
}

function tetrominoFilp()
{
var can, newTetromino, currentX , currentY, index;
	can = true;
	clearTetromino();

	newTetromino =
	[
		tetromino[actualTetromino][12],
		tetromino[actualTetromino][8],
		tetromino[actualTetromino][4],
		tetromino[actualTetromino][0],
		tetromino[actualTetromino][13],
		tetromino[actualTetromino][9],
		tetromino[actualTetromino][5],
		tetromino[actualTetromino][1],
		tetromino[actualTetromino][14],
		tetromino[actualTetromino][10],
		tetromino[actualTetromino][6],
		tetromino[actualTetromino][2],
		tetromino[actualTetromino][15],
		tetromino[actualTetromino][11],
		tetromino[actualTetromino][7],
		tetromino[actualTetromino][3]
	];

	for(currentY = y, index = 0; currentY < y + 4; currentY++)
		for(currentX = x; currentX < x + 4; currentX++, index++)
			if(newTetromino[index])
				if
				(
					currentX < 0 || currentX > 9 || currentY == 20 ||
					canvas.rows[currentY].cells[currentX].style.backgroundPosition
				)
				{
					can = false;
					break;
				}

	if(can)
		tetromino[actualTetromino] = newTetromino;

	displayTetromino(x, y, 0, 0);
}

function testRow(row)
{
	var cells;
	cells = canvas.rows[row].cells;
	return cells[0].style.backgroundPosition && cells[1].style.backgroundPosition &&
	cells[2].style.backgroundPosition && cells[3].style.backgroundPosition &&
	cells[4].style.backgroundPosition && cells[5].style.backgroundPosition &&
	cells[6].style.backgroundPosition && cells[7].style.backgroundPosition &&
	cells[8].style.backgroundPosition && cells[9].style.backgroundPosition;
}

function testFirstRow()
{
	var cells;
	cells = canvas.rows[0].cells;
	return cells[0].style.backgroundPosition || cells[1].style.backgroundPosition ||
	cells[2].style.backgroundPosition || cells[3].style.backgroundPosition ||
	cells[4].style.backgroundPosition || cells[5].style.backgroundPosition ||
	cells[6].style.backgroundPosition || cells[7].style.backgroundPosition ||
	cells[8].style.backgroundPosition || cells[9].style.backgroundPosition;
}

function removeRow(rowIndex)
{
	canvas.deleteRow(rowIndex);
	newRow = canvas.insertRow(0);

	for(x = 0; x < 10; x++)
		newRow.insertCell(0);
}

function loguj(str)
{
document.getElementById('log').innerHTML+= str + "<br>";
}
Nahlásit jako SPAM
IP: 2001:718:2601:1f7:302a:fa...–
Matěj Andrle+1
Grafoman
24. 9. 2014   #16
-
0
-

#14 peter
Pořád netuším proč se intervaly zdvojují - pakliže nepoužívám zrychlení, či cokoliv jiného, co krom resetTetromino ovlivňuje interval...

 Neřeším zdvojení po zrychlení - tomu rozumím a řeším ho. Řeším zdvojení samo o sobě. A tady máš odpověď na novou hru:

function newGame()
{
	clearInterval(gameInterval);
Nahlásit jako SPAM
IP: 78.136.140.–
peter
~ Anonymní uživatel
4005 příspěvků
24. 9. 2014   #17
-
0
-

No, v tom mem poslednim kodu se mi prave nic uz nezdvojuje a zda se to, aspon ve FF, funkcni. Az teda na t dopady, kdy vyleze kosticka z nicehonic dole, obcas.
A ten kod u newgame (NG), mozna jsem to mel popsat. V NG nejdriv provedes STOP, pak je nejaky kod, pak se spousti resetTetromino (RT), ktere provede STOP take. Po sem by to bylo jaksi ok. Ale pak RT provede START a ukonci se. Pak pokracuje dal kod v new game a to na konci take spusti START. Tudiz se v newgame provede v podstate 2x za sebou START, coz asi vyvolava to zdvojovani, ktere nejspis hledas.
A pak jsem v tom posl. kodu jeste caroval s klavesami. Mozna to taky neco poresilo, ale tim si nejsem uz jisty, pac tu RT jsem resil az pak.

Nahlásit jako SPAM
IP: 2001:718:2601:1f7:302a:fa...–
peter
~ Anonymní uživatel
4005 příspěvků
24. 9. 2014   #18
-
0
-

Jo, to start, stop, to mam terminologii podle upraveneho kodu, myslim tim samozrejme setInterval a clearInterval pro start a stop casovace.

Nahlásit jako SPAM
IP: 2001:718:2601:1f7:302a:fa...–
Matěj Andrle+1
Grafoman
24. 9. 2014   #19
-
0
-

#18 peter
Myslím, že vše vyřeší jediný časovač na 30ms. To by byla nejvyšší možná rychlost a zbytek by se dopočítával dle kýžené potřeby - za pomoci druhotné proměnné. Tak by odpadly hrátky s intervaly...

Nahlásit jako SPAM
IP: 78.136.140.–
peter
~ Anonymní uživatel
4005 příspěvků
24. 9. 2014   #20
-
0
-

Zkousel jsi ten muj kod?
s tim jedinym casovacem souhlas.

Nahlásit jako SPAM
IP: 2001:718:2601:1f7:302a:fa...–
Matěj Andrle+1
Grafoman
24. 9. 2014   #21
-
0
-

#20 peter
Zkoušel - stále dost věcí neřeší, pročež jak jsem napsal - spíše dám vše do jednoho časovače, kterýžto bude přidělovat volání dle počtu prázdných ticků...

Nahlásit jako SPAM
IP: 78.136.140.–
Matěj Andrle+1
Grafoman
25. 9. 2014   #22
-
0
-

   

var
	x = 0,
	y = 0,
	canvas,
	lines = 0,
	level = 0,
	score = 0,
	run = false,
	levelElement,
	scoreElement,
	gameInterval,
	duration = 30,
	cycleCount = 0,
	startLevel = 0,
	accelerated = false,
	actualTetromino = 0,
	rowScore = [ 40, 100, 300, 1200 ],
	tetromino =
	[
		[ "", "", "", "", "-24px 0px", "-24px 0px", "-24px 0px", "-24px 0px", "", "", "", "", "", "", "", "" ],
		[ "", "", "", "", "", "-48px 0px", "-48px 0px", "", "", "-48px 0px", "-48px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "", "-72px 0px", "", "", "-72px 0px", "-72px 0px", "-72px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "", "-96px 0px", "-96px 0px", "", "-96px 0px", "-96px 0px", "", "", "", "", "", "" ],
		[ "", "", "", "", "-120px 0px", "-120px 0px", "", "", "", "-120px 0px", "-120px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "-144px 0px", "", "", "", "-144px 0px", "-144px 0px", "-144px 0px", "", "", "", "", "" ],
		[ "", "", "", "", "", "", "-168px 0px", "", "-168px 0px", "-168px 0px", "-168px 0px", "", "", "", "", "" ]
	];

window.onload = function()
{
	aside = document.getElementsByTagName("aside")[0];
	levelElement = document.getElementById("level");
	scoreElement = document.getElementById("score");
	canvas = document.getElementById("canvas");

	document.getElementById("newGame").onclick = newGame;

	pageY = window.innerHeight / 2 - 240;
	pageX = window.innerWidth / 2 - 170;
	canvas.style.left = pageX + "px";
	canvas.style.top = pageY + "px";
	aside.style.left = (pageX + 240) + "px";
	aside.style.top = pageY + "px";

	canvas.onkeydown = function(event)
	{
		switch((event || window.event).keyCode)
		{
			case 37:

				if(run && displayTetromino(x - 1, y, -1, 0))
					resetTetromino();

			break;

			case 38:

				if(run)
					tetrominoFilp();

			break;

			case 39:

				if(run && displayTetromino(x + 1, y, 1, 0))
					resetTetromino();

			break;

			case 40:

				duration = 0;

			break;
		}
	};
	canvas.onkeyup = function(event)
	{
		switch((event || window.event).keyCode)
		{
			case 40:

				duration = 30 - level;

			break;

			case 113:

				newGame();

			break;
		}
	};

	for(var height = 0; height < 20; height++)
	{
		row = canvas.insertRow(0);

		for(var width = 0; width < 10; width++)
			row.insertCell(0);
	}

	canvas.focus();
}

function newGame()
{
	clearInterval(gameInterval);

	score = 0;
	level = 0;
	duration = 30;
	canvas.focus();
	levelElement.innerHTML = level;
	scoreElement.innerHTML = score;

	for(var height = 0; height < 20; height++)
	{
		cells = canvas.rows[height].cells;

		for(var width = 0; width < 10; width++)
			cells[width].style.backgroundPosition = "";
	}

	resetTetromino();
	gameInterval = setInterval(tetrominoFall, 30);
}

function resetTetromino()
{
	x = 4;
	var n = -1;
	actualTetromino = Math.floor(Math.random() * 7);

	for(var row = 1; row < 20; row++)
		if(testRow(row))
		{
			removeRow(row);
			lines++;
			n++;
		}

	if(lines > 9)
	{
		level++;
		duration--;
		lines -= 10;
		levelElement.innerHTML = level;
	}

	if(n > -1)
	{
		score += rowScore[n] * level + rowScore[n];
		scoreElement.innerHTML = score;
	}

	for(flip = Math.floor(Math.random() * 4); flip > 0; flip--)
	{
		newTetromino =
		[
			tetromino[actualTetromino][12],
			tetromino[actualTetromino][8],
			tetromino[actualTetromino][4],
			tetromino[actualTetromino][0],
			tetromino[actualTetromino][13],
			tetromino[actualTetromino][9],
			tetromino[actualTetromino][5],
			tetromino[actualTetromino][1],
			tetromino[actualTetromino][14],
			tetromino[actualTetromino][10],
			tetromino[actualTetromino][6],
			tetromino[actualTetromino][2],
			tetromino[actualTetromino][15],
			tetromino[actualTetromino][11],
			tetromino[actualTetromino][7],
			tetromino[actualTetromino][3]
		];
		tetromino[actualTetromino] = newTetromino;
	}

	cells = tetromino[actualTetromino];

	for(var currentY = 3, offset = 15; currentY >= 0; currentY--, offset -= 4)
		if(cells[offset] || cells[offset - 1] || cells[offset - 2] || cells[offset - 3])
		{
			y = currentY * -1;
			break;
		}

	displayTetromino(x, y, 0, 1)
	run = true;
}

function displayTetromino(newX, newY, xWay, yWay)
{
	if(yWay)
		cycleCount = 0;

	for(var currentY = newY, index = 0; currentY < newY + 4; currentY++)
		for(var currentX = newX; currentX < newX + 4; currentX++, index++)
			if(tetromino[actualTetromino][index] && currentY >= 0)
			{
				var nextIndex = index + xWay + yWay * 4;

				if
				(
					currentX < 0 || currentX > 9 || currentY == 20 ||
					(
						canvas.rows[currentY].cells[currentX].style.backgroundPosition
						&&
						(
							nextIndex > 15
							||
							!tetromino[actualTetromino][nextIndex]
						)
					)
				)
					return yWay;
			}

	clearTetromino();

	x = newX;
	y = newY;

	for(var currentY = y, index = 0; currentY < y + 4; currentY++)
		for(var currentX = x; currentX < x + 4; currentX++, index++)
			if(tetromino[actualTetromino][index] && currentY >= 0)
				canvas.rows[currentY].cells[currentX].style.backgroundPosition = tetromino[actualTetromino][index];

	return false;
}

function clearTetromino()
{
	for(var currentY = y, index = 0; currentY < y + 4; currentY++)
		for(var currentX = x; currentX < x + 4; currentX++, index++)
			if(tetromino[actualTetromino][index] && currentY > -1)
				canvas.rows[currentY].cells[currentX].style.backgroundPosition = "";
}

function tetrominoFall()
{
	if(run && cycleCount++ >= duration && displayTetromino(x, y + 1, 0, 1))
	{
		run = false;

		if(testFirstRow())
			clearInterval(gameInterval);
		else
			resetTetromino();
	}
}

function tetrominoFilp()
{
	can = true;
	clearTetromino();

	newTetromino =
	[
		tetromino[actualTetromino][12],
		tetromino[actualTetromino][8],
		tetromino[actualTetromino][4],
		tetromino[actualTetromino][0],
		tetromino[actualTetromino][13],
		tetromino[actualTetromino][9],
		tetromino[actualTetromino][5],
		tetromino[actualTetromino][1],
		tetromino[actualTetromino][14],
		tetromino[actualTetromino][10],
		tetromino[actualTetromino][6],
		tetromino[actualTetromino][2],
		tetromino[actualTetromino][15],
		tetromino[actualTetromino][11],
		tetromino[actualTetromino][7],
		tetromino[actualTetromino][3]
	];

	for(var currentY = y, index = 0; currentY < y + 4; currentY++)
		for(var currentX = x; currentX < x + 4; currentX++, index++)
			if(newTetromino[index])
				if
				(
					currentX < 0 || currentX > 9 || currentY == 20 ||
					canvas.rows[currentY].cells[currentX].style.backgroundPosition
				)
				{
					can = false;
					break;
				}

	if(can)
		tetromino[actualTetromino] = newTetromino;

	displayTetromino(x, y, 0, 0);
}

function testRow(row)
{
	var cells = canvas.rows[row].cells;
	return cells[0].style.backgroundPosition && cells[1].style.backgroundPosition &&
	cells[2].style.backgroundPosition && cells[3].style.backgroundPosition &&
	cells[4].style.backgroundPosition && cells[5].style.backgroundPosition &&
	cells[6].style.backgroundPosition && cells[7].style.backgroundPosition &&
	cells[8].style.backgroundPosition && cells[9].style.backgroundPosition;
}

function testFirstRow()
{
	var cells = canvas.rows[0].cells;
	return cells[0].style.backgroundPosition || cells[1].style.backgroundPosition ||
	cells[2].style.backgroundPosition || cells[3].style.backgroundPosition ||
	cells[4].style.backgroundPosition || cells[5].style.backgroundPosition ||
	cells[6].style.backgroundPosition || cells[7].style.backgroundPosition ||
	cells[8].style.backgroundPosition || cells[9].style.backgroundPosition;
}

function removeRow(rowIndex)
{
	canvas.deleteRow(rowIndex);
	var newRow = canvas.insertRow(0);

	for(x = 0; x < 10; x++)
		newRow.insertCell(0);
}

Vše funguje jak má. Pár věcí ještě doladím, avšak odpověď to nezmění - čeká... (Děkuji za ochotu.)

Nahlásit jako SPAM
IP: 78.136.140.–
Zjistit počet nových příspěvků

Přidej příspěvek

Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!

Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku

×Vložení zdrojáku

×Vložení obrázku

Vložit URL obrázku Vybrat obrázek na disku
Vlož URL adresu obrázku:
Klikni a vyber obrázek z počítače:

×Vložení videa

Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×
 
Podporujeme Gravatara.
Zadej URL adresu Avatara (40 x 40 px) nebo emailovou adresu pro použití Gravatara.
Email nikam neukládáme, po získání Gravatara je zahozen.
-
Pravidla pro psaní příspěvků, používej diakritiku. ENTER pro nový odstavec, SHIFT + ENTER pro nový řádek.
Sledovat nové příspěvky (pouze pro přihlášené)
Sleduj vlákno a v případě přidání nového příspěvku o tom budeš vědět mezi prvními.
Reaguješ na příspěvek:

Uživatelé prohlížející si toto vlákno

Uživatelé on-line: 0 registrovaných, 2 hosté

 

Hostujeme u Českého hostingu       ISSN 1801-1586       ⇡ Nahoru Webtea.cz logo © 20032024 Programujte.com
Zasadilo a pěstuje Webtea.cz, šéfredaktor Lukáš Churý