Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (492)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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 950 times)
0 Members and 1 Guest are viewing this topic.
Offline gbeebe

Senior Member


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:
1  
Object following;
and then set it to camera, player1, rocket, etc... and then use it like:
1  
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 Member


Medals: 5
Projects: 1



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

no, no.  It's not that.  Even if I do this:
1  
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:
1  
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 Member


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: 4
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 Ninja


Medals: 45



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

1  
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"?
1  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (33 views)
2014-08-22 19:31:30

atombrot (42 views)
2014-08-19 09:29:53

Tekkerue (41 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (26 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!