Hi !
Featured games (84)
games approved by the League of Dukes
Games in Showcase (604)
Games in Android Showcase (171)
games submitted by our members
Games in WIP (653)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  I thought I was being smart...  (Read 1160 times)
0 Members and 1 Guest are viewing this topic.
Offline gbeebe

Senior Devvie

Medals: 5
Projects: 1

« Posted 2011-12-07 05:02:34 »

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?
Offline Damocles
« Reply #1 - Posted 2011-12-07 05:26:34 »

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

Offline gbeebe

Senior Devvie

Medals: 5
Projects: 1

« Reply #2 - Posted 2011-12-07 05:34:23 »

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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Damocles
« Reply #3 - Posted 2011-12-07 05:43:15 »

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.

Offline gbeebe

Senior Devvie

Medals: 5
Projects: 1

« Reply #4 - Posted 2011-12-07 06:00:52 »

Awesome!  Thanks.
Offline Roquen
« Reply #5 - Posted 2011-12-07 14:53:39 »

You should make a "IsCamObject" Interface then, wich every class implements.

That makes me want to cry.
Offline StonePickaxes

JGO Coder

Medals: 5
Projects: 2

Nathan Kramber

« Reply #6 - Posted 2011-12-07 14:57:04 »

You should make a "IsCamObject" Interface then, wich every class implements.

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?

Check out my website!
Offline Roquen
« Reply #7 - Posted 2011-12-07 15:15:42 »

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.
Offline cylab

JGO Wizard

Medals: 89

« Reply #8 - Posted 2011-12-07 17:51:39 »

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.

g.translate((Player)following.x, (Player)following.y);
it crashes saying that following can't be type casted to Player.
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.

Mathias - I Know What [you] Did Last Summer!
Offline sproingie

JGO Kernel

Medals: 202

« Reply #9 - Posted 2011-12-07 18:09:08 »

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.
Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

SHC (25 views)
2015-08-01 03:58:20

Jesse (19 views)
2015-07-29 04:35:27

Riven (40 views)
2015-07-27 16:38:00

Riven (21 views)
2015-07-27 15:35:20

Riven (24 views)
2015-07-27 12:26:13

Riven (14 views)
2015-07-27 12:23:39

BurntPizza (36 views)
2015-07-25 00:14:37

BurntPizza (46 views)
2015-07-24 22:06:39

BurntPizza (30 views)
2015-07-24 06:06:53

NoxInc (37 views)
2015-07-22 22:16:53
List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!