but i have some problem
this is a server side code
- Code: Select all
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using PlayerIO.GameLibrary;
using System.Drawing;
using System.Timers;
namespace MyGame
{
public class Player : BasePlayer
{
public string Name;
public int posX;
public int posY;
public int rotation;
public int hit;
}
[RoomType("asteroidduel")]
public class GameCode : Game<Player>
{
private Player[] playerArr;
public override void GameStarted()
{
Console.WriteLine("Game is started: " + RoomId);
playerArr = new Player[5];
}
public override void GameClosed()
{
Console.WriteLine("Game closed: " + RoomId);
}
public override void UserJoined(Player player)
{
Console.WriteLine("UserJoined: " + player.ConnectUserId);
JoinGame(player);
Broadcast("ChatJoin", player.ConnectUserId);
}
public override void UserLeft(Player player)
{
Broadcast("ChatLeft", player.ConnectUserId);
for (int i = 0; i < playerArr.Length; i++)
{
if (playerArr[i] != null)
{
if (playerArr[i].ConnectUserId == player.ConnectUserId)
{
playerArr[i] = null;
break;
}
}
}
Broadcast("left", player.ConnectUserId);
}
public override void GotMessage(Player player, Message message)
{
for (int i = 0; i < playerArr.Length; i++)
{
if (playerArr[i] != null)
{
if (playerArr[i].ConnectUserId == player.ConnectUserId)
{
player = playerArr[i];
}
}
}
Console.WriteLine("GotMessage: " + message.Type);
switch (message.Type)
{
case "Chat":
{
String messageText = message.GetString(0);
Broadcast("Chat", player.ConnectUserId, messageText);
break;
}
case "RotateLeft":
{
player.rotation -= 5;
if (player.rotation <= 0) player.rotation = 360;
Broadcast("UpdateRocket", player.ConnectUserId, player.posX, player.posY, player.rotation);
break;
}
case "MoveUp":
{
player.posY += (int)(Math.Sin((player.rotation - 90) * (Math.PI / 180)) * 10);
player.posX += (int)(Math.Cos((player.rotation - 90) * (Math.PI / 180)) * 10);
if (player.posX < 32 || player.posX > 365 || player.posY < 42 || player.posY > 290)
{
player.posY -= (int)(Math.Sin((player.rotation - 90) * (Math.PI / 180)) * 10);
player.posX -= (int)(Math.Cos((player.rotation - 90) * (Math.PI / 180)) * 10);
}
else
{
Broadcast("UpdateRocket", player.ConnectUserId, player.posX, player.posY, player.rotation);
}
break;
}
case "RotateRight":
{
player.rotation += 5;
if (player.rotation >= 360) player.rotation = 0;
Broadcast("UpdateRocket", player.ConnectUserId, player.posX, player.posY, player.rotation);
break;
}
case "MoveDown":
{
player.posY -= (int)(Math.Sin((player.rotation - 90) * (Math.PI / 180)) * 10);
player.posX -= (int)(Math.Cos((player.rotation - 90) * (Math.PI / 180)) * 10);
if (player.posX < 32 || player.posX > 365 || player.posY < 42 || player.posY > 290)
{
player.posY += (int)(Math.Sin((player.rotation - 90) * (Math.PI / 180)) * 10);
player.posX += (int)(Math.Cos((player.rotation - 90) * (Math.PI / 180)) * 10);
}
else
{
Broadcast("UpdateRocket", player.ConnectUserId, player.posX, player.posY, player.rotation);
}
break;
}
case "Shoot":
{
Broadcast("Shoot", player.ConnectUserId);
break;
}
case "HitRocket":
{
Console.WriteLine("HitRocket: " + player.ConnectUserId);
player.hit++;
Broadcast("HitRocket", message.GetString(0), message.GetInt(1));
if (player.hit >= 5)
{
EndGame();
}
break;
}
}
}
private void EndGame()
{
String result = "";
for (int i = 0; i < playerArr.Length; i++)
{
if (playerArr[i] != null)
{
for (int j = i; j < playerArr.Length; j++)
{
if (playerArr[j] != null)
{
if (playerArr[j].hit < playerArr[i].hit)
{
Player tempPlayer = playerArr[i];
playerArr[i] = playerArr[j];
playerArr[j] = tempPlayer;
}
}
}
}
}
for (int i = 0; i < playerArr.Length; i++)
{
if (playerArr[i] != null)
{
result += (i + 1) + ". " + playerArr[i].ConnectUserId + ", " + playerArr[i].hit + "x hit\n";
}
}
Console.WriteLine("EndGame: " + result);
Broadcast("EndGame", result);
}
private void JoinGame(Player user)
{
Random rand = new Random();
int posX = rand.Next(350) + 30;
int posY = rand.Next(250) + 40;
user.posX = posX;
user.posY = posY;
user.rotation = 0;
user.hit = 0;
for (int i = 0; i < playerArr.Length; i++)
{
if (playerArr[i] == null)
{
playerArr.SetValue(user, i);
Console.WriteLine("user " + user.Id);
Broadcast("join", user.ConnectUserId, posX, posY);
break;
}
}
for (int i = 0; i < playerArr.Length; i++)
{
if (playerArr[i] != null && playerArr[i] != user)
{
Console.WriteLine("user " + user.Id);
Broadcast("addRocket", playerArr[i].ConnectUserId, playerArr[i].posX, playerArr[i].posY, playerArr[i].rotation);
break;
}
}
}
}
}
and this is a client code ( action script)
- Code: Select all
stop();
//musicSND.play(0,999);
informationMC.visible = false;
informationMC.newGameBtn.visible = false;
function handleJoin(connection:Connection):void
{
if(informationMC.visible) informationMC.visible = false;
chatTxt.text = "";
this.connection = connection;
connection.addDisconnectHandler(handleDisconnect);
connection.addMessageHandler("join", function(m:Message,playerUserId:String,posX:int,posY:int):void
{
for(var i:int=0;i<=playerArr.length;i++)
{
if ( i==0){
trace ("love");
}
}
var rocketMC:MovieClip = new hijau1();
rocketMC.x = posX;
rocketMC.y = posY;
addChild(rocketMC);
playerArr.push({playerUserId:playerUserId,rocket:rocketMC});
});
connection.addMessageHandler("addRocket", function(m:Message,playerUserId:String, posX:int, posY:int, rotation:int):void
{
if(playerUserId != client.connectUserId)
{
var rocketMC1:MovieClip = new putih1();
rocketMC1.x = posX;
rocketMC1.y = posY;
rocketMC1.rotation = rotation;
addChild(rocketMC1);
playerArr.push({playerUserId:playerUserId,rocket:rocketMC1});
}
});
connection.addMessageHandler("left", function(m:Message,playerUserId:String):void
{
for(var i:int=0;i<playerArr.length;i++)
{
if(playerArr[i].playerUserId == playerUserId)
{
removeChild(playerArr[i].rocket);
playerArr.splice(i,1);
break;
}
}
});
connection.addMessageHandler("reset", function(m:Message,isSelf:int):void
{
trace("Reset! "+isSelf);
});
connection.addMessageHandler("ChatJoin", function(m:Message,playerUserId:String):void
{
chatTxt.appendText(playerUserId+" join the game!\n");
});
connection.addMessageHandler("ChatLeft", function(m:Message,playerUserId:String):void
{
chatTxt.appendText(playerUserId+" leave the game!\n");
});
connection.addMessageHandler("Chat", function(m:Message,playerUserId:String,messageText:String):void
{
chatTxt.appendText(playerUserId+": "+messageText+"\n");
});
connection.addMessageHandler("UpdateRocket", function(m:Message,playerUserId:String, posX:int, posY:int, rotation:int):void
{
for(var i:int=0;i<playerArr.length;i++)
{
if(playerArr[i].playerUserId == playerUserId)
{
playerArr[i].rocket.x = posX;
playerArr[i].rocket.y = posY;
playerArr[i].rocket.rotation = rotation;
break;
}
}
});
connection.addMessageHandler("Shoot", function(m:Message,playerUserId:String):void
{
//shootSND.play(0,1);
for(var i:int=0;i<playerArr.length;i++)
{
if(playerArr[i].playerUserId == playerUserId)
{
var laserMC:MovieClip = new LaserMC();
laserMC.x = playerArr[i].rocket.x;
laserMC.y = playerArr[i].rocket.y;
laserMC.rotation = playerArr[i].rocket.rotation;
laserMC.addEventListener(Event.ENTER_FRAME,onUpdateLaser);
addChild(laserMC);
laserArr.push({playerUserId:playerUserId,laser:laserMC});
break;
}
}
});
connection.addMessageHandler("HitRocket", function(m:Message,playerUserId:String,laserIndex:int):void
{
//explodeSND.play(0,1);
for(var i:int=0;i<playerArr.length;i++)
{
if(playerArr[i].playerUserId == playerUserId && laserArr[laserIndex] != null)
{
playerArr[i].rocket.alpha = 0.4;
var timer:Timer = new Timer(2000,1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, function():void { playerArr[i].rocket.alpha = 1; });
timer.start();
laserArr[laserIndex].laser.removeEventListener(Event.ENTER_FRAME,onUpdateLaser);
removeChild(laserArr[laserIndex].laser);
laserArr.splice(laserIndex,1);
break;
}
}
});
connection.addMessageHandler("EndGame", function(m:Message,resultText:String):void
{
trace("EndGame, result="+resultText);
informationMC.visible = true;
informationMC.informationTxt.text = resultText;
informationMC.newGameBtn.visible = true;
connection.disconnect();
});
}
newGameBtn.addEventListener(MouseEvent.CLICK,onNewGame);
informationMC.newGameBtn.addEventListener(MouseEvent.CLICK,onNewGame);
function onNewGame(event:MouseEvent):void
{
for(var i:int=playerArr.length-1;i>=0;i--)
{
removeChild(playerArr[i].rocket);
playerArr.pop();
}
informationMC.visible = true;
informationMC.informationTxt.text = "Exiting and Rejoining room...";
connection.disconnect();
handleConnect(null);
}
sendBtn.addEventListener(MouseEvent.CLICK,onChat);
function onChat(event:MouseEvent):void
{
if(inputTxt.text != "")
{
connection.send("Chat",inputTxt.text);
inputTxt.text = "";
}
}
stage.focus = stage;
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
function onKeyDown(event:KeyboardEvent):void
{
switch(event.keyCode)
{
case 37:
connection.send("RotateLeft");
break;
case 38:
connection.send("MoveUp");
break;
case 39:
connection.send("RotateRight");
break;
case 40:
connection.send("MoveDown");
break;
case 32:
connection.send("Shoot");
break;
}
}
function onUpdateLaser(event:Event):void
{
var laserMC:MovieClip = event.currentTarget as MovieClip;
laserMC.y += (int)(Math.sin((laserMC.rotation - 90) * (Math.PI / 180)) * 10);
laserMC.x += (int)(Math.cos((laserMC.rotation - 90) * (Math.PI / 180)) * 10);
var laserIndex:int = getLaserIndex(laserMC);
if(laserArr[i].playerUserId == client.connectUserId)
{
for(var i:int=0;i<playerArr.length;i++)
{
if(laserMC.hitTestObject(playerArr[i].rocket) && playerArr[i].rocket.alpha==1 && playerArr[i].playerUserId != laserArr[laserIndex].playerUserId)
{
connection.send("HitRocket", playerArr[i].playerUserId, laserIndex);
}
}
}
if(laserMC.x < 0 || laserMC.x > 550 || laserMC.y < 0 && laserMC.y > 400)
{
laserMC.removeEventListener(Event.ENTER_FRAME,onUpdateLaser);
removeChild(laserMC);
laserArr.splice(laserIndex,1);
}
}
function getLaserIndex(laserMC:MovieClip):int
{
for(var i:int=0;i<laserArr.length;i++)
{
if(laserMC == laserArr[i].laser)
{
return i;
}
}
return -1;
}
I want the character the player 1 and player 2 is different, but in fact the character remains the same player 1 and player 2 different character.
this is my screenshot
i want di left side same character with right side character...
pleasee help me....