(Renkum, Wednesday 6th of June 2012)

Interaction

Now it is time to add some interaction to the game. I will add events to handle mouse/finger clicks. Clicking on the left side of the hunter will let the hunter go right and clicking on the right side of the hunter will let the hunter go left.

We will add a handler for the click-event to the canvas, using the following line of code in the main routine:

...
drawField(fields, hunterPosX);

window.setInterval(nextStep, 1000);
canvas.addEventListener('click',canvasOnClick);

function initField(fieldArray, hunterPositionX){
...

According to the DOM Event reference we will use the MouseEvent event as parameter in the event catching method that I called canvasOnClick. The ClientXArg and ClientYArg properties will contain the click location in the canvas. A short inspection tells me that there is a vertical and horizontal offset of 8 compared to the canvas (clicking on position 0,0 gives me 8,8) I decided to find out later where those 8 pixels come from.

I refactored some code to be able to change the X position:

(function () {
// some constants
numToAvoid = 50;
numTargets = 50;
fieldWidth = 41;
fieldHeight = 25;
itemWidth = 16;
itemHeight = 16;
scrWidth = fieldWidth * itemWidth;
scrHeight = fieldHeight * itemHeight;
curStartPos = 20;
numFields = fieldWidth * fieldHeight;

var canvas=document.getElementById('gameCanvas');
var ctx=canvas.getContext('2d');
var fields = new Array(numFields);
var hunterPosX = curStartPos;
var hunterPosY = 0;
var hunterPosXPrev = hunterPosX;
var hunterPosYPrev = hunterPosY;

initField(fields, hunterPosX); 
drawField(fields, hunterPosX);

window.setInterval(nextStep, 1000);
canvas.addEventListener('click',canvasOnClick);

function initField(fieldArray, hunterPositionX){
	// Clear the field
	for (i = 0; i < numFields; i++){
		fieldArray[i] = 0;
	}
	
	// Place the targets and avoidables
	for (i = 0; i < numToAvoid + numTargets ; i++){
		x = Math.floor(Math.random() * numFields);
		while (fieldArray[x] != 0 || x == hunterPositionX){
			x++;
			if (x > numFields) x = 0;
		}
		if (i < numToAvoid) fieldArray[x] =  1;
		else fieldArray[x] =  2;		
}	}
function drawField(fieldArray, hunterPositionX){
    // draw a rectangle as background
	ctx.moveTo(0,0);
	ctx.fillStyle='#DDDDFF';
	ctx.fillRect(0,0,scrWidth,scrHeight);
	
	//draw the other fields
	for ( k = 1; k < 3; k++){
		setFillStyle(k);
	    for ( i = 0; i < fieldHeight; i++){
			for ( j = 0; j < fieldWidth; j++){
				if (fieldArray[j + i * fieldWidth] == k) drawItem(j,i);
	}	}	}
	//draw the hunter
    ctx.fillStyle='#0000FF';
	drawItem(hunterPositionX, 0);
}
function drawItem(x,y){
	ctx.fillRect(x*itemWidth,(fieldHeight-y-1)*itemHeight,itemWidth,itemHeight)
}
function setFillStyle(itemType){
    if (itemType == 1) ctx.fillStyle='#FF0000';
	else if (itemType == 2) ctx.fillStyle='#00FF00';
	else ctx.fillStyle='#DDDDFF';
}
function nextStep(){
	var newHunterPosY = hunterPosY + 1;
	if (newHunterPosY >= fieldHeight) newHunterPosY = 0;
	hunterPosY = newHunterPosY;
	redrawCursor();
}
function redrawCursor(){
	var newHunterPosX = hunterPosX;
	var newHunterPosY = hunterPosY;
	if (newHunterPosX != hunterPosXPrev || newHunterPosY != hunterPosYPrev){
		//redraw the hunter
		ctx.fillStyle='#0000FF';
		drawItem(newHunterPosX, newHunterPosY);
		var oldItem = fields[hunterPosXPrev + hunterPosYPrev * fieldWidth];
		setFillStyle(oldItem);	 
		drawItem(hunterPosXPrev, hunterPosYPrev);
		hunterPosYPrev = newHunterPosY;
		hunterPosXPrev = newHunterPosX;
	}
}
function canvasOnClick(event){
	var x = event.clientX - 8;
	var y = event.clientY - 8;
	var hunterposnew = 0;
	if (x >= 0 && x < scrWidth && y >= 0 && y < scrHeight){
		var hunterX = (hunterPosX+ 0.5)*itemWidth;
		var diff = hunterX -x;
		var returnPoint = scrWidth /2;
		if (diff > 0)
			if (diff <= returnPoint) hunterposnew = hunterPosX + 1;
			else hunterposnew = hunterPosX -1;
		else
			if (diff > -returnPoint) hunterposnew = hunterPosX -1;
			else hunterposnew = hunterPosX + 1;		
		if (hunterposnew < 0) hunterposnew = fieldWidth -1;
		else if (hunterposnew >= fieldWidth) hunterposnew = 0;
		hunterPosX = hunterposnew;
	}
	redrawCursor();
}
})();

So after 2 hours in reading about events, implementing the event and refactoring some code we have a moving item and I'm able to control it with click.

To check that result, click here.

 


Day1 Day2 Day3 Day4 Day5 Day6 Day7
back to index