...

View Full Version : Need help with functions etc.



GSimpson
08-01-2006, 12:47 PM
Hey I'm creating a game that has a 6 buttons below some ingame options etc.
Now I'm having problems with my levels and its script.

Below is my script.


<!-- Begin
function stats(){
var armour=3;
var defense=1;
var attack=1;
var total=attack+defense;
var disp=0;
if (disp==0) { alert("Your Attack Level Is:"+attack+"\nYour Defense Level Is:"+defense+"\nYour Armour Bonus

Is:"+armour+"\n_________________\n\nYour Total Level Is:"+total+"\nWith a bonus of "+armour+" Defense Points");}
}

function raiseattack(){
var goupa=0;
if (goupa==0) { alert("You gained One Level In Attack!"); attack=+1;}
}
// End -->

now when I click a button that has this code:

<INPUT TYPE=BUTTON VALUE="Train Attack" ONCLICK="raiseattack()">

The button displays the message that I have leveled up, but doesnt add 1 to the variable atack. so when I view the stats button its still displaying the attack to be one. I need it to raise by one every time this button is clicked.

PLEASE IF POSSIBLE NO ELEMENTS!!! i havent learnt them yet.

vwphillips
08-01-2006, 12:55 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
</head>

<body onload="stats();">
<script language="JavaScript" type="text/javascript">
<!-- Begin
var armour=3;
var defense=1;
var attack=1;

function stats(){
var total=attack+defense;
var disp=0;
if (disp==0) { alert("Your Attack Level Is:"+attack+"\nYour Defense Level Is:"+defense+"\nYour Armour BonusIs:"+armour+"\n_________________\n\nYour Total Level Is:"+total+"\nWith a bonus of "+armour+" Defense Points");}
}

function raiseattack(){
var goupa=0;
if (goupa==0) { attack++; alert("You gained One Level In Attack!\n"+attack); }
}


//-->
</script>
<INPUT TYPE=BUTTON VALUE="Train Attack" ONCLICK="raiseattack()">
</body>

</html>

Beagle
08-01-2006, 08:53 PM
if (goupa==0) { alert("You gained One Level In Attack!"); attack=+1;}
}


Your error is quite simply a problem with your syntax. Instead of attack =+ 1 it should be attack += 1

vwphillips
08-01-2006, 08:59 PM
local variable 'attack' needs to be global

GSimpson
08-02-2006, 01:00 AM
Thanks everyone, yeah I'm still learning javascript so i probably made sevral mistakes.

GSimpson
08-02-2006, 01:32 AM
Now problem no.2 I am using the script from the first reply.
A game just wouldn't be any good if you can keep leveling up... thats pretty pathetic.

So my solution to making it harder is having to wait. So when you click
the train attack button, I want it to have an alert that counts down 2minutes. and when it counts down to zero it has the alert - you leveled up!!!
then add one to the variable.
can someone help me out??

vwphillips
08-02-2006, 11:42 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>
<title></title>
</head>

<body onload="stats();">
<script language="JavaScript" type="text/javascript">
<!-- Begin
var armour=3;
var defense=1;
var attack=1;
var TO;

function stats(){
var total=attack+defense;
var disp=0;
if (disp==0) { alert("Your Attack Level Is:"+attack+"\nYour Defense Level Is:"+defense+"\nYour Armour BonusIs:"+armour+"\n_________________\n\nYour Total Level Is:"+total+"\nWith a bonus of "+armour+" Defense Points");}
}

function raiseattack(){
clearTimeout(TO);
var goupa=0;
if (goupa==0) { TO=setTimeout('raiseattack2()',5000); } // 5000 milliSec = 5 seconds
}

function raiseattack2(){
attack++;
alert("You gained One Level In Attack!\n"+attack);
}

//-->
</script>
<INPUT TYPE=BUTTON VALUE="Train Attack" ONCLICK="raiseattack()">
</body>

</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum