edit::Jeez I think I got it to work, of course i post it and 5 min later I solve it after trying to solve it for more than 6 hours I didn't haveugh, it still has to use setInterval(setStyle, 500) to update it every half second, am I just not calling setStyle correctly to trigger it manually when the points change?? if you look at the code below, to call setStyle(baseval) {} from another function what's the format: setStyle(); or setStyle(baseval); or setStyle(points); ?? or something else?Code:div.style.width = baseval + "px";
The setInterval is wrong I know, but it was the only thing that would cause the div to change, but it doesn't grow as the points variable increases.Code://Global Variables var points; //this is adjusted by other functions when you win/lose points var baseval = points; //i want baseval to grow as points increase function setStyle(baseval) { var div = document.getElementById("hp"); div.style.width = baseval + "px"; } if(kill monster1) {++points; respawn();} //add a point and respawn if(kill monster2) {++points; respawn()} //add a point and respawn if(killedBYmonster) {died();} //run died() function died() { points -= 3; //lose 3 points for dying setTimeout("respawn()", 2500); //set timeout to respawn, } setInterval(setStyle, 500); //constantly updates size of "hp" div
I tried to call setStyle(baseval) a 100 different ways and from various places, inside myIdeally, only when points change, would the bar adjust......I've tried calling setStyle(baseval) inside my respawn() function, thinking it would run it each time I kill a monster and it's set to respawn, but nothing will actually change the bar size except setInterval(setStyle, msdelay)Code:if(kill monster1) {++points; setStyle();} //also setStyle(baseval) or setStyle(points), etc, nothing works..
With setInterval(setStyle, msdelay) the healthbar adjusts constantly but it never gets above a sliver regardless of how high my score gets......
Any ideas? The variable "points" is made inside a js for a canvas game, the hp div isn't within the canvas, does that make a difference? I wanted to make some bars outside the canvas like health bar, progression bar, etc tied to variables but can't get the first one to work....



Reply With Quote

Bookmarks