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 7 of 7

Thread: Method sharing question.

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Amused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Method sharing question.

    Just a quick question. What is a way this could be coded?

    Imagine this:
    • I have a class called "Monster."
    • Class "Werewolf" extends "Monster."
    • "Werewolf" has a method called "update()" for updating everything that's in "Werewolf."
    • "Werewolf" has a method called "anim()" for animating a walking pattern.
    • "Werewolf" has many variables that are used in "anim()." Mainly "Speed."


    What I'm trying to do:
    • Make it so "anim()" can be moved to "Monster" and still work.
    • Make it so "update()" in "Werewolf" contains "{anim();}" but in a way that "anim()" uses the variables in "Werewolf."


    What I tried, but didn't work:
    • As soon as I moved the method, I noticed I had to fix something with the variables. I can't just put the variables in "Monster" because, sure, werewolves are monsters, but I'm trying to make it so in every subclass of "Monster" I only need to make new variables and call the method "anim()" in some way.
    • Someone told me to use "this.*methodNameHere*()" but it doesn't work.


    I want to call a method from a superclass in every subclass. The method has a variable "Speed" that is a different value in every subclass. The variable is the same name in every subclass, like this:

    Werewolf.Speed
    Ghost.Speed
    Clown.Speed

    And in those subclasses, there's a method "update()." Like this:

    Werewolf.update();
    Ghost.update();
    Clown.update();

    I'm not sure how to describe it, but I want those updates to contain "anim()" in a way that this happens:

    • Werewolf.update(); calls Monster.anim(); in a way that Werewolf.Speed is used.
    • Ghost.update(); calls Monster.anim(); in a way that Ghost.Speed is used.
    • Clown.update(); calls Monster.anim(); in a way that Clown.Speed is used.


    All I'm trying to do is use a method as a simple framework for animating an animation, keeping the method in a superclass while letting all of its subclasses call the method, while using their own variables/variable values.

    So, all I really need is a way to call methods but in some way that the values change. Like Monster.Speed is the basis and the subclass variables of the same name somehow extend that? I don't know.

    This thing that I'm trying to do is extremely new to me and I don't know where to go.
    Lol I've been stuck on this one problem for 2 weeks.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Method sharing question.

    Why can't you pass the speed from each subclass to the parent method, something like:

    In class Werewolf:
    super.anim( speed ); // (the 'super' isn't required, but helps make it clear)

    Where speed is Werewolf's speed.

    I think that answers the rest of your question about update(), but I'm not sure.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Rexoa (February 9th, 2014)

  4. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Amused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Method sharing question.

    That was exactly what I was looking for.
    Making variables IN the method while make a constructor solved part of my problem, but now I have an issue with one certain variable.

    Say I have 4 animated sprites in my "Werewolf" class:
    AnimatedSprite up,
    AnimatedSprite down,
    AnimatedSprite left,
    and AnimatedSprite right.


    I have this in "Monster:"
    public void moveAnim(AnimatedSprite up, AnimatedSprite down, AnimatedSprite left, AnimatedSprite right) {
    		AnimatedSprite animSprite = down;
    		if (walking) animSprite.update();
    		else animSprite.setFrame(0);
    		if (movingUp) {
    			dir = Direction.UP;
    			animSprite = up;
    		} else if (movingDown) {
    			dir = Direction.DOWN;
    			animSprite = down;
    		}
    		if (movingLeft) { 
    			dir = Direction.LEFT;
    			animSprite = left;
    		} else if (movingRight) { 
    			dir = Direction.RIGHT;
    			animSprite = right;
    		}
    	}
    "animSprite" is a variable used to set the direction to either up, down, left, or right. Never equal to more than 1 of the variables.

    The problem is animSprite. I can't find a way to use it since it's defined in a method and not in the class.
    I'm trying to get that animSprite variable in the method to go here:
    sprite = animSprite.getSprite();
    There's a red line under "animSprite" that I can't get rid of.
    Is there a way to use moveAnim's animSprite variable?

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Method sharing question.

    By convention in Java, class names are capitalized, variable and method names begin with lowercase letters, and all are camel-cased thereafter. Constants (or final variables) are typically all caps with names separated by underscores.

    Your moveAnim() method seems way more complicated than it needs to be, and it's hurting my head, so I haven't tried to simplify it. See if you can do it.

    As for your question about the variable animSprite, define it outside the method as an instance variable, and then give access to the variable by other classes using a 'getter' (or accessor) method:

    public class Animal  // or whatever the right class is
    {
        AnimatedSprite animSprite;
     
        //  etc . . . 
     
        public void moveAnim()
        {
            animSprite = down;  // however it's set
     
            // etc. . . .
     
        }
     
        // etc . . .
     
        // an accessor that allows outside access to the variable animSprite
        public AnimatedSprite getAnimSprite()
        {
            return animSprite;
        }
     
    }
    You should have a firm foundation that includes a complete understanding and familiarity with these OOP concepts and practices before starting a project of this magnitude. You'll ignore me and continue on. When you end up with a big pile of code that doesn't do what you want, you don't know how to fix it, and you give up frustrated, remember you were told. Slow down, take a few steps back, and learn the basics.

  6. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Amused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Method sharing question.

    Oh, haha. Sorry if it hurt your head, I was showing that animSprite's value is supposed to change according to direction.
    This is the whole method I should have shown before:
    public void moveAnim(AnimatedSprite up, AnimatedSprite down, AnimatedSprite left, AnimatedSprite right, double xa, double ya) {
    		animSprite = down;
    		if (walking) animSprite.update();
    		else animSprite.setFrame(0);
    		if (ya < 0) {
    			dir = Direction.UP;
    			animSprite = up;
    		} else if (ya > 0) {
    			dir = Direction.DOWN;
    			animSprite = down;
    		}
    		if (xa < 0) { 
    			dir = Direction.LEFT;
    			animSprite = left;
    		} else if (xa > 0) { 
    			dir = Direction.RIGHT;
    			animSprite = right;
    		}
    		if (xa != 0 || ya != 0) { 
    			move(xa, ya);
    			walking = true;
    		} else {
    			walking = false;
    		}
    	}
    Your solution works, but the way I have my AnimatedSprite class coded, animSprite has to be equal to a variable or else I get a
    null pointer exception. I think.

    It seems, even with "animSprite = down;" animSprite is still null, so, with all honesty, I have no clue what's going on.

    Here's where the issue started off:
    In the constructor, I made a spot for separate animSprite variables to go from each subclass, but, again, animSprite had to be initialized, so I set it to "down." That made the character's animation only show the downward animation. So it's like I either set it to down or else it's null.

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Method sharing question.

    It seems, even with "animSprite = down;" animSprite is still null
    Impossible, unless: 1) down is null, or 2) you've created another animSprite variable that hasn't been properly initialized. If animSprite = down, then somewhere 'down' should have been initialized as in:

    down = new AnimatedSprite();

  8. #7
    Junior Member
    Join Date
    Feb 2014
    Posts
    12
    My Mood
    Amused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Method sharing question.

    Indeed, down is initialized by this line:

    public AnimatedSprite down = new AnimatedSprite(SpriteSheet.dummy_down, 32, 32, 4);

    Nothing in that line is null, so I'm guessing it all has to do with animSprite needing to be initialized outside of a method .

    And the only animSprite variable I can see is the one I initialized in moveAnim().

    EDIT: I got rid of the animSprite variable and used up, down, left, and right in its place,
    while moving some other things around.

    Everything seems to be working fine, I just need to reorder some code. Thanks again.

Similar Threads

  1. Sharing the particular object
    By mohamed_mashood in forum Object Oriented Programming
    Replies: 2
    Last Post: June 27th, 2013, 12:37 AM
  2. Problem sharing objects between threads
    By pepzi999 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 18th, 2012, 04:02 PM
  3. Objects sharing data
    By masterkale in forum Java Theory & Questions
    Replies: 2
    Last Post: March 5th, 2012, 12:00 AM
  4. BoxLayout: Sharing ISN'T Caring... :(
    By snowguy13 in forum AWT / Java Swing
    Replies: 5
    Last Post: December 24th, 2011, 11:21 AM
  5. Extending a Servlet and sharing HttpServletRequest
    By mastervh in forum Java Servlet
    Replies: 6
    Last Post: October 12th, 2011, 03:51 AM

Tags for this Thread