This site requires JavaScript, please enable it in your browser!
Greenfoot back
RedPhoneBooth
RedPhoneBooth wrote ...

2012/4/14

Public boolean values updating

RedPhoneBooth RedPhoneBooth

2012/4/14

#
Hey, Well ive been having this problem for a while and nothing seems to be wrong with my code but obviously something is. I'm working on an adventure game and am trying to implement combat. What happens is when I press space, I change a the boolean SPACEBAR to true, in the world class, some code picks that value up and adds a "swoosh" at a certain point in relation to the knight (ill change it when ive figured out this to account for the direction the knights facing), in the swoosh class, I have a method called setRotate(); in this method, the swoosh picks up 4 variables from the knight which ive coded to make return the direction the knight is facing, only one of which is true at any given time. This previous part works, ive inspected it and its returning the right values. In the swoosh class, what should happen is it picks up the variables and sets the rotation to the correct direction based on the direction the knight is facing. Everything seems sound but what's happening is that through inspecting the knight through the swoosh, all 4 variables are in the position their initialized in, so it doesnt actually change the swoooshe's direction unless you re-set the initialized statement. So it seems that the boolean variables are updating within the knight, but not within the knight object I created within the swoosh. I'm not really sure what's wrong here, or should I have 4 different methods for each variable that return the boolean variable that should be changed. But my version should work shouldn't it? I dont know whats going wrong really... Oh and you can see the project on my scenarios (its the only one ive posted) and im posting the code for it (although none of it is commented).
nccb nccb

2012/4/14

#
In the Swoosh class, you say:
Knight player = new Knight();
But in Town, you also have:
Knight player = new Knight();
So the "player" in Swoosh is a different knight (one that isn't in the world) to the "player" in Town (which is actually the one in the world). The right solution is really to pass the player to the Swoosh's constructor, but if you're happy with public variables, then the easiest solution might be to make the "player" variable in Town public, then remove Swoosh's player and change Swoosh to use this code at the start of act:
Town town = (Town)getWorld();
Knight player = town.player;
...
RedPhoneBooth RedPhoneBooth

2012/4/16

#
Thanks works great!
You need to login to post a reply.