简体   繁体   中英

calling static methods from other classes

so i'm having some problems integrating scoreloop into my game. I use cocos2dx which is written in c++ and uses the ndk. The main application class is derived from activity and not from android.app.application. Adding a button to the layout and using it to bring up a scoreboard or submit a score works,but it doesn't connect to the internet. i've found a solution to this here : scoreloop support forum or more specifically

Yes, using libgdx seems to be the issue. libgdx brings it's own Application class that is actually derived from Android's Activity, not Application. The helloworld sample from libgdx does not come with an (Android) Application class at all, here is how to add one:

Create a new class that extends android.app.Application (not com.badlogic.gdx.backends.android.AndroidApplication)

In the AndroidManifest.xml find the tag and the name of the created class as an attribute: android:name="YourApplication"

Add the method public void onCreate() to that class and initialize Scoreloop there.

so following that i created this :

public class scoreLooped extends android.app.Application{
public void onCreate(Bundle savedInstanceState){
    ScoreloopManagerSingleton.init(this, "redacted");
} 
 public void onTerminate()
 {
     ScoreloopManagerSingleton.destroy();
 }

}

and i create this class from my main activity class like this :

public class wordsweeper extends Cocos2dxActivity implements OnScoreSubmitObserver{
private Cocos2dxGLSurfaceView mGLView;
private static scoreLooped a;

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    wordsweeper.a = new scoreLooped();
    //Set the observer equal to an instance of this class
    ScoreloopManagerSingleton.get().setOnScoreSubmitObserver(this);

and the last line is where it crashes with an error "Caused by: java.lang.IllegalStateException: ScoreloopManagerSingleton.init() must be called first" so obviously my scoreLooped class doesn't call the scoreloopmanager. I've thought about using the scorelooped class to submit,retrieve scoreboards but it seems that i can't do that without extending activity. I'm pretty new to java so i might be missing something obvious so it would be great if somebody could point me in the right direction.

It seems that you need to create your own Application class and call ScoreloopManagerSingleton.init() there. See the answer here, which references some example documentation: http://support.scoreloop.com/discussions/problems/789-illegalstateexception-scoreloopmanagersingletoninit-can-be-called-only-once

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM