Jump to content

onKeyUp Problems


destrugter

Recommended Posts

Ok, so to get to the point, I have run into this problem with the onKeyUp event.I have 2 input fields. Each with an onKeyUp event attribute. They are very similar in which, when you update input B, it goes through a function to update input A. The problem is the values for input A vary widely for each value of input B.In different terms, I am running a formula for the inputs to determine what the value of the other should be. My problem is that when you update input B, it updates input A perfectly...but when you try to put in a seperate value for input A (say 1+ the value in there), it updates it, then triggers the onKeyUp for input B and resets the value to the lower one.Here are the parts of my code that are necassary to see. If a better explination is needed, please ask...I have been working my pants off to get this working, and it just doesn't want to go.The functions(in Javascript)

<script type=\"text/javascript\">    function experience(L)  {	points = 0;	output = 0;	minlevel = 2; // first level to display	maxlevel = L-1; // last level to display	for (lvl = 1; lvl <= maxlevel; lvl++)	{	  points += Math.floor(lvl + 300 * Math.pow(2, lvl / 7.));	}	return Math.floor(points / 4);  }  function Level(xp)  {	points = 0;	output = 0;	minlevel = 2; // first level to display	maxlevel = 2; // last level to display	for (lvl = 1; lvl <= maxlevel; lvl++)	{	  points += Math.floor(lvl + 300 * Math.pow(2, lvl / 7.));	  	  if(Math.floor(points / 4) > xp)	  {		level = maxlevel-1;	  }	  Else	  {		maxlevel++;	  }	}	return level;  }  function update(updateWhat, xp, level)  {	if(updateWhat = \"Experience\")	{	  y=document.getElementById(level).value;	  document.getElementById(xp).value = experience(y);	}		if(updateWhat = \"Level\")	{	  y=document.getElementById(xp).value;	  document.getElementById(level).value = Level(y);  	}  }  function syncRemain(current, goal, remainDIV, remain)  {	xpCurrent = eval(document.getElementById(current).value);	xpGoal = eval(document.getElementById(goal).value);		xpRemaining = xpGoal - xpCurrent;		document.getElementById(remainDIV).innerText = xpRemaining;		document.getElementById(remain).value = xpRemaining;  }  function updateAmounts(loopNumber, xpRemainId)  {	for(i=1; i<=loopNumber; i++)	{	  xpRemain = eval(document.getElementById(xpRemainId).value);	  	  amountsLeft = Math.ceil((xpRemain) / (aItemExperience[i]));		  divId = \"amount\" + i;	  document.getElementById(divId).innerText = amountsLeft;	}  }</script>

The Inputs

			  Experience:<input type='text' id='goalXp' onKeyUp=\"update('Level', 'goalXp', 'goalLevel');\" size='10' maxlength='9'>			  Level:<input type='text' id='goalLevel' onKeyUp=\"update('Experience', 'goalXp', 'goalLevel'); syncRemain('currentXp', 'goalXp', 'xpRemaining', 'xpRemain'); updateAmounts($amounts, 'xpRemain');\" size='5' maxlength='3'>

~~~~~~~~~~~~~~~~~ EDIT ~~~~~~~~~~~~Well...to be a little more clear.The level field worked. If you put in a value from 1 to 99, it returned the correct xp into the Experience input.Now, I removed the level updating function, and instead used just the experience to see if it works on its own, and it does not.So my problem lies within my formula for obtaining the level.Here is what I did with the formulas.

  function experience(L)  {	points = 0;	output = 0;	minlevel = 2; // first level to display	maxlevel = L-1; // last level to display	for (lvl = 1; lvl <= maxlevel; lvl++)	{	  points += Math.floor(lvl + 300 * Math.pow(2, lvl / 7.));	}	return Math.floor(points / 4);  }

This is the experience formula used in the game RuneScape to determine how much experience you need to get from each level (from 1 to 99).So, you put in the Level (L) and it runs a for loop that adds the points all up until you reach the level, and returns the points divided by 4.What I wanted to do from there is reverse the formula, and I couldn't find an easy way to do it. The easiest way I could figure it out was to run the exact same script, but instead, put an if statement that declared that if the current experience yielded becomes higher than the user's input at the first instance, it should break the for loop and return the current level - 1.Here is that code

  function Level(xp)  {	points = 0;	output = 0;	minlevel = 2; // first level to display	maxlevel = 2; // last level to display	for (lvl = 1; lvl <= maxlevel; lvl++)	{	  points += Math.floor(lvl + 300 * Math.pow(2, lvl / 7.));	  	  if(Math.floor(points / 4) > xp)	  {		level = maxlevel-1;	  }	  Else	  {		maxlevel++;	  }	}	return level;  }

Any suggestions as to why it returns Level 0, then automatically updates my experience field to 0 (even when there is no argument for the onKeyUp event)?

Link to comment
Share on other sites

Is there any chance you can isolate the problem more specifically, explain it more specifically, and bombard us with less code? The fact that you have received no responses in 12 hours probably reflects our collective reluctance to deal with so much stuff at one time.

Link to comment
Share on other sites

or maybe ask to have this post moved to the Javascript sub-forum?As far as I can tell, in most games, you can't just level yourself automatically just by giving yourself a level to be at...but maybe thats a feature of your game?It seems, in concept, this should be just as simple as passing the value of the experience input to the function, which just checks the value of the function against a bunch of pre-conditions, in the case the experience points need to be at certain levels and then just update the level field when the experience matches that levels range of experience points.

function LevelUp(xp){   if(xp >= 0 && < 100){	  //set level input value to level 1   }else if(xp >= 100 && < 200){	 //set level input value to level 2   };  //so and so forth};

Link to comment
Share on other sites

Maybe I should have mentioned it's not a game I am making. I am making level calculators for a game, and that formula is the formula that retruns the experience when you plug in the level.For example, Level 2 is reached when you get 83 xp, this means that from 0 to 82 xp, you are still Level 1.Level 3 is reached at 174 xp, so from 83 to 173, you are level 2.The formula wasn't found by me. I tried figuring out a way to reverse it, and since there is no real way to do it, I made that if statement, that obviously fails.

Link to comment
Share on other sites

it seems to me the range doesn't matter then. you give the function a number (1 - 99) and it outputs a number associated with that level. You could just use an array for that. Unless you somehow want to give a random number within that level range...which is something else entirely.

Link to comment
Share on other sites

No, I don't want a random number.It is just a formula that is used to determine what the experience has to be for the player to get to the next level.The first formula

  function experience(L)  {	points = 0;	output = 0;	minlevel = 2; // first level to display	maxlevel = L-1; // last level to display	for (lvl = 1; lvl <= maxlevel; lvl++)	{	  points += Math.floor(lvl + 300 * Math.pow(2, lvl / 7.));	}	return Math.floor(points / 4);  }

This works just fine. Putting in any number (1-99 is the only sensible range since the highest level is 99), will return the xp value.If you were to do...experience(2);It would return 83.And so on until 99.experience(3) returns 174.experience(4) returns 276.So, that part works fine. The player can enter any level, and it returns the amount of xp you have at that level.Now say you are level 3, and want to get to level 4. Any value greater than 174, but less than 276 yields you in level 3 still. Once you get 276, you are level 4. The thing is, when you enter level 4 into the Level field, it will return the experience in the experience field. Now what I want, is so when the user types in experience, it takes that, and finds the level.So if you did your level as 3, it returned 174.What if you have 120 experience...you would be level 2...but this just doesn't even make it past 0 anymore.Edit: I apologize if I sound impatient, in fact it's the opposite...I just want to make it clear what my problem is.

Link to comment
Share on other sites

I think what scientist said before is the way you want to go with this.

It seems, in concept, this should be just as simple as passing the value of the experience input to the function, which just checks the value of the function against a bunch of pre-conditions, in the case the experience points need to be at certain levels and then just update the level field when the experience matches that levels range of experience points.
function LevelUp(xp){   if(xp >= 0 && < 100){	  //set level input value to level 1   }else if(xp >= 100 && < 200){	 //set level input value to level 2   };  //so and so forth};

It sounds like you just need to check to see if the experience entered is between two numbers. Ex. For level 2 you need between 83 and 173 experience, so the structure in the code above would be able to check that. But I think I do know what your problem is: you don't necessarily know what the ranges are, right?Well, the above function could be modified to look like this in that case:
function LevelUp(xp){   if(xp >= experience(1) && < experience(2)){	  //set level input value to level 1   }else if(xp >= experience(2) && < experience(3)){	 //set level input value to level 2   }  //so and so forth}

That will run the experience function to calculate the experience required for the given level ranges.

Link to comment
Share on other sites

What I wanted to do from there is reverse the formula, and I couldn't find an easy way to do it. The easiest way I could figure it out was to run the exact same script, but instead, put an if statement that declared that if the current experience yielded becomes higher than the user's input at the first instance, it should break the for loop and return the current level - 1.Here is that code...
Actually, looking back on the OP, I think you can just add the line I have commented below. You mention breaking the loop but never do.
  function Level(xp)  {	points = 0;	output = 0;	minlevel = 2; // first level to display	maxlevel = 2; // last level to display	for (lvl = 1; lvl <= maxlevel; lvl++)	{	  points += Math.floor(lvl + 300 * Math.pow(2, lvl / 7.));	  	  if(Math.floor(points / 4) > xp)	  {		level = maxlevel-1;		break; //Break the loop here as you mention	  }	  Else	  {		maxlevel++;	  }	}	return level;  }

Link to comment
Share on other sites

I actually did have a break, before I tried the method I had posted.The method I posted should update the maxlevel, so the loop repeats indefinitely until it reaches the level the person is, then it subtracts 1, thus ending the loop...but that doesn't work either.

Link to comment
Share on other sites

I actually did have a break, before I tried the method I had posted.The method I posted should update the maxlevel, so the loop repeats indefinitely until it reaches the level the person is, then it subtracts 1, thus ending the loop...but that doesn't work either.
Indeed. Upon taking a closer look at that function you are right. The loop should end, but not immediately however. Your for loop should not have an '=' sign for the condition. It will run one extra time after it finds the level. But that should still return the right level. What do you get when you run that function?
Link to comment
Share on other sites

Sorry, I'm not actually able to view any links since I'm at work, but I was looking through some the code you had originally posted. I noticed that in the update function you have some incorrect syntax for your if statements:if(updateWhat = \"Experience\")That should actually look like this:if(updateWhat == "Experience")The single '=' sign actually assigns that value to updateWhat, making that condition always true. Also I'm not sure what you're trying to accomplish with the '\' characters. Those are used within strings to escape special characters. If they are part of the value of updateWhat then it should look like this:if(updateWhat == "\"Experience\"")The same thing goes for the if statement for the level.

Link to comment
Share on other sites

I have the \ sign because I am using php, and the javascript is located within an echo statement.
Ah, well that makes sense.
Thanks for your help, I can't believe I didn't see that.
No problem. My instructor in college always said that sometimes all it takes is a second set of eyes. :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...