Why are you giving "Uncaught Referenceerror: getSetorObj is not defined"?

Asked

Viewed 4,369 times

0

You are saying that "getSetorObj" is not defined, but the method exists and is in the Map class. The error is at line 38 of the script.

$(document).ready(function(){
	var jogo = new Jogo();
	jogo.init();
});

function Setor(X, Y){ 				//Classe Setor: Cada setor do mapa
	var x = X, y = Y; 				//	Coordenadas xy do setor	
	var id = "x" + x + "y" + y;		//	id do div do setor
	this.get = function(){			//	retorna o objeto JQuery do setor
		return $("#" + id);
	}
	
	$("#mapa").append("<div class=\"setor\" id=\"" + id + "\" ></div>" );	//	Adiciona div do setor ao mapa
}

function Mapa(TamX, TamY){
	var tamX = TamX, tamY = TamY;
	var setores = [];
	
	for(i = 1; i <= tamY; i++){
		for(j = 1; j <= tamX; j++){
			setores[tamX * (i - 1) + j] = new Setor(j, i);
		}
		$("#x" + 1 + "y" + i).css("clear", "left");
	}
	
	this.getSetorObj = function(x, y){
		return $("#x" + x + "y" + y);
	}
	
	
	var atualiza = function(){
		
	
	}
	
	this.selectSetor = function(X, Y){
		getSetorObj(X, Y).css("background-color", "red");
	}
	this.destSetor = function(X, Y){
		getSetorObj(X, Y).css("background-color", "lightblue");
	}
	this.getTamX = function(){
		return tamX;
	}
	this.getTamY = function(){
		return tamY;
	}
	this.getSetor = function(x, y){
		return setores[tamX * (y - 1) + x]
	}
}

function Jogo(){
	mapa = new Mapa(10, 10);
	var initX, initY;
	var setoresJogador = [];
	

	
	this.init = function(){
		initX = Math.floor((Math.random() * mapa.getTamX()) + 1);
		initY = Math.floor((Math.random() * mapa.getTamY()) + 1);
		
		setorInit = {x: initX, y: initY};
		setoresJogador.push(setorInit);
		mapa.selectSetor(initX, initY);
		atualiza();
	}
	
	var addSetorJog = function(novoSetor){
		setoresJogador.push(novoSetor);
		atualiza();
	}
	
	var getSetoresAdj = function(setores){
		var setoresAdjacentes = []; 
		for(i=0; i<setores.length; i++){
			var setor = setores[i];
			for(j= (-1); j<=1; j++){
				for(k= (-1); k<=1; k++){
					if(j!=0 || k!=0){
						var x = setor.x + j;
						var y = setor.y + k;
						if((x >= 1 && x <= mapa.getTamX()) && (y >=1 && y <= mapa.getTamY()))
							setoresAdjacentes.push({x: x, y: y});
                        }
				}
			}
		}
		return setoresAdjacentes;
	}
	var atualiza = function(){
		adjacentes = getSetoresAdj(setoresJogador);
		for(i=0; i<adjacentes.length; i++){
			var setor = adjacentes[i];
			mapa.destSetor(setor.x, setor.y);
			mapa.getSetorObj(setor.x, setor.y).click(function(){
				setoresJogador.push({x: setor.x, y: setor.y});
			});
		}
	}
}

function Jogador(){




}
<html>
	<head>
		<style>
		.setor{
			margin: 0px;
			padding: 0px;
			float: left;
			width: 40px;
			height: 40px;
			border: solid 1px lightblue;
			background-color: lightgreen;
		}
		</style>
		<script src="jquery.min.js"></script>
		<script src="script.js" ></script>
	</head>
	<body>
		<div id="mapa">
		</div>
	</body>
</html>

1 answer

4


You need to specify the this. to call the function getSetorObj on that line (and on others as well). If you do not use this prefix, the function

this.selectSetor = function(X, Y){
    getSetorObj(X, Y).css("background-color", "red");
}

is equivalent to

this.selectSetor = function(X, Y){
    [[global]].getSetorObj(X, Y).css("background-color", "red");
}

And that function is not defined in the global scope. Using the this you will use the correct scope.

this.selectSetor = function(X, Y){
    this.getSetorObj(X, Y).css("background-color", "red");
}
  • That was it, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.