Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-01-2006, 11:47 AM   PM User | #1
GSimpson
Regular Coder

 
GSimpson's Avatar
 
Join Date: Aug 2006
Location: New Zealand
Posts: 268
Thanks: 9
Thanked 0 Times in 0 Posts
GSimpson is an unknown quantity at this point
Need help with functions etc.

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.
GSimpson is offline   Reply With Quote
Old 08-01-2006, 11:55 AM   PM User | #2
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,354
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<!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>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 08-01-2006, 07:53 PM   PM User | #3
Beagle
Senior Coder

 
Join Date: Jul 2005
Location: New York, NY
Posts: 1,084
Thanks: 4
Thanked 19 Times in 19 Posts
Beagle is an unknown quantity at this point
Quote:
Originally Posted by GSimpson
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
Beagle is offline   Reply With Quote
Old 08-01-2006, 07:59 PM   PM User | #4
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,354
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
local variable 'attack' needs to be global
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Old 08-02-2006, 12:00 AM   PM User | #5
GSimpson
Regular Coder

 
GSimpson's Avatar
 
Join Date: Aug 2006
Location: New Zealand
Posts: 268
Thanks: 9
Thanked 0 Times in 0 Posts
GSimpson is an unknown quantity at this point
Thanks everyone, yeah I'm still learning javascript so i probably made sevral mistakes.
GSimpson is offline   Reply With Quote
Old 08-02-2006, 12:32 AM   PM User | #6
GSimpson
Regular Coder

 
GSimpson's Avatar
 
Join Date: Aug 2006
Location: New Zealand
Posts: 268
Thanks: 9
Thanked 0 Times in 0 Posts
GSimpson is an unknown quantity at this point
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??
GSimpson is offline   Reply With Quote
Old 08-02-2006, 10:42 AM   PM User | #7
vwphillips
Senior Coder

 
Join Date: Mar 2005
Location: Portsmouth UK
Posts: 4,354
Thanks: 3
Thanked 458 Times in 445 Posts
vwphillips is a jewel in the roughvwphillips is a jewel in the roughvwphillips is a jewel in the rough
Code:
<!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>
__________________
Vic

God Loves You and will never love you less.

http://www.vicsjavascripts.org.uk/

If my post has been useful please donate to http://www.operationsmile.org.uk/
vwphillips is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:51 AM.


Advertisement
Log in to turn off these ads.