js math function help

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Nov 2006
Posts: 66
Reputation: Racoon200 is an unknown quantity at this point 
Solved Threads: 1
Racoon200's Avatar
Racoon200 Racoon200 is offline Offline
Junior Poster in Training

js math function help

 
0
  #1
Jul 1st, 2007
Iam making a game, the error is that in the "d" loop (for...) the value "holeBalls" stays always the same.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. for (d=holeNum;d<=holeBalls;d++)
  2. {
  3. document.getElementById('hole'+d).value++
  4. }
  5. }
what i want is that holeBalls here, gets the value of "'hole'+id" because now what happens is that (this is a kalah game) when you click on a hole with 4 holeBalls, then the next hole has 5 holeBalls right? When you click on it, the balls will only spread out in 4 pieces, when the purpose is to give the next 5 holes one piece, but in this code i made, one piece gets lost...
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. // variables
  2. gameStarted = false;
  3. gemDif = 4;
  4. function getGemDif()
  5. {
  6. gemDif = 4
  7. var MySelect = document.getElementById("dif");
  8. gemDif = MySelect.options[MySelect.selectedIndex].value;
  9. document.getElementById("gemsAmount").value = gemDif;
  10. }
  11.  
  12. function start()
  13. {
  14. gameStarted = true;
  15. n = 1;
  16. for (n=1;n<=12;n++)
  17. {
  18. document.getElementById('hole'+n).value = gemDif;
  19. document.getElementById('hole'+n).style.backgroundImage = 'url(img/hole_gem.png)';
  20. }
  21. }
  22.  
  23. function move(hole)
  24. {
  25.  
  26. if (gameStarted == true)
  27. {
  28. changeSrc(hole)
  29. holeID = hole.id
  30. holeNum = holeID.replace(/hole/,'')
  31. holeBalls = hole.value
  32. hole.value = 0
  33. holeNum++
  34. holeBalls++
  35. for (d=holeNum;d<=holeBalls;d++)
  36. {
  37. document.getElementById('hole'+d).value++
  38. }
  39. }
  40.  
  41. if(gameStarted == false)
  42. {
  43. alert('Select a difficulty and start the game!')
  44. }
  45. }
  46.  
  47. function changeSrc(clicked)
  48. {
  49. clicked.style.backgroundImage = 'url(img/hole_empty.png)';
  50. }

IF YOU WISH, I CAN SEND YOU THE WHOLE WEBPAGE SO THAT YOU TEST IT AND UNDERSTAND IT BETTER
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,620
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 468
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: js math function help

 
0
  #2
Jul 2nd, 2007
> I am making a game, the error is that in the "d" loop (for...) the
> value "holeBalls" stays always the same.
It will remain the same because you never update it in the loop and always use it to perform the bound check.

Maybe something like this:
  1. for (d=holeNum;d<=holeBalls;d++)
  2. {
  3. var elem = document.getElementById('hole'+d);
  4. elem.value++;
  5. holeBalls = elem.value;
  6. }

If not this, then maybe you should give us some live working version of the game so that we can understand your predicament.
Last edited by ~s.o.s~; Jul 2nd, 2007 at 6:08 am.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 66
Reputation: Racoon200 is an unknown quantity at this point 
Solved Threads: 1
Racoon200's Avatar
Racoon200 Racoon200 is offline Offline
Junior Poster in Training

Re: js math function help

 
0
  #3
Jul 2nd, 2007
Yes, Ill better give you the zip (attached below).

It is supposed that when you click a ball, the value has to be shared in equal quantities within the next (value) balls.

i.e.
if the hole has x balls, then, when the hole is clicked, the next x holes will have one more ball each. The clicked hole will have no more balls left.
x = amount of balls of the clicked hole
Attached Files
File Type: zip kalah game.zip (506.3 KB, 2 views)
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC