jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes accessing method in another file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "accessing method in another file" Watch "accessing method in another file" New topic

accessing method in another file

Matthew Barker

Joined: Oct 29, 2002
Posts: 4
hi, the answer to my question is probably simple but i cant for the life of me work it out.
I'm making a simple asteroid game and to keep track of the score i want to use a method in the main file (asteroid.java) that keeps track of the score by modifying a textfield.

I want to call this method from my canvas file (gameArea.java) when a meteor is destroyed. but when i compile the whole applet i get the message
.\GameArea.java:187: cannot resolve symbol
symbol : method scoreIncrease (int)
location: class GameArea
1 error
the class files are public and the method is public.
Wilfried LAURENT
Ranch Hand

Joined: Jul 13, 2001
Posts: 269
You must find a way to make a relation between GameArea and Asteroid
1. Who instantiates GameArea? If it's Asteroid, then you may have in Asteroid.java something like:

In GameArea.java, then:

A second solution would be to use the Observer pattern. GameArea would be Observable, Asteroid would be the observer. Asteroid registers as an Observer of GameArea. When a meteor is destroyed, then GameArea sends a update message to its Observer. Then the scoreIncrease method is called in the body of the update method in the Asteriod class. Hence no problem.
A third solution would be to give a reference on the textArea to GameArea.
A fourth solution ....
Matthew Barker

Joined: Oct 29, 2002
Posts: 4
Thanks heaps, it worked perfectly. It was driving me crazy.
Have you checked out Aspose?
subject: accessing method in another file
It's not a secret anymore!