944,085 Members | Top Members by Rank

Ad:
Jul 1st, 2007
0

js math function help

Expand Post »
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
Similar Threads
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
Racoon200 is offline Offline
66 posts
since Nov 2006
Jul 2nd, 2007
0

Re: js math function help

> 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:
javascript Syntax (Toggle Plain Text)
  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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Jul 2nd, 2007
0

Re: js math function help

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, 9 views)
Reputation Points: 13
Solved Threads: 1
Junior Poster in Training
Racoon200 is offline Offline
66 posts
since Nov 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Hit counter
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: highlighting text in the text area





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC