Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How can i modify this to work ?

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    My Mood
    Cold
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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.



     
    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.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    9
    My Mood
    Cold
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default 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.

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default 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

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How can i modify this to work ?

    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.

Similar Threads

  1. Having Trouble Modify the code!!
    By jehov86 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 2nd, 2012, 09:42 AM
  2. How to modify System.out.println()
    By emailkia in forum Java Theory & Questions
    Replies: 6
    Last Post: April 25th, 2011, 12:40 AM
  3. File I/O Modify Text Problem
    By Fordy252 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 26th, 2010, 05:30 AM
  4. Modify Colors in a Picture
    By theuniverse in forum Java Theory & Questions
    Replies: 0
    Last Post: October 17th, 2009, 04:49 PM
  5. how to modify a JList Frame?
    By JM_4ever in forum AWT / Java Swing
    Replies: 0
    Last Post: October 14th, 2009, 11:58 PM