Implementing the Kongregate High Scores API
I consider myself an “alright” programmer. So when something that is supposed to be simple in programming frustrates me, I feel like a complete moron. Enter the Kongregate API, chances are if you are reading this post you have dealt with it in some fashion. Now if the documentation was all over the place and split up (and done in an actual OOP example) it might actually make some since. Hence the reason for this post. I hope to clear things up a bit.
First, to check out the documentation here. Nice huh? Well hopefully I don’t screw it up more. You will notice that there are few options to which you can implement said API. I recommend the SWC since you can have code hinting and its right there at your disposal. Or you can go with downloading it remotely (this post does not deal with the component). If you decide to download it at runtime check out this guys post. If you do go with that dude’s way make sure you listen to the KongregateEvent.COMPLETE event. (this is what threw me off completely)
Ok, so this is going to be short and sweet. When you use the SWC way I recommend having a reference to it that you can access from anywhere. Let’s not argue about OOP practices and evil singletons, just roll with it. I like to throw a reference to it in my “Model” which I have access to anywhere, this could be said about any class that you are storing persistent data through out your game. Then in your “Main” or Document class make a new KongregateAPI (NOT getInstance() like the docs say and then listen for the KongregateEvent.COMPLETE event. Those two things completely threw me off because I heard different stories from different sources… as dumb as that makes me look. Then finally where you want submit a score do so in your “Results” class. (Imports NOT included in code, I assume you are just putting those in)
In your “Model”:
public var kongregate:KongregateAPI;
In your “Main” or Document Class:
Security.allowDomain('http://www.kongregate.com');
_model = Model.getInstance();
_model.kongregate = new KongregateAPI();
_model.kongregate.addEventListener(KongregateEvent.COMPLETE, kongInit);
stage.addChild(_model.kongregate);
private function kongInit(event:Event):void {
_model.kongregate.services.connect();
}
In your “Results” Class:
_model = Model.getInstance();
_model.kongregate.scores.submit(_score);