Turn based multiplayer iOS game with flash and vitapoly.extension.gamekit.ane

107 views Asked by At

I am new to flash and AS3. I am trying to make a turn based iOS game using vitapoly.extension.gamekit. The problem is - I can't understand the usage. I have tried what they have told me but can't implement it correctly. I really appreciate if anyone can tell me how it works or how I can implement this in a perfect way.

Please please please.....help....

the thing I want now is simple - when game starts it will go to game center and find players. Once the match started - local player will take the turn. Ater that the other player will take turn.

Here's my code -

    import com.vitapoly.nativeextensions.gamekit.*;
    import com.vitapoly.nativeextensions.gamekit.events.*;
    import com.vitapoly.nativeextensions.gamekit.ios.*;
    import com.vitapoly.nativeextensions.gamekit.realtime.*;
    import com.vitapoly.nativeextensions.gamekit.turnbased.*;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;

    var supported:Boolean = GameKit.isSupported;
    if(supported){
    var gamekit:GameKit = new GameKit();
    }
    init();

    function init(){
    btn_start.addEventListener(MouseEvent.CLICK, gameStart);
    }

    function gameStart(m:MouseEvent):void{
    btn_start.visible = false;
    gameCenterStart();
    }
    function gameCenterStart():void{
    if(supported){
    gamekit.addEventListener(GameKit.LOCAL_PLAYER_AUTHENTICATED_EVENT,            function(e:Event):void {
        trace("LOCAL_PLAYER_AUTHENTICATED_EVENT");
        gameCenterMatch();
    });

    gamekit.addEventListener(GameKit.LOCAL_PLAYER_NOT_AUTHENTICATED_EVENT, function(e:ErrorEvent):void {
        trace("LOCAL_PLAYER_NOT_AUTHENTICATED_EVENT: " + e.text);

        // don’t allow to proceed if no single player
        // or just disable multiplayer interface
        disableMultiplayer(); // implement your own
    });
    gamekit.authenticateLocalPlayer();
    var localPlayer:LocalPlayer = gamekit.localPlayer;
    trace(localPlayer.displayName);

    // load friends with a completion callback function
    localPlayer.loadFriends(function(friends:Array):void {
        // localPlayer.friends property is now loaded
        for each (var friend:Player in localPlayer.friends)
            trace("friend: " + friend.displayName);
    });
    gamekit.showGameCenter();
   }
   }
function startGame():void{
box_mc.visible = true;
box_mc.player_txt.text = "Game Started";
box_mc.addEventListener(MouseEvent.CLICK, done);
gameCenterMatch();
}
function takeTurn():void{
box_mc.addEventListener(MouseEvent.CLICK, done);
}
function done(m:MouseEvent):void{
box_mc.player_txt.text = "Local Player Turn";
gameCenterMatch();
}
function displayGame():void{
box_mc.player_txt.text = "Other's Turn";
gameCenterMatch();
}
function showMsg(a:String){

}
function disableMultiplayer():void{
box_mc.player_txt.text = "No Multi";
}
function gameCenterMatch():void{
if(supported){
          gamekit.turnBasedMatchesController.addEventListener(TurnBasedMatchesController.MATCH_MAKER_FAILED_EVENT, function(e:ErrorEvent):void {
        trace("MATCH_MAKER_FAILED_EVENT: " + e.errorID + ", " + e.text);
    });

    gamekit.turnBasedMatchesController.addEventListener(TurnBasedMatchesController.MATCH_MAKER_CANCELLED_EVENT, function(e:Event):void {
        trace("MATCH_MAKER_CANCELLED_EVENT");
    });

    gamekit.turnBasedMatchesController.addEventListener(TurnBasedMatchesController.MY_TURN_EVENT, function(e:MatchEvent):void {
        trace("MY_TURN_EVENT: " + JSON.stringify(e));

        if (e.turnBasedMatch.isNewMatch)
            startGame(); // local player just started the game, so show new game  
        else if (e.turnBasedMatch.isCurrentMatch)
            takeTurn(); // it's the currently displaying match, so the local player take the turn
        else
            showMsg("It's your turn for another match: " + e.turnBasedMatch.matchID);
    });

    gamekit.turnBasedMatchesController.addEventListener(TurnBasedMatchesController.NOT_MY_TURN_EVENT, function(e:MatchEvent):void {
        trace("NOT_MY_TURN_EVENT: " + JSON.stringify(e));

        if (e.turnBasedMatch.isCurrentMatch)
            displayGame(); // not the player's turn, just show it
        else
            showMsg("It's someone else's turn for another match: " + e.turnBasedMatch.matchID);
    });

    gamekit.turnBasedMatchesController.addEventListener(TurnBasedMatchesController.MATCH_ENDED_EVENT, function(e:MatchEvent):void {
        trace("MATCH_ENDED_EVENT: " + JSON.stringify(e));

        if (e.turnBasedMatch.isCurrentMatch)
            displayGame(); // just show the end game
        else
            showMsg("Another match ended: " + e.turnBasedMatch.matchID);
    });

    gamekit.turnBasedMatchesController.addEventListener(TurnBasedMatchesController.PLAYER_QUIT_EVENT, function(e:MatchEvent):void {
        trace("PLAYER_QUIT_EVENT: " + JSON.stringify(e));

        // the player quit a match from match making interface
        // quit it with a lost outcome
        showMsg("player quit match: " + e.turnBasedMatch.matchID);
        e.turnBasedMatch.quitDuringTurn(GKTurnBasedMatchOutcome.GKTurnBasedMatchOutcomeLost, e.turnBasedMatch.matchData);
    });

    gamekit.turnBasedMatchesController.addEventListener(TurnBasedMatchesController.INVITE_PLAYERS_EVENT, function(e:InvitePlayersEvent):void {
        trace("INVITE_PLAYERS_EVENT: " + JSON.stringify(e));

        // invited players from Game Center, bring up match making interface with the array of invited players
        gamekit.turnBasedMatchesController.startMatch(2, 2, e.playersToInvite);
    });

    gamekit.turnBasedMatchesController.init();
    gamekit.turnBasedMatchesController.startMatch(2, 12);
    }
    }
0

There are 0 answers