Class brl.gamecenter.GameCenter - leonard-thieu/monkey GitHub Wiki
The gamecenter module provides simple support for the iOS Game Center framework.
GameCenterAvail : Bool ()
GameCenterState : Int ()
ReportAchievement : Void ( percentComplete:Float, achievement_ID:String )
ReportScore : Void ( value:Int, leaderboard_ID:String )
ShowAchievments : Void ()
ShowLeaderboard : Void ( leaderboard_ID:String )
StartGameCenter : Void ()
GetGameCenter : GameCenter ()
The gamecenter module provides simple support for the iOS Game Center framework.
To use GameCenter in your game:
- Add your game in iTunes connect, Apple's portal for uploading iOS apps - http://itunesconnect.apple.com
- Enable Game Center support for your game in iTunes connect.
- Add Leaderboards and/or achievements to your game in iTunes connect.
- Import brl.gamecenter into your game and use the GameCenter class as required.
Notes:
- You must add your app to iTunes connect even if you are just testing/experimenting.
- You will need to be a registered Apple developer to use iTunes connect and therefore Game Center.
- Make sure your app's 'Bundle Identifier' in XCode matches the iTunes connect 'Bundle ID' for your app.
#If TARGET<>"ios"
#Error "GameCenter is only available on iOS"
#End
Import mojo
Import brl.gamecenter
Class MyApp Extends App
Field gamectr:GameCenter
Field coins_collected:=0
Const coins_total=25
Method OnCreate()
gamectr=GameCenter.GetGameCenter()
gamectr.StartGameCenter
SetUpdateRate 60
End
Method OnUpdate()
Select gamectr.GameCenterState()
Case 2
If MouseHit(0)
If Abs( MouseY-DeviceHeight/4 )<16
gamectr.ShowLeaderboard "HIGH_SCORES" 'use the Leaderboard ID you specified in iTunes connect here...
Else If Abs( MouseY-DeviceHeight/2 )<16
gamectr.ShowAchievements
Else If Abs( MouseY-DeviceHeight*3/4 )<16
random.Seed=Millisecs
gamectr.ReportScore Rnd(1000,2000),"HIGH_SCORES"
coins_collected+=1
gamectr.ReportAchievement coins_collected*100/coins_total,"COLLECT_ALL_COINS" 'use the Achievement ID you specified in iTunes connect here...
Endif
Endif
End
End
Method OnRender()
Cls
DrawText "Game Center state="+gamectr.GameCenterState(),DeviceWidth/2,0,.5,0
If gamectr.GameCenterState()<>2 SetColor 128,0,0
DrawText "Click here to show Leaderboard",DeviceWidth/2,DeviceHeight/4,.5,.5
DrawText "Click here to show Achievements",DeviceWidth/2,DeviceHeight/2,.5,.5
DrawText "Click here to report Score+Achievement",DeviceWidth/2,DeviceHeight*3/4,.5,.5
End
End
Function Main()
New MyApp
End
Method GameCenterAvail : Bool ()
Returns true if Game Center support is available.
Method GameCenterState : Int ()
Returns one of the following values:
State | Meaning |
---|---|
-1 | Error : Game Center cannot be used |
0 | Game Center is available but has not been started yet |
1 | Game Center has started and is busy connecting |
2 | Game Center has successfully connected and is awaiting further instructions |
3 | A leaderboard is currently showing |
4 | Game achievements are currently showing |
Report an achievement to Game Center.
The achievement_ID parameter should match the achievement ID specified in iTunes connect when the achievement was created.
Report a score to Game Center.
The leaderboard_ID parameter should match the leaderboard ID specified in iTunes connect when the leaderboard was created.
Method ShowAchievments : Void ()
Shows game achievements.
While game achievements are showing your game will continue to update (OnUpdate will continue to be called), however GameCenterState will return 4.
Shows the leaderboard with the given id.
While a leaderboard is showing your game will continue to update (OnUpdate will continue to be called), however GameCenterState will return 3.
Method StartGameCenter : Void ()
Starts Game Center.
Once started, you should wait until GameCenterState returns 2 before using any Game Center functionality.
Function GetGameCenter : GameCenter ()
Gets the global GameCenter object.