I thought I was being smart...

I have a Camera Class that has an x and y of the viewing offset that I drag around with the mouse. I also wanted to start following players (at the start of their turn) and then follow projectiles when they are created (all these classes have an x and y as well). So, I thought that I could just make an Object named following:

Object following;

and then set it to camera, player1, rocket, etc… and then use it like:

g.translate(following.x, following.y);

but it don’t seem to work like that. Anyone got a sneaky little trick like that, that does work?

g.translate(-following.x + screen.width()/2, -following.y + screen.height()/2); maybe?

As the object should be in the centerpoint (0,0) of the view.

So if the Object is at 10x5y it needs to end up at 0x 0y, thus you use the negative value
of its position to translate everything.

adding half the screensize will move everything to the center. (else its always in the top-left corner 0,0 position)

(for more we need your code at this point…)

no, no. It’s not that. Even if I do this:

following = player1

player1 is of the Player class and has a .x and .y but following still won’t have a .x or .y. If I try to type cast following to Player:

g.translate((Player)following.x, (Player)following.y);

it crashes saying that following can’t be type casted to Player.

You should make a “IsCamObject” Interface then, wich every class implements.
And some getPosX() getPosY() methods.

Have some look into an Interfaces tutorial, it will help you more in the long run than making just some codeexample here.

Awesome! Thanks.

That makes me want to cry.

Why? Sorry just wondering cause I am also new, is there a better way to do something like this?

If you think of your class design as being a tree, with Object at the top, and each deriving class as children of the class that it’s extending, then the point of interfaces is where this model starts to break down and there are “cross cuts” in some horizontal manner across this picture. Or in other words it wouldn’t make sense (or is impossible) to put some set of methods high-up enough in the picture to provide the desired functionality. So creating an interface and having “all classes” implement it makes no sense. “All classes” should just extend a base class which provide.

NOTE: that since annotations are “new” language features, the older mechanism to “marking” classes as being of some special type was to use interfaces.

I disagree here. Depite the naming (should be more like ‘LocationProvider’ or something instead of ‘IsCamObject’), it’s a perfectly fine solution to separate interfaces to access specific information from the overall class hierarchy.

But you are right, that in this case it would probably be simpler to make a base class (Entity or ScreenObject or something), simply extend all Player, Enemy, Shot, whatever classes from it and make the following object of this base type.

are you sure it doesn’t say “following.x can’t be type casted to Player”?

g.translate(((Player)following).x, ((Player)following).y);

should work - but wouldn’t be a real solution, since you would still be forced to only use Player objects in following. It’s even worse, since it would shift the possible error from compile time to runtime. So makes it harder to debug.

There’s no reason you can’t do both:


public interface Located {
   int getX();
   int getY();
   void setLocation(int,int);
   void moveBy(int,int);
}
...
public class GameObject implements Located {
   ... blah blah ...
}

public class Player extends GameObject
public class Enemy extends GameObject
... etc ...

Anything that deals with updating locations can take a Located, and if you need to break out of the GameObject hierarchy, you’re free to. It’s nothing fancy like an Entity System but it’s perfectly serviceable.