Really don't know why API doesn't work

13 posts

Flag Post

I tried the both two methods: loading swf from web and using swc.

For the loading swf method, I do:

   private function InitKongregateAPI ():void 
   {
      var paramObj:Object = LoaderInfo(stage.loaderInfo).parameters; 
      var apiPath:String = paramObj.api_path || "http://www.kongregate.com/flash/API_AS3_Local.swf"; 
      Security.allowDomain(apiPath); 
      var request:URLRequest = new URLRequest (apiPath); 
      var loader:Loader = new Loader(); 
      loader.contentLoaderInfo.addEventListener (Event.COMPLETE, OnKongregateApiLoadComplete); 
      loader.load (request);
      stage.addChild (loader);
   }
   
   private var mKongregate:Object = null; 
   private function OnKongregateApiLoadComplete( event:Event ):void 
   {
      event.currentTarget.removeEventListener(Event.COMPLETE, OnKongregateApiLoadComplete);
      
      mKongregate = event.target.content;
trace ("kong API loaded: mKongregate = " + mKongregate); // this one is traced out
      mKongregate.services.connect();
      mKongregate.user.getPlayerInfo (OnKongregateGetUserInfo);
   }
   
   private function OnKongregateGetUserInfo ( playerData:Object ):void
   {
      trace ("isGuest: "+ playerData.isGuest); // this one is never traced out. strange!
   }
   
   private function SubmitStat (name:String, value:Number):void
   {
      mKongregate.stats.submit (name, value);
      
trace (“submitted.”); // this is traced out, but refresh the game page on Kongregate, no HighScore tab shown
   }

For the swc method:

   import com.kongregate.as3.client.KongregateAPI; 
   import com.kongregate.as3.client.events.KongregateEvent;
   
   private function InitKongregateAPI ():void
   {
      var kongregate:KongregateAPI = new KongregateAPI();
      kongregate.addEventListener (KongregateEvent.COMPLETE, OnKongregateApiLoadComplete2);
      stage.addChild (kongregate);
   }
   
   private function OnKongregateApiLoadComplete2 ( e:KongregateEvent ):void
   {
      trace (“kongregate API inited”); // this is traced out
      var kongregate:KongregateAPI = KongregateAPI.getInstance();
      kongregate.user.getPlayerInfo (OnKongregateGetUserInfo); 
   }

   private function OnKongregateGetUserInfo ( playerData:Object ):void
   { 
      trace ("222 isGuest: "+ playerData.isGuest); // this is also traced out, false.
   }
   
   private function SubmitStat (name:String, value:Number):void
   {
      var kongregate:KongregateAPI = KongregateAPI.getInstance();
      kongregate.user.getPlayerInfo (OnKongregateGetUserInfo);
      
      trace ("user name: " + kongregate.user.getName ()); // my user name is traced out
      kongregate.stats.submit (key, value);

      // no errors, but refresh the game page on Kongregate, still no HighScore tab shown
   }

Very frustrated, don’t know what is wrong in the code. The second method was ever successful in my old games.

 
Flag Post

You should try QuickKong. It will handle all of the loading and initializing data for you.

 
Flag Post

Ok, I tried the QuickKong and retried my first method.
They both throw an error at:


api.services.connect();

By adding some traces, I found:
1. api is not null
2. api.hasOwnProperty (“services”)
3. api.services is null !!!

So strange!

 
Flag Post

I just rebuilt one of my old games, which uses the swc method.
But the kongregate high score API in it also doesn’t work now.
Totally have no ideas now.

 
Flag Post

I just downloaded one the my old games and uploaded it as an unpublished game.
The high score tab is also not shown in the preview page.
Messed up. :(

 
Flag Post

For QuickKong did you try calling QuickKong.connectToKong(stage); before calling anything else.

If QuickKong.services is null, then you aren’t giving it enough time to load or you haven’t called that function yet.

 
Flag Post

I write a simple program to use QuickKong
Click to submit score.
Ok, api.services is not null. But the HighScore tab is still not shown.
You can try it by yourself, please remember to add two stats: HighScore and Test before testing it.
You can download the project zip file here: http://www.colorinfection.com/TestQuickKong.zip

package
{
   import flash.display.Sprite;
   import flash.text.TextField;
   import flash.events.Event;
   import flash.events.MouseEvent;
   
   public class Game extends Sprite 
   {
      private var mTextField:TextField = new TextField ();

      public function Game ()
      {
         mTextField.textColor = 0xFFFFFF;
         mTextField.width = 250;
         mTextField.height = 500;
         addChild (mTextField);
         
         addEventListener(Event.ADDED_TO_STAGE , OnAddedToStage);
      }
      
      private function OnAddedToStage (event:Event):void
      {
         stage.addEventListener (MouseEvent.MOUSE_DOWN, OnMouseDown);
         
         mTextField.appendText ("Initizing ...\n");

         QuickKong.connectToKong (stage, OnKongApiInited);
      }
      
      private var mKongApiInited:Boolean = false;
      private function OnKongApiInited ():void
      {
         mKongApiInited = true;
         
         mTextField.appendText ("Initized!\n");
      }
      
      private var mFakeScore:int = 0;
      private function OnMouseDown (event:MouseEvent):void
      {
trace ("[OnMouseDown] mKongApiInited = " + mKongApiInited);

         if (mKongApiInited)
         {
            ++ mFakeScore;
            
            mTextField.appendText (QuickKong.services.getUsername());
            mTextField.appendText ("> to submit score: ");
            mTextField.appendText (mFakeScore.toString ());
            
trace ("[OnMouseDown] mFakeScore = " + mFakeScore);
trace ("[OnMouseDown] QuickKong.stats = " + QuickKong.stats);

            QuickKong.stats.submit ("Test", mFakeScore);
            QuickKong.stats.submit ("HighScore", mFakeScore);
            
            mTextField.appendText (" [done]\n");
         }
      }
   }
}
 
Flag Post

The callback on QuickKong.connectToStage() is a pre connect callback that is used for the shared content API. There is not a callback for completion in QuickKong (I’ve written versions that do).

When QuickKong does load the API you should be getting a trace saying “[QuickKong] loadedAPI()”.

Also, if Game is not your Document Class (first class that runs) then you should have a removeEventListener(Event.ADDED_TO_STAGE, OnAddedToStage); inside OnAddedToStage otherwise it will likely trigger twice.


EDIT The full trace for a working QuickKong implementation should look like:

[QuickKong] connectToKong()
Alert: Kongregate API shadow services loaded due to local testing. API will load when the game is uploaded.
[QuickKong] loadedAPI()
Kongregate API: IKongregateServices.connect() 
[QuickKong] Kong API Successfully loaded and extracted. Shadow Services alert should appear for local testing

EDIT 2:
If the only issue after this is highscore tab not showing, usually that happens because Kongregate has not detected enough scores submitted (assuming the API is working). Maybe when players click the play button in your game, you should submit a play stat with a value of 1.

 
Flag Post

The Game class is the Document Class.
The output traces are exactly as what your said:


[QuickKong] connectToKong()
[QuickKong] loadedAPI()
[QuickKong] Kong API Successfully loaded and extracted. Shadow Services alert should appear for local testing
[OnMouseDown] mKongApiInited = true
[OnMouseDown] mFakeScore = 1
[OnMouseDown] QuickKong.stats = [object StatServices]
[OnMouseDown] mKongApiInited = true
[OnMouseDown] mFakeScore = 2
[OnMouseDown] QuickKong.stats = [object StatServices]

The zip file I posted above contains the released swf file, you can try to upload it in your account to check if the HighScore tab will be shown.

For this is just a test, it is not suitable to publish it to receive more players. Once it is published, no ways to remove it.

I remember, long long ago, the HighScore tab was ever shown in the preview page, even if the stat is submitted only once. If the HighScore needs many scores to show, I think Kongregate really should tweak it.

 
Flag Post

Yeh, I agree that it needs to be a bit more accurate.

I’m not sure if they can do a whole bunch of changes for it and, sadly, I think that it isn’t as high of a priority as other things on their list.

 
Flag Post

After this discussion, I’m more confused than before starting this discussion. :(

 
Flag Post

My apologies for confusing you. I was answering multiple questions that you had not asked (in anticipation that you would ask them or to comment on something).

So, as long as the API prints out what it should be in the trace panel, you really should be good to go.

Upload to Kongregate and make sure you have added all of the variables you submit and make sure to check “Display in Leaderboards”. If your highscore board doesn’t show up by then, you will just have to trust that it will show up sometime down the road when enough stats have been submitted.

 
Flag Post

haha,
ok, it is shown now, by select the “Display in leaderboards”
really didn’t know and notice this option.

Thanks!