Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package States
- {
- import flash.text.TextField;
- import flash.text.TextFormat;
- import flash.ui.Mouse;
- import org.flixel.*;
- import playerio.*;
- import Utility.Registry;
- public class MenuState extends FlxState
- {
- private var bumb2:FlxText;
- private var bumb:TextField = new TextField();
- private var time:Number = 0;
- private var t:FlxText;
- private var readyButton:FlxButton;
- private var isInRoom:Boolean = false;
- private var roomName:String = "";
- override public function create():void
- {
- FlxG.framerate = 60;
- FlxG.flashFramerate = 60;
- bumb.width = 200;
- bumb.embedFonts = true;
- bumb.text = "Type a Room Name"
- bumb.selectable = true;
- bumb.type = 'input';
- bumb.textColor = 0xFFFFFFFF;
- bumb.setTextFormat(new TextFormat("system", 10, 0xffffff, null, null, null, null, null, "center"));
- bumb.backgroundColor = 0x000000;
- bumb.border = true;
- bumb.antiAliasType = 'none';
- bumb.borderColor = 0xFFFFFF
- bumb.height = 15;
- bumb.x = FlxG.width / 2 - 100;
- bumb.y = 250;
- FlxG.stage.addChild(bumb);
- bumb2 = new FlxText(FlxG.width / 2 - 100, 30, 200, "Room Listing");
- bumb2.color = 0xFFFFFFFF
- bumb2.size = 12;
- bumb2.alignment = 'center';
- bumb2.height = 200;
- add(bumb2);
- FlxG.bgColor = 0xFF333333;
- //t = new FlxText(0,FlxG.height/2-40,FlxG.width,"Breakout");
- //t.size = 32;
- //t.alignment = "center";
- //add(t);
- t = new FlxText(FlxG.width/2-200,FlxG.height-30,400,"Connecting to Server...");
- t.size = 12;
- t.alignment = "center";
- add(t);
- readyButton = new FlxButton(310, 250, "Ready", readyUp);
- readyButton.visible = false;
- add(readyButton);
- //The Flixel mouse cursor looks awfully laggy with the framerate
- //so low, so we'll hide it away.
- FlxG.mouse.show();
- PlayerIO.connect(
- FlxG.stage, //Referance to stage
- "breakout-zwe2bywuqelkoperhwzg", //Game id (Get your own at playerio.com)
- "public", //Connection id, default is public
- "GuestUser", //Username
- "", //User auth. Can be left blank if authentication is disabled on connection
- null, //Current PartnerPay partner.
- handleConnect, //Function executed on successful connect
- handleError //Function executed if we recive an error
- );
- }
- private function handleConnect(client:Client):void{
- FlxG.log("Sucessfully connected to player.io");
- t.text = "Join an existing game, or make a new one";
- //Set developmentsever (Comment out to connect to your server online)
- client.multiplayer.developmentServer = "24.107.232.63:8184";
- Registry.client = client;
- //Create pr join the room test
- //client.multiplayer.createJoinRoom(
- //"test", //Room id. If set to null a random roomid is used
- //"MyCode", //The game type started on the server
- //true, //Should the room be visible in the lobby?
- //{}, //Room data. This data is returned to lobby list. Variabels can be modifed on the server
- //{}, //User join data
- //handleJoin, //Function executed on successful joining of the room
- //handleError //Function executed if we got a join error
- //);
- client.multiplayer.listRooms("MyCode", null, 10, 0, roomListing, null);
- }
- private function handleJoin(connection:Connection):void{
- FlxG.log("Sucessfully connected to the multiplayer server");
- t.text = "Connected, waiting on both players to be ready";
- readyButton.visible = true;
- Registry.connection = connection;
- connection.addMessageHandler("State Setup", quickLaunch);
- connection.addMessageHandler("begin", begin);
- connection.addMessageHandler("wait", wait);
- }
- private function quickLaunch(m:Message, userid:uint):void {
- Registry.playerIndex = m.getInt(0);
- }
- private function begin(m:Message, userid:uint):void {
- Registry.connection.removeMessageHandler("State Setup", quickLaunch);
- Registry.connection.removeMessageHandler("begin", begin);
- Registry.connection.removeMessageHandler("wait", wait);
- FlxG.stage.removeChild(bumb);
- FlxG.switchState(new PlayState());
- }
- private function wait(m:Message):void {
- if(m.getString(0) == "W1"){
- t.text = "Waiting on another player to join";
- }else {
- t.text = "Waiting on the other player to Ready";
- }
- }
- private function readyUp():void {
- Registry.connection.send("ready");
- }
- override public function update():void
- {
- time += FlxG.elapsed;
- if (time > 10) {
- Registry.client.multiplayer.listRooms("MyCode", null, 10, 0, roomListing, null);
- time = 0;
- }
- super.update();
- if (FlxG.keys.justPressed("ENTER")) {
- if(!isInRoom){
- Registry.client.multiplayer.createJoinRoom(
- bumb.text, //Room id. If set to null a random roomid is used
- "MyCode", //The game type started on the server
- true, //Should the room be visible in the lobby?
- {}, //Room data. This data is returned to lobby list. Variabels can be modifed on the server
- {}, //User join data
- handleJoin, //Function executed on successful joining of the room
- handleError //Function executed if we got a join error
- );
- roomName = bumb.text;
- isInRoom = true;
- }else {
- if (bumb.text != roomName) {
- Registry.connection.disconnect();
- Registry.client.multiplayer.createJoinRoom(
- bumb.text, //Room id. If set to null a random roomid is used
- "MyCode", //The game type started on the server
- true, //Should the room be visible in the lobby?
- {}, //Room data. This data is returned to lobby list. Variabels can be modifed on the server
- {}, //User join data
- handleJoin, //Function executed on successful joining of the room
- handleError //Function executed if we got a join error
- );
- roomName = bumb.text;
- }
- }
- }
- }
- private function roomListing(rooms:Array):void {
- if(FlxG.state is MenuState){
- bumb2.text = "Room Listing\n";
- for each(var a:RoomInfo in rooms) {
- bumb2.text += a.id + "\n";
- }
- FlxG.log("Listing Rooms");
- }
- }
- private function handleDisconnect():void{
- FlxG.log("Disconnected from server")
- }
- private function handleError(error:PlayerIOError):void{
- FlxG.log("got" + error)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement