Skip to content

Instantly share code, notes, and snippets.

@Geokureli
Last active March 7, 2018 01:16
Show Gist options
  • Save Geokureli/b6972d352327157452fe39745d921463 to your computer and use it in GitHub Desktop.
Save Geokureli/b6972d352327157452fe39745d921463 to your computer and use it in GitHub Desktop.
Basic usage of the haxe NG.io API
package;
import io.newgrounds.NG;
import io.newgrounds.objects.Medal;
import io.newgrounds.objects.ScoreBoard;
import io.newgrounds.components.ScoreBoardComponent.Period;
import flixel.FlxG;
class NGTest
{
public function initNG():Void
{
NG.createAndCheckSession(FlxG.stage, "12345:AbcD3jf9h");
// Set the encryption cipher/format to RC4/Base64. AES128 and Hex are not implemented yet
NG.initEncryption("AbCdEfGhIjKlMnOpQrStUv==");// Found in you NG project view
if (NG.core.attemptingLogin)
{
/* a session_id was found in the loadervars, this means the user is playing on newgrounds.com
* and we should login shortly. lets wait for that to happen
*/
NG.core.onLogin.add(onNGLogin);
}
else
{
/* They are NOT playing on newgrounds.com, no session id was found. We must start one manually, if we want to.
* Note: This will cause a new browser window to pop up where they can log in to newgrounds
*/
NG.core.requestLogin(onNGLogin);
}
}
function onNGLogin():Void
{
trace ('logged in! user:${NG.core.user.name}');
// Load medals then call onNGMedalFetch()
NG.core.reguestMedals(onNGMedalFetch);
// Load Scoreboards hten call onNGBoardsFetch()
NG.core.requestScoreBoards(onNGBoardsFetch);
}
// --- MEDALS
function onNGMedalFetch():Void
{
// Reading medal info
for (id in NG.core.medals.keys())
{
var medal:Medal = NG.core.medals.get(id);
trace('loaded medal id:$id, name:${medal.name}, description:${medal.description}');
}
// Unlocking medals
var unlockingMedal = NG.core.medals.get(54001);// medal ids are listed in your NG project viewer
if (!medal.unlocked)
medal.sendUnlock();
}
// --- SCOREBOARDS
function onNGBoardsFetch():Void
{
// Reading medal info
for (id in NG.core.scoreBoards.keys())
{
var board:ScoreBoard = NG.core.scoreBoards.get(id);
trace('loaded scoreboard id:$id, name:${board.name}');
}
var board:ScoreBoard = NG.core.medals.get(7971);// ID found in NG project view
// Posting a score thats OVER 9000!
board.postScore(9001);
// --- To view the scores you first need to select the range of scores you want to see ---
// add an update listener so we know when we get the new scores
board.onUpdate.add(onNGScoresFetch);
board.requestScores(10);// get the best 10 scores ever logged
// more info on scores --- http://www.newgrounds.io/help/components/#scoreboard-getscores
}
function onNGScoresFetch():Void
{
for (score in NG.core.medals.get(7971).scores)
{
trace('score loaded user:${score.user.name}, score:${score.formatted_value}');
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment