(Renkum, Thursday 7th of June 2012)

Grabbing the green objects

We will add an evaluation routine to evaluate each location that the hunter visits. To be able to start and stop the game I put the initialization into a function restartGame. I also made a function stopGame(). Notice that I cache the ID of the scheduled interval. Using that ID I'm able to stop the interval.

function restartGame(){
	hunterPosX = curStartPos;
	hunterPosY = 0;
	hunterPosXPrev = hunterPosX;
	hunterPosYPrev = hunterPosY;
	currentScore = 0;
	initField(fields, hunterPosX); 
	drawField(fields, hunterPosX);

	gameID = window.setInterval(nextStep, 1000);
}
function stopGame(){
	if (gameID != -1){
		window.clearInterval(gameID);
		gameID = -1;
}	}

Now I can create the evaluation method. And call it from the:

function checkCursorPosition(){
	var curLocation = hunterPosX + hunterPosY * fieldWidth;
	var oldItem = fields[curLocation];
	if (oldItem == 1){ // Game over
		stopGame();
		if (alert("Game over, you've got " + currentScore + 
                     " points. Press OK to restart the game."))
			restartGame();
	}
	else if (oldItem == 2){
		currentScore++; //Yes you earned a point!
	}
	fields[curLocation] = 0;
}

From now on, after spending 1 extra hour in refactoring and adding the stop, we can call our app a game. I must admit that it currently is a very boring game. But I'm going to Pimp it.

To check the result, click here.

 


Day1 Day2 Day3 Day4 Day5 Day6 Day7
back to index