How can i modify this to work ?
Why doesn't this sort of thing work ?
I created a method that takes one String, and that method will set a new path of an ImageIcon based on the parameter passed in.
I had planned on using it as a neat little method to swap the players current image form different classes.
based on KeyEvents trigged, to reflect a player jumping, or walking and so forth.
Code :
private static String player_ii_path = "";
static ImageIcon player_ii = new ImageIcon(player_ii_path) ;
static Image player = player_ii.getImage() ;
public static void set_new_player_ii_path(String p_new_player_ii_Path)
{
player_ii_path = p_new_player_ii_Path ;
if(p_new_player_ii_Path.equals("jump_up"))
{
player_ii_path = "images\\player_jump_up.png" ;
}
}
Thank You.
Re: How can i modify this to work ?
So what does not work? Have you verified the values of the variables through the method to see if it does what you want it to do?
Where do things go wrong? as in, how do you know it is not working? You never really said what the problem is or what is not working.
Re: How can i modify this to work ?
Oh right OK. when i pass in a string to the method as the method does expect one string parameter
say i passed in "jump" as in this example as the parameter of the method.
1. the if statement of the method should validate what the String player_ii_path value should be set to, based on that parameter is passed in "jump"
2 the player_ii ImageIcon should take the value of the string as its own parameter
3. player Image takes the value of the imageIcon
4. I call this method on keyPressed with the parameter of "jump" hoping this will change the appearance of the player Image
Its suppose to be a versatile reusable method of my game engine to update the players image based on the virtual keys triggered.
--- Update ---
Oh right OK. when i pass in a string to the method as the method does expect one string parameter
say i passed in "jump" as in this example as the parameter of the method.
1. the if statement of the method should validate what the String player_ii_path value should be set to, based on that parameter is passed in "jump"
2 the player_ii ImageIcon should take the value of the string as its own parameter
3. player Image takes the value of the imageIcon
4. I call this method on keyPressed with the parameter of "jump" hoping this will change the appearance of the player Image
Its suppose to be a versatile reusable method of my game engine to update the players image based on the virtual keys triggered.
Re: How can i modify this to work ?
The function can change the player_ii_path String, but does not change the values of the player_ii object or the player object.
Bottom line: Nothing that you have shown can result in any visible changes.
Cheers!
Z
Re: How can i modify this to work ?
Quote:
Its suppose to be a versatile reusable method of my game engine to update the players image based on the virtual keys triggered.
I would not be looking up images when the player tries to jump, walk, run, turn, etc... I would have those images loaded and ready to be drawn long before the jump button is pushed. In the player's draw method, on-the-fly as the player draws it's own graphics, the player should determine if it is jumping, landing, walking, running, etc and draw the correct, pre-loaded image. (note that I said determine the state, not advance or make any state changes, never make any changes while drawing)
Explain what the variable player_ii_path is for. It is not declared locally but the method is changing the value at least once (even if it is replacing the same value). The variable is getting changed when the method is called, there is no way around it. The question is, after you change 'that' variable what happens. Is that the variable used to determine the path later? Is this variable to be shared or does it belong to player_ii only? Which bring me to a side note: Variable names like p_new_player_ii_Path make reading the code very difficult.