Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-13-2004, 11:42 AM
|
#1
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374
Rep:
|
java, call function in calling object
I have a class called Game.class, inside it creates an instance of another class called GameCharacter.class, from GameCharacter is it possible to call a function inside Game.class?
example
class Game impliments CanBeUsedByCharacter
{
GameCharacter Bob = new GameCharacter();
//other stuff
public void DoSomething()
{
}
//other stuff
}
class GameCharacter
{
if (CallingObject instanceof CanBeUsedByCharacter)
{
(CanBeUsedByCharacter)(CallingObject).DoSomething();
}
}
that is essentially what I want to do, I know I can pass Game into GameCharacter with the "this" keyword, but this is a watered down version of what I am doing, and while the "this" keyword will work it is extreamely ugly for my purposes
|
|
|
06-13-2004, 11:43 AM
|
#2
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374
Original Poster
Rep:
|
let me specify, I have a lot of object with sub-objects with yet more sub-objects, all from a main object, they all need to be able to through events that are intercepted by yet another object that only contains the Game object, and the functiosn to through the event are in the Game object.
|
|
|
06-13-2004, 03:33 PM
|
#3
|
Senior Member
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120
Rep:
|
I'm not sure exactly what you are wanting to do, sounds even, a bit like inter-thread communication, but seeing as GameCharacter appears to have no relationship at all to Game I'm at a bit of a loss how GameCharacter can call anything in Game directly as it doesn't at first glance appear to know it exists, of course there are probably a ton more of enlightened folk out there. Now if it GameCharacter was an inner/member class of Game that would be different. It all seems a bit upside down 
|
|
|
06-13-2004, 03:37 PM
|
#4
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374
Original Poster
Rep:
|
ok let me explain what I am doing:
there is the main game object, any interaction done with game from a frontend is done here.
every time a character moves the game object throws an event to the frontend saying hey update the display a character moved, he moved from x, y to x, y. however each chracter object is it's own thread, it controls when and where it moves, so it needs to be able to call the updatedisplay event caster that is inside of the game object.
|
|
|
06-13-2004, 04:35 PM
|
#5
|
Senior Member
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120
Rep:
|
Okay, I'm assuming you are only going to have one "Game", yeah?
So how about making the relevant methods that you want to call static.
i.e
public static void doSomething(){
blah...blah
}
Then call it by
Game.doSomething();
|
|
|
06-13-2004, 04:48 PM
|
#6
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374
Original Poster
Rep:
|
well, the game is an active game, and the dosomething function is actually pass an event to listeners, and the game object is keeping track of the listeners, so it can't be as you say..
let me rephrase:
game object contains characters, event catsers, and the list of things listening for events, th characters in the game object need to tell the game object to throw an event to the listener.
|
|
|
06-13-2004, 06:25 PM
|
#7
|
Senior Member
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120
Rep:
|
Struggling a bit, are the characters data structures or do they also extend gui components, just wondering becuase if they were you could use a component listener and when it move it would fire a component moved event which could call whatever routine you want
|
|
|
06-13-2004, 06:34 PM
|
#8
|
Senior Member
Registered: Aug 2003
Location: Portland, Oregon
Distribution: Arch
Posts: 1,374
Original Poster
Rep:
|
I have figured out a different way to doit, thank you for your help.
|
|
|
06-13-2004, 06:40 PM
|
#9
|
Senior Member
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120
Rep:
|
No problem, for all the good I was, anyway seeing as I'm interested, what was the general way you got round what you are trying to do?
|
|
|
06-13-2004, 11:49 PM
|
#10
|
Member
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216
Rep:
|
Quote:
Originally posted by exodist
let me specify, I have a lot of object with sub-objects with yet more sub-objects, all from a main object, they all need to be able to through events that are intercepted by yet another object that only contains the Game object, and the functiosn to through the event are in the Game object.
|
Java has a really slick mechanism for notifying one object that another object has changed. Look at java.util.Observer and java.util.Observable. Here's a toy example to demonstrate the idea.
The first class extends Observable. Whenever the state of the object changes, you call setChanged() to indicate that the object has changed, then call notifyObservers() or notifyObservers( Object arg ) to tell all of the Observers of the object that the state has changed. The Observers can either query the Observable object for its new state via accessor methods, or the Observable object can pass an Object argument to all of the Observers. For this little example, whenever the field x in MyObservable changes, it notifies all of the Observers passing the new value of x to the Observers.
Code:
// MyObservable.java
import java.util.Observable;
public class MyObservable extends Observable {
private int x = 0;
public void setX( int x ) {
this.x = x;
setChanged();
notifyObservers( new Integer( x ) );
}
}
The next class implements the Observer interface. This interface has only one method: update( Observable o , Object arg ). In this example, it just gets the new value of x and prints it out.
Code:
// MyObserver.java
import java.util.Observable;
import java.util.Observer;
public class MyObserver implements Observer {
public void update( Observable o , Object arg ) {
Integer i = (Integer)arg;
System.out.println( i );
}
}
The last class is just the main driver of the whole thing. It creates a MyObservable and MyObserver, and adds the MyObserver to the MyObservable. Then it updates the MyObservable's x value so that the MyObserver will print the value out.
Code:
// Observation.java
public class Observation {
public static void main( String[] args ) {
MyObservable observable = new MyObservable();
MyObserver observer = new MyObserver();
observable.addObserver( observer );
for ( int x = 0 ; x < 10 ; x++ ) {
observable.setX( x );
}
}
}
One of the cool things about using Observable/Observer is that you can have different Observers that react differently to changes in the Observable without Observable having to know anything about the Observers.
For the problem you are working on, the Game could be an Observer of each of the GameCharacters. When a GameCharacter moves it can notify the Observer (the Game) of its new position by passing a Point with the new locations. Likewise the frontend can be an Observer of the Game. When the Game is notified by the GameCharacter of the character's new position, the Game can notify the frontend.
Last edited by eric.r.turner; 06-14-2004 at 01:36 AM.
|
|
|
All times are GMT -5. The time now is 06:15 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|