1 Replies - 1486 Views - Last Post: 04 May 2017 - 04:24 AM Rate Topic: -----

#1 lostgurl1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 21-March 17

Countdown issue

Posted 21 April 2017 - 06:35 AM

Hi guys, I'm having an issue with my countdown...its generally not working but if i take out the playGame function it works...can anyone see the error here?

The error I'm getting is that timer isn't defined but i dont know where to call the function...Thank you for viewing and helping

//Image for Instruction Page 
function playGame () {
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");


var bgReady = false;
var bgImage = new Image();
bgImage.onload = function() {
	bgReady = true;
};
bgImage.src = "images/grass.png";


var TIME_PER_FRAME = 10;
var  BOX_WIDTH = 40,
	BOX_HEIGHT = 40;

var score = 0;
var time = 30;
var beat = 330;

var gameLoop, mouseX, mouseY,bugArray; 
var isClicked = false;

bugArray = new Array();

gameLoop = setInterval(update, TIME_PER_FRAME);
canvas.addEventListener("click",canvasClick,false);

function canvasClick(event){
	mouseX = event.clientX - canvas.offsetLeft + document.body.scrollLeft;
	mouseY = event.clientY - canvas.offsetTop + document.body.scrollTop;

	isClicked = true;
}

function update(){

	if (bgImage){
		ctx.drawImage(bgImage, 0, 0, 700, 520);
	}

	//Generates images 
	if(Math.random() < 0.02){
		var newBug = new Object();
		newBug.x = Math.floor(Math.random() * canvas.width);
		newBug.y = 20;
		bugArray.push(newBug);
	
	}
for (var i=bugArray.length-1; i >=0; i-- ){
	
	bugArray[i].y++;
	if (bugArray[i].y > canvas.height)
		bugArray.splice(i,1);
	else{
		drawBug(bugArray[i].x,  bugArray[i].y);

		if (isClicked)
		{
			if(hitTestPoint(bugArray[i].x, bugArray[i].y, BOX_WIDTH, BOX_HEIGHT, mouseX,mouseY))
			{
				score+=10;
				playSound();
				bugArray.splice(i,1);
				
			}
		}

	}
}
isClicked = false;

	
// Score
	ctx.font =" 25px verdana";
	ctx.fillStyle = "Black";
	ctx.fillText("Points: " + score, 0, 32);

	ctx.fillStyle = "black";
	ctx.fillText("Time remaining: " + time, 400, 32);
}

	var bugReady = false;
	var bugImage = new Image();
	bugImage.onload = function(){
		bugReady = true;
		};
	bugImage.src = "images/bug1.png";
	
	// Draw everything

	function drawBug(xPos,yPos){
		if (bugReady){
			ctx.drawImage(bugImage, xPos, yPos);
		}
	}	
		function hitTestPoint(x1, y1, w1, h1, x2, y2){
			if ((x1 <= x2 && x1+w1 >= x2) && (y1 <= y2 && y1+h1 >= y2))
			return true;
	else
		return false;
	}
	
	function squashBug(){
		var press = new Audio("music/splat.mp3");
		press.volume = 1.0;
		press.load();
		press.loop = false;
		return press;
	}
	function playSound(){
		var squashAudio = squashBug();
		squashAudio.play();
	}
timer();
	function timer(time){
    if(score >= beat && time!=0){
					alert("YOU WIN, CONGRATULATIONS 😄😄😁😁!!!!");
					//document.location.reload();
				}
				else{
					if(time === 0 && score < beat){
					alert("YOU LOST, SORRY ☹😭");
					//document.location.reload();
				}
			}
			
	time--;

}
var set = setInterval('timer(time)',1000);

function displaycounter(){

clearInterval(set);
}
setTimeout('displaycounter()',72000);

}




Is This A Good Question/Topic? 0
  • +

Replies To: Countdown issue

#2 lrk89  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 104
  • Joined: 20-May 09

Re: Countdown issue

Posted 04 May 2017 - 04:24 AM

You are passing a string to setInterval instead of a function. See line 133.

Quote

The error I'm getting is that timer isn't defined

Where you call Timer on line 117, you are not passing a value in so that will error when accessing time as it's undefined. which is probably what you mean by the error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1