I'm getting the notification that I'm getting a high score. When I was working on
Space Junk, the table wouldn't show up for an hour or two. Here's what I did in FlashPunk (and it worked!).
So, Flash is pretty bad (especially when using a preloader) at knowing what the stage is sometimes. This problem is increased two-fold when you're talking with an API that is supposed to know whatever is going on at where (that sentence made as little sense as the Kong API does).
SO.
If you've got a preloader preloadin' your shit, you'll totally want to connect to the API then (the sooner, the better!). Here's my Preloader.as:
package
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.utils.getDefinitionByName;
import flash.text.TextField;
import flash.display.LoaderInfo;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.system.Security;
[SWF(width = "640", height = "480", bgcolor="0x111111")]
/**
* Preloads shit.
* @author Zachary Weston Lewis
*/
public class Preloader extends MovieClip
{
private var s:Sprite, p:Sprite, r:Sprite;
public function Preloader()
{
trace("Starting.");
addEventListener(Event.ENTER_FRAME, checkFrame);
loaderInfo.addEventListener(ProgressEvent.PROGRESS, progress);
// show loader
s = new Sprite;
s.graphics.beginFill(0xff00ff);
s.graphics.drawRect(0, 340 - 40, 640, 80);
addChild(s);
p = new Sprite;
p.graphics.beginFill(0xff00ff);
p.graphics.drawRect(0, 240 - 40, 640, 80);
addChild(p);
r = new Sprite;
r.graphics.beginFill(0xff00ff);
r.graphics.drawRect(0, 140 - 40, 640, 80);
GameProperties.GAME_STAGE = stage;
addChild(r);
api();
}
private function api():void
{
//Set up our... API?
// Pull the API path from the FlashVars
var paramObj:Object = LoaderInfo(GameProperties.GAME_STAGE.loaderInfo).parameters;
// The API path. The "shadow" API will load if testing locally.
var apiPath:String = paramObj.kongregate_api_path ||
"http://www.kongregate.com/flash/API_AS3_Local.swf";
// Allow the API access to this SWF
Security.allowDomain(apiPath);
// Load the API
var request:URLRequest = new URLRequest(apiPath);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
loader.load(request);
GameProperties.GAME_STAGE.addChild(loader);
}
// This function is called when loading is complete
private function loadComplete(event:Event):void
{
// Save Kongregate API reference
trace("LOADED");
GameProperties._API = event.target.content;
// Connect to the back-end
GameProperties._API.services.connect();
// You can now access the API via:
// kongregate.services
// kongregate.user
// kongregate.scores
// kongregate.stats
// etc...
}
private function progress(e:ProgressEvent):void
{
// update loader
p.x = -640 + 640 * loaderInfo.bytesLoaded / loaderInfo.bytesTotal;
s.x = 640 - 640 * loaderInfo.bytesLoaded / loaderInfo.bytesTotal;
r.x = 640 - 640*loaderInfo.bytesLoaded / loaderInfo.bytesTotal;
}
private function checkFrame(e:Event):void
{
if (currentFrame == totalFrames)
{
removeEventListener(Event.ENTER_FRAME, checkFrame);
startup();
}
}
private function startup():void
{
// hide loader
stop();
loaderInfo.removeEventListener(ProgressEvent.PROGRESS, progress);
var mainClass:Class = getDefinitionByName("Main") as Class;
addChild(new mainClass(stage) as DisplayObject);
}
}
}
Something good to know is that GamePropertites just as a bunch of static variables and constants. Also, GamePropertites._API is cast as a "*". So, I just connect to the API and shove that shit in there.
NOW. AND THEN, SO.
Once the player begins the game, I load up GameWorld and start tracking stats. Here's how I submit stuff (I update my stats every 15 seconds, just to prevent timeouts and stuff. Depending on your game style, you may want to only submit once a game. I like sending a bunch of shit, because if you get one bad submit, you don't lose everything.)
THIS:
private function updateScore():void
{
_submitTimer += FP.elapsed;
GameProperties.STAT_TIME_ALIVE += FP.elapsed;
if (_submitTimer > GameProperties.STAT_SUBMIT_TIME)
{
//Submit some shit.
submitScores();
resetScores(false);
}
}
private function submitScores():void
{
var _api:* = GameProperties._API;
if (_api != null)
{
trace("=======================================");
_api.scores.submit(GameProperties.WORLD_PLAYER_SCORE);
_api.stats.submit("Add_RoundsFired", GameProperties.STAT_ROUNDS_FIRED);
_api.stats.submit("Add_RamKills", GameProperties.STAT_RAM_KILLS);
_api.stats.submit("Add_DamageTaken", GameProperties.STAT_DAMAGE_TAKEN);
_api.stats.submit("Add_DamageDealt", GameProperties.STAT_DAMAGE_DEALT);
_api.stats.submit("Add_TreasuresOpened", GameProperties.STAT_TREASURES_OPENED);
_api.stats.submit("Add_OverloadedTurrets", GameProperties.STAT_OVERLOADED_TURRETS);
_api.stats.submit("Add_MisslesDowned", GameProperties.STAT_MISSLES_DOWNED);
_api.stats.submit("Add_ExperienceGained", GameProperties.STAT_EXPERIENCE_GAINED);
_api.stats.submit("Add_BossesKilled", GameProperties.STAT_BOSSES_KILLED);
_api.stats.submit("Max_BossesKilled", GameProperties.STAT_BOSSES_KILLED_SEQUENTIAL);
_api.stats.submit("Add_PointGuardsKilled", GameProperties.STAT_POINTGUARDS);
_api.stats.submit("Add_LastShadowsKilled", GameProperties.STAT_LASTSHADOWS);
_api.stats.submit("Add_QuadroniansKilled", GameProperties.STAT_QUADRONIANS);
_api.stats.submit("Add_HellfiresKilled", GameProperties.STAT_HELLFIRES);
_api.stats.submit("Add_DefendersKilled", GameProperties.STAT_DEFENDERS);
_api.stats.submit("Add_SpittersKilled", GameProperties.STAT_SPITTERS);
_api.stats.submit("Max_EnemiesKilled", GameProperties.STAT_ENEMIES_SEQUENTIAL);
_api.stats.submit("Max_TimeAlive", GameProperties.STAT_TIME_ALIVE);
_api.stats.submit("Max_Highscore", GameProperties.WORLD_PLAYER_SCORE);
_api.stats.submit("Max_Level", GameProperties.PLAYER_LEVEL);
}
}
private function resetScores(init:Boolean):void
{
if (init)
{
GameProperties.STAT_BOSSES_KILLED_SEQUENTIAL = 0;
GameProperties.STAT_ENEMIES_SEQUENTIAL = 0;
GameProperties.STAT_TIME_ALIVE = 0;
}
else
{
GameProperties.STAT_BOSSES_KILLED_SEQUENTIAL += GameProperties.STAT_BOSSES_KILLED;
GameProperties.STAT_ENEMIES_SEQUENTIAL += GameProperties.STAT_POINTGUARDS + GameProperties.STAT_LASTSHADOWS + GameProperties.STAT_QUADRONIANS +
GameProperties.STAT_HELLFIRES + GameProperties.STAT_DEFENDERS + GameProperties.STAT_SPITTERS;
}
GameProperties.STAT_ROUNDS_FIRED = 0;
GameProperties.STAT_RAM_KILLS = 0;
GameProperties.STAT_DAMAGE_TAKEN = 0;
GameProperties.STAT_DAMAGE_DEALT = 0;
GameProperties.STAT_TREASURES_OPENED = 0;
GameProperties.STAT_OVERLOADED_TURRETS = 0;
GameProperties.STAT_MISSLES_DOWNED = 0;
GameProperties.STAT_EXPERIENCE_GAINED = 0;
GameProperties.STAT_BOSSES_KILLED = 0;
GameProperties.STAT_POINTGUARDS = 0;
GameProperties.STAT_LASTSHADOWS = 0;
GameProperties.STAT_QUADRONIANS = 0;
GameProperties.STAT_HELLFIRES = 0;
GameProperties.STAT_DEFENDERS = 0;
GameProperties.STAT_SPITTERS = 0;
_submitTimer = 0;
GameProperties.PLAYER_LEVEL = 1;
}
So, that is things.
My guess is the preloader thing. Also, gotta' give it time to work its magic. Also, make sure you type 'em in the website right.