Forum Moderators: open

Message Too Old, No Replies

inserting a time into this game.

need some help

         

urgirljavabeginner

8:49 am on Apr 29, 2004 (gmt 0)

10+ Year Member



ok well i have this lil game i'm trying to make...and i put a time thing in but the time isnt counting down. i need help in fixing it count down.

this is wat i have on my javascript.

gamelength=4;
rounds=10;
timerID=null
var playing=false;
var numpics=20*31;
var currentpostion=-1;


function clearpic() {
for(var i=0;i<document.images.length;i++)
document.images[i].src="blank.bmp";
}

function stoptimer() {
if(playing)
clearTimeout(timerID);
}

function showtime(remaintime) {
document.controlpanel.timeleft.value=remaintime;
if(playing) {
if(remaintime==0) {
stopgame();
return;
}
else {
current=remaintime-1
timerID=setTimeout("Showtime(current)",1000);
}
}
}

function stopgame() {
stoptimer();
playing=false;
document.controlpanel.timeleft.value=0;
clearpic();
display("Game Over");
alert('Game Over.\nYour Score is: '+totalhits);
}

function startgame() {
if(playing) {
stopgame();
}
playing=true;
clearpic();
totalhits=0;
document.controlpanel.score.value=totalhits;
display("Playing");
launch();
showtime(gamelength);

setInterval(cycle(),4000);
}

function starttime() {
x--;
if(x==0)
stopgame();
}

function display(message) {
document.controlpanel.state.value=message;
}

function launch(){
var launched=false;
while(!launched) {
mypic=random();
if(mypic!=currentpostion) {
document.images[mypic].src="basketball.jpeg";
currentpostion=mypic;
launched=true;
}
}
}

function hithead(id) {
if(playing==false) {
clearpic();
display("Push New Game to Play");
return;
}
if(currentpostion!=id) {
totalhits+=-1;
document.controlpanel.score.value=totalhits;
}
else {
totalhits+=1;
document.controlpanel.score.value=totalhits;
launch();
document.images[id].src="blank.bmp";
}
}

function random() {
return(Math.floor(Math.random()*639*100%numpics));
}

i need some help caus ei don tknow how to put the time in proper...

Bernard Marx

10:47 am on Apr 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This may do it (it's definately an error)
Line 67:
setInterval(cycle(),4000); 

to

setInterval("cycle()",4000); 

I couldn't find a

cycle()
function in your script, but perhaps you have it somewhere else.

urgirljavabeginner

6:21 pm on Apr 29, 2004 (gmt 0)

10+ Year Member



i still have error.

gamelength=4;
rounds=10;
timerID=null
var playing=false;
var numpics=20*31;
var currentpostion=-1;

var arrow=new Array();

for(x=0; x<=2; x++){
arrow[x]=new Image();
}
arrow[0].src="basketball.jpeg";

var x;
x=Math.round(2*Math.random());

function cycle()
{
if(x<2)
{
x=x+1;
}
else
{
x=0;
}
document.images[0].src=arrow[x].src
}

function clearpic() {
for(var i=0;i<document.images.length;i++)
document.images[i].src="blank.bmp";
}

function stoptimer() {
if(playing)
clearTimeout(timerID);
}

function showtime(remaintime) {
document.controlpanel.timeleft.value=remaintime;
if(playing) {
if(remaintime==0) {
stopgame();
return;
}
else {
current=remaintime-1
timerID=setTimeout("Showtime(current)",1000);
}
}
}

function stopgame() {
stoptimer();
playing=false;
document.controlpanel.timeleft.value=0;
clearpic();
display("Game Over");
alert('Game Over.\nYour Score is: '+totalhits);
}

function startgame() {
if(playing) {
stopgame();
}
playing=true;
clearpic();
totalhits=0;
document.controlpanel.score.value=totalhits;
display("Playing");
launch();
showtime(gamelength);

setInterval("cycle()",4000);
}

function starttime() {
x--;
if(x==0)
stopgame();
}

function display(message) {
document.controlpanel.state.value=message;
}

function launch(){
var launched=false;
while(!launched) {
mypic=random();
if(mypic!=currentpostion) {
document.images[mypic].src="basketball.jpeg";
currentpostion=mypic;
launched=true;
}
}
}

function hithead(id) {
if(playing==false) {
clearpic();
display("Push New Game to Play");
return;
}
if(currentpostion!=id) {
totalhits+=-1;
document.controlpanel.score.value=totalhits;
}
else {
totalhits+=1;
document.controlpanel.score.value=totalhits;
launch();
document.images[id].src="blank.bmp";
}
}

function random() {
return(Math.floor(Math.random()*639*100%numpics));
}

Bernard Marx

8:12 pm on Apr 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As I haven't got the page thi is working on, I can't run the script.
Can you locate the error?

What does the error say?
Which line number?

Bernard Marx

9:07 pm on Apr 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a tip, it might be relevant to this problem, it might just be a snotty attempt at advice.

Watch out for your variables, and try to keep them local if you can. This avoids conflicts, and can make the code more efficient too.

[pre]
[blue]/* A global var, x is created.
It comes out of the loop with the value 2.
I'm assuming the loop is so short because it's
just a test version */[/blue]

for(x=0; x<=2; x++){
arrow[x]=new Image();
}

[blue]/* Now x is declared again. In another language that might have caused an error.
IE's script engine doesn't seem to mind. Not necessary though.*/[/blue]
var x;
arrow[0].src="basketball.jpeg";

var x;
x=Math.round(2*Math.random()); [blue]// gives: 0¦1¦2 [/blue]

function cycle()
{
[blue]/*
Replace this with:
x = (x+1)%3
*/[/blue]
if(x<2)
{
x=x+1;
}
else
{
x=0;
}

[blue]/*Perhaps the error's here.
Have you assigned .src for arrow[1] and [2]? */[/blue]
document.images[0].src=arrow[x].src

}

[/pre]

urgirljavabeginner

11:05 pm on Apr 29, 2004 (gmt 0)

10+ Year Member



ok..let me try it...

i kinda stop working on it cause i got frustrated. but lets see.