Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Steven Wheeler
920 PointsBig problems with objects in JS/jQuery, scope problems maybe?
Hey there guys, the link to my code is under here:
http://codepen.io/swhizzle/pen/zGxOvx
I have only been coding for about a month and I know what I am attempted is probably overambitious or whatnot. However, I have been trying to build a little RPG battle system using jQuery and regular Javascript but I am encountering some problems.
As I was developing it, I would "refresh" the page to get a new monster and I encountered no problems. It was only until I added a "new battle" button that would run a battle() function to elimate the need to refresh the page that problems started to occur.
Basically once the player has won (the enemies can't hurt you yet) you click "new battle" and it gives you a new monster. The problem occurs here, the previous monster's information is still being used by the battle() function! I would like to overwrite the previous monster and do battle with the current one instead (as refreshing the page would do).
In the code I started to set new variables equal to the object's hitpoints etc as to avoid altering the original but this doesn't seem to avoid the problem.
I've been working on this for about 8 hours today (as well as the rest of the "rpg" heh) but its only until recently that I have realised this huge bug if you don't want to refresh the page everytime.
I hope I have explained myself enough, the bug is pretty obvious. I wish I could be more specific as to what I think the bug could be, but I am pretty much clueless at the moment; sorry.
Also, the css file has a few items that are not relevant to the "battle system" and can be ignored :).
Any help would be HUGELY appreciated.
Steve
1 Answer
Jon Brady
10,868 PointsHi that's a very interesting project your working on, I'm no expert (yet) but here's a line that might need looking at var randomNum = Math.floor(Math.random()*5); try adding +1 like this var randomNum = Math.floor(Math.random()*5) + 1; probably wont work but... whatever... give it a shot :).
Steven Wheeler
920 PointsSteven Wheeler
920 PointsUnfortunately that changes nothing, but thank you for trying hehe.
I think the problem is how I determine the battle to be over (monster health being less than zero). I think I'm going to mess around with using a "if(isAlive == false)" thing tomorrow.
Failing that... I'm not sure. I might have to make a different instance of a class (Goblin, Dragon etc) everytime a new battle starts. Maybe that's a more logical way of trying to do this, I'm not sure.