7 Replies - 2366 Views - Last Post: 23 May 2009 - 08:06 AM

#1 chez007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-April 09

Variable in dice rolling game undefined

Post icon  Posted 10 May 2009 - 07:46 PM

Very new to Javascript only been doing it for eight weeks. In the course we have to make a dice rolling game which has the playing deposit an imaginary figure into their account of between 500 and 1000 by clicking on an image. (The figures are in multiples of 100).

I have done up the code for this but it keeps telling me that variable dp1 is undefined. I am also not even sure if I am on the right track with the coding.

<table border="0" width="450" height="50" bordercolordark="#330000" cellpadding="10" cellspacing="10">
	<tr>
	 <td width="65%" align="left" height="15"> 
	 <div align="left"><input type="image" src="images/500.gif" alt="Submit button"  height=70 width=200 value ="" onclick="make_deposit(dp1)"></div>
	</td>
<td width="65%" align="middle" height="15"> 
	  <div align="center"><input type="image" src="images/600.gif" alt="Submit button"  height=70 width=200 value ="" onclick= "make_deposit(dp2)"></div>
	</td>
<td width="100%" align="right" height="15"> 
	  <div align="right"><input type="image" src="images/700.gif" alt="Submit button"  height=70 width=200 value ="" onclick= "make_deposit(dp3)"></div>
	</td><br />
 	</tr>
<tr>
<td width="65%" align="left" height="30"> 
 <div align="left"><input type="image" src="images/800.gif" alt="Submit button"  height=70 width=200 value = "" onclick= "make_deposit(dp4)"></div>
	</td>
<td width="65%" align="middle" height="30"> 
	  <div align="center"><input type="image" src="images/900.gif" alt="Submit button"  height=70 width=200 value = "" onclick="make_deposit(dp5);"></div>
	</td>
<td width="65%" align="right" height="30"> 
  <div align="right"><input type="image" src="images/1000.gif" alt="Submit button"  height=70 width=200 value = "" onclick="make_deposit(dp6)";></div>
	</td>
</tr>
</table>

<script>
function make_deposit(dp1, dp2, dp3, dp4, dp5, dp6)
{
dp1=500
dp2=600
dp3=700
dp4=800
dp5=900
dp6=1000
}
alert(dp1)
</script>

<script>
var amountleft
function showbal() {
document.showbal.aleft.value=amountleft
timer=setTimeout("showbal()", 1000)
}
</script>
<form name="showbal">
<div align="center"><b>Amount left</b>:
<input type = "text" name= "aleft" size = "8" value=""></div>
</form>
<p>
<font color=#FFFF00 font size="3" font face="Comic Sans MS"><b></b>Step 1: </b>Click on the amount that you want to deposit into your account. When you have made a deposit. Go to Step 2.</p>



Any help would be greatly appreciated. :)

Is This A Good Question/Topic? 0
  • +

Replies To: Variable in dice rolling game undefined

#2 Bort  Icon User is offline

  • Ill-informed Mongoloid
  • member icon

Reputation: 445
  • View blog
  • Posts: 3,084
  • Joined: 18-September 06

Re: Variable in dice rolling game undefined

Posted 11 May 2009 - 02:59 AM

You may need to provide some additional info, like where the error is occuring. Have you tried debugging it using Firebug in Firefox?

Here is one idea though. When you call the 'make_deposit' function, you are telling the function to use values for dp1 - dp6, but you are setting these values inside the function.

This line:
function make_deposit(dp1, dp2, dp3, dp4, dp5, dp6)



should read:
function make_deposit(depositTextBoxValue)



where depositTextBoxValue is whatever is entered into the box that asks the user how much they would like to deposit. It might not even need the depositTextBoxValue.
Was This Post Helpful? 1
  • +
  • -

#3 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: Variable in dice rolling game undefined

Posted 13 May 2009 - 08:46 PM

from what I can see, you're writing alert(dp1), but that is outside the function where dp1 is defined. The alert wil execute first thing in the code you'rve shown. Even before the function is run, so dp1 will not be defined. I think that's your problem. Simple, huh?
Was This Post Helpful? 0
  • +
  • -

#4 chez007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-April 09

Re: Variable in dice rolling game undefined

Posted 14 May 2009 - 08:44 PM

View PostBort, on 11 May, 2009 - 01:59 AM, said:

You may need to provide some additional info, like where the error is occuring. Have you tried debugging it using Firebug in Firefox?

Here is one idea though. When you call the 'make_deposit' function, you are telling the function to use values for dp1 - dp6, but you are setting these values inside the function.

This line:
function make_deposit(dp1, dp2, dp3, dp4, dp5, dp6)



should read:
function make_deposit(depositTextBoxValue)



where depositTextBoxValue is whatever is entered into the box that asks the user how much they would like to deposit. It might not even need the depositTextBoxValue.





I have been able to put the value of 500 to 1000 into the textbox but still not too sure if I'm going in the right direction with this game. When the user clicks on the image and deposits the amount into the textbox it must alert them that they have put 500 etc into their account. We do I put the alert? Should I be using IF and Else statements for this game?

The details of the game are

* The user will click a figure to deposit into their account from a list of images (500, 600 etc)

* The user must then venture an amount of either 100 or 200.

*They then pick a value from 3 buttons ...1to 4, 5to8, 9to12 that they think the dice will roll.
They must click a button that rolls two dice.
Two dice images will be displayed that shows the 'result' of the roll , if the total of the two dice match their selection (1-4, 5-8, 9-12), they double their venture capital, otherwise they lose their venture capital.
This amoutn must be added or subtracted from their account balance.

I have been working on this project for weeks, I have the doll rolling part down but I just can't seem to work at the first part with depositing the value 500, 600etc into their account.

Just need some help to get me started then I think I can work the rest out. Thank you



<table border="0" width="450" height="50" bordercolordark="#330000" cellpadding="10" cellspacing="10">
	<tr>
	 <td width="65%" align="left" height="15"> 
	 <div align="left"><input type="image" src="images/500.gif" alt="submit" name="aleft" size=10 value ="Transfer" onclick="document.showbal.aleft.value=500" ></div>
	</td>
<td width="65%" align="middle" height="15"> 
	  <div align="center"><input type="image" src="images/600.gif" alt="submit" name="aleft" size=10 value ="Transfer" onclick="document.showbal.aleft.value=600"></div>
	</td>
<td width="100%" align="right" height="15"> 
	  <div align="right"><input type="image" src="images/700.gif" alt="submit" name="aleft" size=10 value ="Transfer" onclick="document.showbal.aleft.value=700"></div>
	</td><br />
 	</tr>
<tr>
<td width="65%" align="left" height="30"> 
 <div align="left"><input type="image" src="images/800.gif" name="aleft" alt="submit" size=10 value ="Transfer" onclick="document.showbal.aleft.value=800"></div>
	</td>
<td width="65%" align="middle" height="30"> 
	  <div align="center"><input type="image" src="images/900.gif" alt="submit" name="aleft" size=10 value ="Transfer" onclick="document.showbal.aleft.value=900"></div>
	</td>
<td width="65%" align="right" height="30"> 
  <div align="right"><input type="image" src="images/1000.gif" alt="submit" name="aleft" size=10 value ="Transfer" onclick="document.showbal.aleft.value=1000"></div>
	</td>
</tr>
</table>



<script>
var amountleft
function showbal() {
document.showbal.aleft.value=amountleft
timer=setTimeout("showbal()", 1000)
}
</script>



<form name="showbal">
<div align="center"><b>Amount left</b>:
<input type = "text" name= "aleft" size = "8" value=""></div>
</form>
<p>
<font color=#FFFF00 font size="3" font face="Comic Sans MS"><b></b>Step 1: </b>Click on the amount that you want to deposit into your account. When you have made a deposit. Go to Step 2.</p>
<hr />

Was This Post Helpful? 0
  • +
  • -

#5 Bort  Icon User is offline

  • Ill-informed Mongoloid
  • member icon

Reputation: 445
  • View blog
  • Posts: 3,084
  • Joined: 18-September 06

Re: Variable in dice rolling game undefined

Posted 15 May 2009 - 01:25 AM

For the alert, change this:

dp6=1000
}
alert(dp1)
</script>



To this:

dp6=1000
alert("You have deposited $" + dp1) //or whatever currency you'll be using
}
</script>



Why not have a single variable for the amount deposited, then use an If... statement to set the value depending on which image is clicked. something like this:

In the image tags, put 'onclick="make_deposit(this)"'

function make_deposit(imgclicked){
	var deposit
	If imgClicked.Name = img100 then
		deposit = 100
	elseif imgClicked.Name = img200 then
		deposit = 200
...and so on.

	alert("You have deposited $" + deposit);
}



Also, make sure you have your semicolons ( ; ) all in the right place. I've had many problems with Javascript just by missing those off the end of lines :)

Hope this helps,
Bort

This post has been edited by Bort: 15 May 2009 - 01:56 AM

Was This Post Helpful? 0
  • +
  • -

#6 chez007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-April 09

Re: Variable in dice rolling game undefined

Posted 17 May 2009 - 07:47 PM

View PostBort, on 15 May, 2009 - 12:25 AM, said:

For the alert, change this:

dp6=1000
}
alert(dp1)
</script>



To this:

dp6=1000
alert("You have deposited $" + dp1) //or whatever currency you'll be using
}
</script>



Why not have a single variable for the amount deposited, then use an If... statement to set the value depending on which image is clicked. something like this:

In the image tags, put 'onclick="make_deposit(this)"'

function make_deposit(imgclicked){
	var deposit
	If imgClicked.Name = img100 then
		deposit = 100
	elseif imgClicked.Name = img200 then
		deposit = 200
...and so on.

	alert("You have deposited $" + deposit);
}



Also, make sure you have your semicolons ( ; ) all in the right place. I've had many problems with Javascript just by missing those off the end of lines :)

Hope this helps,
Bort




Thanks for that Bort. That helps alot.
Was This Post Helpful? 0
  • +
  • -

#7 chez007  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 28-April 09

Re: Variable in dice rolling game undefined

Posted 21 May 2009 - 06:24 PM

Hi,

Still having trouble with this dice game. I now have the amount going into the text box but after the amount goes in there when you click the image it then reverts back to undefined.

I have the code written so the text box amount continually updates itself. The code is

<script>
var amountleft;
function showbal() {
document.showbal.aleft.value=amountleft
timer=setTimeout("showbal()", 1000)
}
</script>
<form name="showbal">
<div align="center"><b>Amount left</b>:
<input type="text" name="aleft" size="8" value=""></div>
</form>




Here is the code for the user to click on the image and then deposit ($500, $600 etc) into their account.

<script>
function make_deposit(imgclicked){
var amountleft = amountleft + imgclicked;
document.showbal.aleft.value=imgclicked
alert("You have deposited $" + imgclicked);
}
</script>

<form="myform">
<input type="image" src="images/500.gif" alt="submit1" name="aleft" onclick="make_deposit(500)">
<input type="image" src="images/600.gif" alt="submit2" name="aleft" onclick="make_deposit(600)">
<input type="image" src="images/700.gif" alt="submit3" name="aleft" onclick="make_deposit(700)">
<input type="image" src="images/800.gif" alt="submit4" name="aleft"  onclick="make_deposit(800)">
<input type="image" src="images/900.gif" alt="submit5" name="aleft" onclick="make_deposit(900)">
<input type="image" src="images/1000.gif" alt="submit6" name="aleft" onclick="make_deposit(1000)">
</form>




Hope someone can help.

Thanks
Was This Post Helpful? 0
  • +
  • -

#8 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Variable in dice rolling game undefined

Posted 23 May 2009 - 08:06 AM

When you declare var amountleft in the function it overrides the other already existing amountleft variable, so the original one doesn't get changed. Just take out the var part of the statement so you're not redeclaring it.

This post has been edited by crazyjugglerdrummer: 23 May 2009 - 08:07 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1