This site requires JavaScript, please enable it in your browser!
Greenfoot back
Scoter_man1
Scoter_man1 wrote ...

2012/5/11

Problem calling methods of actors from other actors

Scoter_man1 Scoter_man1

2012/5/11

#
I'm trying to make a game in which the main character collects something like coins. So, in that main character I have a variable set up to store the number of coins collected. In the Coins class, I am trying to call the method I created in the main character's class to set that variable. Here's the code in short (main character) private int Coins=0; public int getCoins() { return Coins; } public void setCoins(int num) { Coins=Coins+num; } and for the Coin class public Coin() { Maincharacter character = (Maincharacter) getOneIntersectingObject(Maincharacter.class) if (character != null) { Maincharacter.setCoins(5); } } Now, when I call try to compile, it tells me that non-static method setCoins(int) cant be referenced from a static context. I'm not really sure whats wrong. I've tried so many different things so I'd appreciate it if y'all could help.
kiarocks kiarocks

2012/5/11

#
change
Maincharacter.setCoins(5);
to
character.setCoins(5);
kiarocks kiarocks

2012/5/11

#
also, the above in your constructor should go in your Coin's act method.
Scoter_man1 Scoter_man1

2012/5/11

#
Thanks! That's helping me with so much more than just that one issue.
You need to login to post a reply.