2

I am a complete beginner to programming, but I've spent the last month reading and looking through tutorials.

I am stuck in my current project. I'm set up an entities package, with a class called Mob inside. From Mob, I've created Player and a monster class called Slime.

Now, I am really having a hard time getting the slime to move randomly, but before I even worry about that I have to figure out why I can't position him correctly.

When I do my addEntity(Slime) into my main class, with a x and y parameters, it ignores that and places him at 1,1.

Here is my Slime monster class: http://pastie.org/5404386

My entity class with the addEntity function: http://pastie.org/5404390

and my main class where I call in the slime and (attempt) to set its position parameters:

slime = new Slime(level, "Slime", 5, 5, 1);
level.addEntity(slime);
player = new Player(level, 11,11, input,JOptionPane.showInputDialog(this,"Please enter a Username."));
level.addEntity(player);

Thanks for any help!

  • Can you provide the `Mob` class source? Also, do you have variables `x` and `y` defined in both `Mob` *and* `Slime`? – mellamokb Nov 20 '12 at 18:13
  • Here is my entire Mob class: http://pastie.org/5407384 x and y are both private int in the Slime class, and and public int in the Entity class – user1837518 Nov 20 '12 at 18:27
  • Ah I see. Why do you have `x` and `y` defined in both `Entity` and `Slime`? The variables in `Slime` are going to override the variables in `Entity`, so that when you reference `x`, you are actually getting `Slime.x`. However, when you call `super(`, you are storing the values in static variables `Entity.x` and `Entity.y`. And why are they static variables? Do all entities always have the same location at all time? That's what static indicates - a global variable shared by all instances of the class. – mellamokb Nov 20 '12 at 18:29
  • 1
    Think about it this way: variables should be defined in a class hierarchy where they are the most general. Since position is a thing common to all `Entity` objects, the position should be defined only there, and inherited by all the subclasses. Colour may or may not be a global property, depending on how you are using it; but if all `Entity` objects have a meaningful use for a colour, then define it there instead of on `Slime`. The class `Slime` should only be concerned with attributes, behaviors, and properties that are specific only to `Slime` objects, but not any of its parent classes. – mellamokb Nov 20 '12 at 18:32
  • Thank you so much! That makes a lot of sense. I'm going to study what you've wrote and rework all my code! – user1837518 Nov 20 '12 at 19:07
  • @mellamokb That fixed it perfectly and I made sure to change my static int in Entity. Could you possibly recommend a way to get the slime to move occasionally? If I use thread.sleep the entire game stops. And I can't start slime as its own thread because of the way it's nested (I think). – user1837518 Nov 20 '12 at 19:27

0 Answers0