Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

accessing method in another file

 
Matthew Barker
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
scoreIncrease(10);
^
1 error
the class files are public and the method is public.
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ....
W.
 
Matthew Barker
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks heaps, it worked perfectly. It was driving me crazy.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic